首先布局:布局很简单
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_home" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/ll_title_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#553190E8" android:orientation="horizontal"> <LinearLayout android:id="@+id/ll_title_search" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="30dp" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:orientation="horizontal"> <TextView android:id="@+id/home_tv_address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="5dip" android:layout_weight="1" android:singleLine="true" android:text="安徽合肥市经济开发区" android:textColor="#FFF" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="5dip" android:src="@mipmap/home_address_drop_down" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginBottom="12dp" android:layout_marginRight="10dp" android:layout_marginTop="12dp" android:layout_weight="3" android:background="@drawable/home_search_text_background"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_marginRight="10dip" android:layout_weight="1" android:orientation="horizontal"> <ImageView android:layout_width="15dp" android:layout_height="15dp" android:layout_gravity="center_vertical" android:layout_marginLeft="4dp" android:src="@mipmap/home_search" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="输入商家、商品名称" android:textColor="#FFF" android:textSize="12sp" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> </RelativeLayout>二、当向下时改变头部透明度变化
private int sumY = 0; private float duration = 150;//0-150之间透明度变化 private ArgbEvaluator evaluator=new ArgbEvaluator(); private RecyclerView.OnScrollListener listener = new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); sumY+=dy; int bgColor=0x553190E8; if(sumY<0){ bgColor=0x553190E8; }else if(sumY>150){ bgColor=0xFF3190E8; }else{ bgColor= (int) evaluator.evaluate(sumY/duration,0x553190E8,0xFF3190E8); } llTitleContainer.setBackgroundColor(bgColor); } };