要實現如上圖的效果,紅色區域是篩選條件的區域,隨著想向上滾動并且停留在頂部。
第一感覺是想用CoordinatorLayout.Behavior 來實現,將Toolbar+圖片區域+紅色篩選區域作為折疊的頭部。但是這樣SwipeRefreshLayout的下拉圈圈就不是從Toolbar下面下拉出來的。
我嘗試過自己用CoordinatorLayout.Behavior來實現這這個效果,而且上下滾動的效果已經實現了,但是寫到下拉刷新處的時候,我暫時放棄了,我想到了另外一個方法,就是仿CoordinatorLayout.Behavior 來攔截SwipeRefreshLayout 的nestedscroll事件,因為SwipeRefreshLayout也繼承了NestedScrollingParent。
public class MySwipeRefreshLayout extends SwipeRefreshLayout {
private static final String TAG = "MySwipeRefreshLayout";
private static int HEADER_EXPEND_HEIGTH =0;
private View rootScrollLayout;
private View mScrollUpChild;
private int curentTranslationY;
public MySwipeRefreshLayout(Context context) {
this(context, null);
}
public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
HEADER_EXPEND_HEIGTH = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, context.getResources().getDisplayMetrics());
curentTranslationY = HEADER_EXPEND_HEIGTH;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
rootScrollLayout = findViewById(R.id.rootScrollLayout);
Log.i(TAG, "MySwipeRefreshLayout: rootScrollLayout=" + rootScrollLayout);
rootScrollLayout.setTranslationY(curentTranslationY);
}
@Override
public boolean canChildScrollUp() {
if (mScrollUpChild != null) {
return ViewCompat.canScrollVertically(mScrollUpChild, -1);
}
return super.canChildScrollUp();
}
public void setScrollUpChild(View view) {
mScrollUpChild = view;
}
@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
Log.i(TAG, "onStartNestedScroll: child="+child);
//接收垂直方向的滾動事件,SwipeRefreshLayout默認是,只要正在刷新,父控件就不接受嵌套事件,由scrollview自己內部消耗
return isEnabled() && (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
// return super.onStartNestedScroll(child, target, nestedScrollAxes);
}
@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (dy > 0) {//向上滾動
if (curentTranslationY>=0) {//可以整體先向上滾動
int min = (int) Math.min(rootScrollLayout.getTranslationY(), dy);
curentTranslationY = (int) (rootScrollLayout.getTranslationY() - min);
rootScrollLayout.setTranslationY(curentTranslationY);
consumed[1] = min;
}
}
super.onNestedPreScroll(target, dx, dy, consumed);
Log.i(TAG, "onNestedPreScroll: ");
}
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
int max = 0;
if (dyConsumed <= 0 && dyUnconsumed < 0) {//向下滾動
if (curentTranslationY<HEADER_EXPEND_HEIGTH) {//可以整體先向下滾動
max = (int) Math.max(rootScrollLayout.getTranslationY()-HEADER_EXPEND_HEIGTH, dyUnconsumed);
curentTranslationY = (int) (rootScrollLayout.getTranslationY() - max);
rootScrollLayout.setTranslationY(curentTranslationY);
}
if (isRefreshing()) {//如果正在更新,則不顯示讓SwipeRefreshLayout處理,否則會二度下拉正在更新
return;
}
}
//如果還有多余的需要SwipeRefreshLayout 處理,比如再下拉顯示正在更新
super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed-max);
Log.i(TAG, "onNestedScroll: ");
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
Log.i(TAG, "drawChild: child=" + child);
return super.drawChild(canvas, child, drawingTime);
}
}
onlayout
主要處理紅色篩選區與scrollview所在區域的setTranslationY偏移,以露出imageView,實現滑動遮蓋的效果
由于滾動后系統有可能會調用onlayout方法,所以要記錄當前的curentTranslationY,在onlayout的時候設置他的偏移
onStartNestedScroll
只要是垂直方向的滾動就都要處理,SwipeRefreshLayout默認的是在Refreshing狀態就不接收子view的nested事件。而這里正在刷新的時候,整體還要能向上滾動,所以重寫了此處。
onNestedPreScroll
當開始向上滾動的時候,先整體向上移動SwipeRefreshLayout的內部控件,剩余的再給子view去內部消耗
向下滾動的時候,先由子view內部消耗。
onNestedScroll
當子view 滾動完畢后,如果是向下滾動,先整體向下移動SwipeRefreshLayout的內部控件,還有多余的就給SwipeRefreshLayout自己處理,比如顯示下拉進度條。而如果正在刷新的時候,是不應該二次顯示下拉進度條的,這也是為啥isRefreshing 為true的時候,直接return的原因,否則你會看到下拉進度條再次被下拉下來。
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.tospur.exmind.testswiperefresh.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>
<com.tospur.exmind.testswiperefresh.MySwipeRefreshLayout
android:id="@+id/mySwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/mImgView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@mipmap/test"
/>
<LinearLayout
android:id="@+id/rootScrollLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/mFilterView"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#f0f"
android:layout_height="72dp"></LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:background="#ff0"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
</com.tospur.exmind.testswiperefresh.MySwipeRefreshLayout>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.toolBar));
MySwipeRefreshLayout mySwipeRefreshLayout = (MySwipeRefreshLayout) findViewById(R.id.mySwipeRefreshLayout);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MAdapter());
mySwipeRefreshLayout.setScrollUpChild(recyclerView);
}
private class MAdapter extends RecyclerView.Adapter<MHolder> {
@Override
public MHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TextView textView = new TextView(parent.getContext());
return new MHolder(textView);
}
@Override
public void onBindViewHolder(MHolder holder, int position) {
holder.textView.setText("A "+position);
}
@Override
public int getItemCount() {
return 50;
}
}
private class MHolder extends RecyclerView.ViewHolder {
TextView textView;
public MHolder(View itemView) {
super(itemView);
textView = (TextView) itemView;
}
}
}
如果要實現平滑滾動,就是imageview隨著一起向上滾動,只需要做個簡單的調整
public class MySwipeRefreshLayout2 extends SwipeRefreshLayout {
private static final String TAG = "MySwipeRefreshLayout";
private static int HEADER_MAX_TOP_OFFSET =0;
private View rootScrollLayout;
private View mScrollUpChild;
private int curentTranslationY;
public MySwipeRefreshLayout2(Context context) {
this(context, null);
}
public MySwipeRefreshLayout2(Context context, AttributeSet attrs) {
super(context, attrs);
HEADER_MAX_TOP_OFFSET = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, context.getResources().getDisplayMetrics());
curentTranslationY = 0;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
rootScrollLayout = findViewById(R.id.rootScrollLayout);
Log.i(TAG, "onMeasure: getMeasuredHeight="+rootScrollLayout.getMeasuredHeight()+" getMeasuredWidth="+rootScrollLayout.getMeasuredWidth());
Log.i(TAG, "onMeasure: parent.getMeasuredHeight="+getMeasuredHeight());
int w = View.MeasureSpec.makeMeasureSpec(rootScrollLayout.getMeasuredWidth(),View.MeasureSpec.EXACTLY);
int h = View.MeasureSpec.makeMeasureSpec(getMeasuredHeight()+HEADER_MAX_TOP_OFFSET,View.MeasureSpec.EXACTLY);
rootScrollLayout.measure(w,h);//讓rootScrollLayout的子view知道,重新測量讓子view鋪滿rootScrollLayout
//擴大rootScrollLayout在SwipeRefreshLayout中的顯示區域
rootScrollLayout.layout(rootScrollLayout.getLeft(), rootScrollLayout.getTop(), rootScrollLayout.getRight(), rootScrollLayout.getBottom() + HEADER_MAX_TOP_OFFSET);
rootScrollLayout.setTranslationY(curentTranslationY);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//這里的measure沒有效果
/*rootScrollLayout = findViewById(R.id.rootScrollLayout);
Log.i(TAG, "onMeasure: getMeasuredHeight="+rootScrollLayout.getMeasuredHeight()+" getMeasuredWidth="+rootScrollLayout.getMeasuredWidth());
Log.i(TAG, "onMeasure: parent.getMeasuredHeight="+getMeasuredHeight());
int w = View.MeasureSpec.makeMeasureSpec(rootScrollLayout.getMeasuredWidth(),View.MeasureSpec.EXACTLY);
int h = View.MeasureSpec.makeMeasureSpec(getMeasuredHeight()+HEADER_MAX_TOP_OFFSET,View.MeasureSpec.EXACTLY);
rootScrollLayout.measure(w,h);*/
}
@Override
public boolean canChildScrollUp() {
if (mScrollUpChild != null) {
return ViewCompat.canScrollVertically(mScrollUpChild, -1);
}
return super.canChildScrollUp();
}
public void setScrollUpChild(View view) {
mScrollUpChild = view;
}
@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
Log.i(TAG, "onStartNestedScroll: child="+child);
//接收垂直方向的滾動事件,SwipeRefreshLayout默認是,只要正在刷新,父控件就不接受嵌套事件,由scrollview自己內部消耗
return isEnabled() && (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
// return super.onStartNestedScroll(child, target, nestedScrollAxes);
}
@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (dy > 0) {//向上滾動
if (curentTranslationY<=0) {//可以整體先向上滾動
int min = (int) Math.min(rootScrollLayout.getTranslationY()+ HEADER_MAX_TOP_OFFSET, dy);
curentTranslationY = (int) (rootScrollLayout.getTranslationY() - min);
rootScrollLayout.setTranslationY(curentTranslationY);
consumed[1] = min;
}
}
super.onNestedPreScroll(target, dx, dy, consumed);
Log.i(TAG, "onNestedPreScroll: ");
}
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
int max = 0;
if (dyConsumed <= 0 && dyUnconsumed < 0) {//向下滾動
if (curentTranslationY<0) {//可以整體先向下滾動
max = (int) Math.max(rootScrollLayout.getTranslationY(), dyUnconsumed);
curentTranslationY = (int) (rootScrollLayout.getTranslationY() - max);
rootScrollLayout.setTranslationY(curentTranslationY);
}
if (isRefreshing()) {//如果正在更新,則不顯示讓SwipeRefreshLayout處理,否則會二度下拉正在更新
return;
}
}
//如果還有多余的需要SwipeRefreshLayout 處理,比如再下拉顯示正在更新
super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed-max);
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
Log.i(TAG, "drawChild: child=" + child);
return super.drawChild(canvas, child, drawingTime);
}
}
這里要注意onlayout方法中的代碼,需要調整rootScrollLayout 的高度,防止向上整體滾動時,底部空白問題
如上圖底部。
剛開始在onMeasure中重新測量,死活出不來,然后想起CoordinatorLayout.Behavior 中也有調整位置大小的代碼,于是在onLayout中重新layout,擴大顯示區域,但是必須也要measure一下,否則里面的子view無法完全填充。
附布局代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.tospur.exmind.testswiperefresh.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>
<com.tospur.exmind.testswiperefresh.MySwipeRefreshLayout2
android:id="@+id/mySwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#00f"
android:id="@+id/rootScrollLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/mImgView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@mipmap/test"
/>
<LinearLayout
android:id="@+id/mFilterView"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#f0f"
android:layout_height="72dp"></LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:background="#ff0"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
</com.tospur.exmind.testswiperefresh.MySwipeRefreshLayout2>
</LinearLayout>