自定义View:
package com.example.jingdong.FirstPages; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.util.Log; import android.widget.ScrollView; import com.example.jingdong.FirstPages.FirstPageSearchs.FirstPageSearch; import com.youth.banner.Banner; public class AlphaTitleScrollView extends ScrollView { public static final String TAG = "AlphaTitleScrollView"; private int mSlop; private FirstPageSearch mytitleview; private Banner banner; public AlphaTitleScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } public AlphaTitleScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public AlphaTitleScrollView(Context context) { this(context, null); } private void init(Context context) { // mSlop = ViewConfiguration.get(context).getScaledDoubleTapSlop(); mSlop = 10; Log.i(TAG, mSlop + ""); } //Activity中调用,获取了本自定义View的控件id,就可调用. public void setTitleAndHead(FirstPageSearch mytitleview,Banner banner) { this.mytitleview = mytitleview; //搜索栏,头布局 this.banner = banner; //需要和搜索栏对比滑动高度的bannerbuju,这里最上边的布局是banner,别的也行 } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { float headHeight = banner.getMeasuredHeight() - mytitleview.getMeasuredHeight(); int alpha = (int) (((float) t / headHeight) * 255); if (alpha >= 255) alpha = 255; if (alpha <= mSlop) alpha = 0; mytitleview.setBackgroundColor(Color.argb(alpha, 0, 0, 0)); super.onScrollChanged(l, t, oldl, oldt); } }
sv.setTitleAndHead(fs,banner); //自定义View里的方法 sv.setBackgroundColor(Color.argb(0, 1, 1, 1)); //初始化搜索栏背景颜色
关于自定义ScrollView自动滑动的问题,自布局焦点:
<com.bwie.dongchangqi.personaltailor.CustomViews.CustomScrollView android:id="@+id/found_scrollview_show" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" //-------加入----------- android:focusableInTouchMode="true" //--------加入--------- > <android.support.v7.widget.RecyclerView android:id="@+id/found_recyclerview_show" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> </com.bwie.dongchangqi.personaltailor.CustomViews.CustomScrollView>