title: ScrollView嵌套Recyclerview
date: 2017-01-19 10:33:27
tags: tips
-
老問題了,以前用一些別的方式解決了比如linearlayout,比如listview的type,比如footview之類的
但這次的需求不太一樣
?
上面是一個recyclerview全部顯示,滑到底了,下面一個“今日推薦”,再下面一個資訊類的recyclerview有分頁功能,滑到底了加載下一頁。
-
思路就是讓recyclerview不能滑動,滑動交給外層的scrollview,將recyclerview替換成下面這個
public class NoScrollRecyclerView extends RecyclerView { public NoScrollRecyclerView(Context context) { super(context); } public NoScrollRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public NoScrollRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthSpec, int heightSpec) { int IheightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthSpec, IheightSpec); } }
至少數據就都能顯示了
然后會發現,自帶的慣性滑動沒有了,阻尼也有點別扭,那么把scrollview也給換了吧,加上慣性
public class MyScrollView extends ScrollView { private int downX; private int downY; private int mTouchSlop; public MyScrollView(Context context) { super(context); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } @Override public boolean onInterceptTouchEvent(MotionEvent e) { int action = e.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: downX = (int) e.getRawX(); downY = (int) e.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveY = (int) e.getRawY(); if (Math.abs(moveY - downY) > mTouchSlop) { return true; } } return super.onInterceptTouchEvent(e); } }
ps:其實還有一些別的問題,我的recyclerview用的是hongyang的那baseAdapt,他好久沒維護了,有不少問題。。不過與標題無關,這篇文章暫且按下不表。