package slide.azheng.pers.coordinatorlayoutcollapsingtoolbarlayoutnestedscrollview;
import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener { private LinearLayout mLinearLayout_shrink;
private TextView mTextView_name;
private TextView mTextView_sex;
private TextView mTextView_year;
private int mMaskColor;
private AppBarLayout appbarLayout_behavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
private void initView() {
appbarLayout_behavior= findViewById(R.id.appbarLayout_behavior);
mLinearLayout_shrink = findViewById(R.id.mLinearLayout_shrink);
mTextView_name = findViewById(R.id.mTextView_name);
mTextView_sex = findViewById(R.id.mTextView_sex);
mTextView_year = findViewById(R.id.mTextView_year);
mMaskColor = getResources().getColor(R.color.colorWrite);
appbarLayout_behavior.addOnOffsetChangedListener(this);
}
initView();
}
//AppBarLayout的監(jiān)聽方法
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
int offset = Math.abs(verticalOffset);
int total = appBarLayout.getTotalScrollRange(); //偏移量
int alphaOut = (255 - offset) < 0 ? 0 : 255 - offset;
int maskColorOut = Color.argb(alphaOut, Color.red(mMaskColor),
Color.green(mMaskColor), Color.blue(mMaskColor));
if (offset <= total * 140 / 200) {
mLinearLayout_shrink.setVisibility(View.VISIBLE);
} else {
mLinearLayout_shrink.setVisibility(View.GONE);
}
mTextView_name.setTextColor(maskColorOut);
mTextView_sex.setHintTextColor(maskColorOut);
mTextView_year.setTextColor(maskColorOut);
}}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbarLayout_behavior"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#02A8F3"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/mCollapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_backTop"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#02A8F3"
app:layout_collapseMode="pin">
<TextView
android:id="@+id/back_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="返回"
android:textColor="#fff"
android:textSize="24sp" />
<TextView
android:id="@+id/exampaper_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:text="Title"
android:textColor="#fff"
android:textSize="19sp" />
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="@+id/mLinearLayout_shrink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="90dp"
android:orientation="vertical"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7">
<TextView
android:id="@+id/exampaper_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="content"
android:textColor="#fff"
android:textSize="14sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="36dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/mTextView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:maxLength="15"
android:text="name"
android:textColor="#fff"
android:textSize="12sp" />
<TextView
android:id="@+id/mTextView_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLines="1"
android:text="男"
android:textColor="#fff"
android:textSize="12sp" />
<TextView
android:id="@+id/mTextView_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="13歲"
android:textColor="#fff"
android:textSize="12sp" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
app:layout_anchor="@id/nestedScrollView"
app:layout_anchorGravity="center_vertical|right|bottom"
app:pressedTranslationZ="10dp"
app:rippleColor="@color/colorAccent" />
</android.support.design.widget.CoordinatorLayout>
NestedScrollView嵌套R(shí)ecyclerView滑動(dòng)沖突
mRecyclerView.setNestedScrollingEnabled(false); //添加一句這個(gè)就好