RecyclerView使用案例一之滑动冲突

来源:互联网 发布:淘宝订单险 编辑:程序博客网 时间:2024/06/01 07:18

RecyclerView的使用

在学习完ListView的使用后,我们可能会想,这么难用的控件到底是谁创造出来的,但是它却确确实实的陪伴我们度过了无数个岁月,并且屹立不倒,现在我们需要跟紧时代,去使用RecyclerView了,以后或许还会有更叼的控件供我们使用,那么现在我先看看这RecyclerView吧。

任何一款软件的推出都不免需要处理各类冲突的事件(滑动冲突、点击事件冲突等等),现在我们来处理滑动的各类情况:

1)ScrollView中嵌套RecyclerView的滑动冲突

ScrollView嵌套ListView解决滑动冲突是自定义了listView去测量listView高度,而recycerView嵌套在scrollView中需要重写layoutManager
代码如下:
自定义LinearLayoutManager
import android.content.Context;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.util.Log;import android.view.View;import android.view.ViewGroup;/** *自定义LinearLayoutManager * Created by ${Dota.Wang} on 2017/8/21. */public class FullyLinearLayoutManager extends LinearLayoutManager {    private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();    public FullyLinearLayoutManager(Context context) {        super(context);    }    public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {        super(context, orientation, reverseLayout);    }    private int[] mMeasuredDimension = new int[2];    @Override    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,                          int widthSpec, int heightSpec) {        final int widthMode = View.MeasureSpec.getMode(widthSpec);        final int heightMode = View.MeasureSpec.getMode(heightSpec);        final int widthSize = View.MeasureSpec.getSize(widthSpec);        final int heightSize = View.MeasureSpec.getSize(heightSpec);        Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode                + " \nheightMode " + heightSpec                + " \nwidthSize " + widthSize                + " \nheightSize " + heightSize                + " \ngetItemCount() " + getItemCount());        int width = 0;        int height = 0;        for (int i = 0; i < getItemCount(); i++) {            measureScrapChild(recycler, i,                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),                    mMeasuredDimension);            if (getOrientation() == HORIZONTAL) {                width = width + mMeasuredDimension[0];                if (i == 0) {                    height = mMeasuredDimension[1];                }            } else {                height = height + mMeasuredDimension[1];                if (i == 0) {                    width = mMeasuredDimension[0];                }            }        }        switch (widthMode) {            case View.MeasureSpec.EXACTLY:                width = widthSize;            case View.MeasureSpec.AT_MOST:            case View.MeasureSpec.UNSPECIFIED:        }        switch (heightMode) {            case View.MeasureSpec.EXACTLY:                height = heightSize;            case View.MeasureSpec.AT_MOST:            case View.MeasureSpec.UNSPECIFIED:        }        setMeasuredDimension(width, height);    }    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,                                   int heightSpec, int[] measuredDimension) {        try {            View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException            if (view != null) {                RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();                int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,                        getPaddingLeft() + getPaddingRight(), p.width);                int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,                        getPaddingTop() + getPaddingBottom(), p.height);                view.measure(childWidthSpec, childHeightSpec);                measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;                measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;                recycler.recycleView(view);            }        } catch (Exception e) {            e.printStackTrace();        } finally {        }    }}
自定义GridLayoutManager
代码如下:
import android.content.Context;import android.support.v7.widget.GridLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;/** *自定义GridLayoutManager * Created by ${Dota.Wang} on 2017/8/21. */public class FullyGridLayoutManager extends GridLayoutManager {          private int mwidth = 0;          private int mheight = 0;          public FullyGridLayoutManager(Context context, int spanCount) {              super(context, spanCount);          }          public FullyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {              super(context, spanCount, orientation, reverseLayout);          }          private int[] mMeasuredDimension = new int[2];          public int getMwidth() {              return mwidth;          }          public void setMwidth(int mwidth) {              this.mwidth = mwidth;          }          public int getMheight() {              return mheight;          }          public void setMheight(int mheight) {              this.mheight = mheight;          }          @Override          public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {              final int widthMode = View.MeasureSpec.getMode(widthSpec);              final int heightMode = View.MeasureSpec.getMode(heightSpec);              final int widthSize = View.MeasureSpec.getSize(widthSpec);              final int heightSize = View.MeasureSpec.getSize(heightSpec);              int width = 0;              int height = 0;              int count = getItemCount();              int span = getSpanCount();              for (int i = 0; i < count; i++) {                  measureScrapChild(recycler, i,                          View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),                          View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),                          mMeasuredDimension);                  if (getOrientation() == HORIZONTAL) {                      if (i % span == 0) {                          width = width + mMeasuredDimension[0];                      }                      if (i == 0) {                          height = mMeasuredDimension[1];                      }                  } else {                      if (i % span == 0) {                          height = height + mMeasuredDimension[1];                      }                      if (i == 0) {                          width = mMeasuredDimension[0];                      }                  }              }              switch (widthMode) {                  case View.MeasureSpec.EXACTLY:                      width = widthSize;                  case View.MeasureSpec.AT_MOST:                  case View.MeasureSpec.UNSPECIFIED:              }              switch (heightMode) {                  case View.MeasureSpec.EXACTLY:                      height = heightSize;                  case View.MeasureSpec.AT_MOST:                  case View.MeasureSpec.UNSPECIFIED:              }              setMheight(height);              setMwidth(width);              setMeasuredDimension(width, height);          }          private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,                                         int heightSpec, int[] measuredDimension) {              if (position < getItemCount()) {                  try {                      View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException                      if (view != null) {                          RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();                          int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,                                  getPaddingLeft() + getPaddingRight(), p.width);                          int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,                                  getPaddingTop() + getPaddingBottom(), p.height);                          view.measure(childWidthSpec, childHeightSpec);                          measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;                          measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;                          recycler.recycleView(view);                      }                  } catch (Exception e) {                      e.printStackTrace();                  }              }          }      }
自定义StaggeredGridLayoutManager
代码如下:
import android.support.v7.widget.RecyclerView;import android.support.v7.widget.StaggeredGridLayoutManager;import android.view.View;import android.view.ViewGroup;/** * StaggeredGridLayoutManager不规则排列(类似于瀑布流)的布局管理器 * Created by ${Dota.Wang} on 2017/8/21. */public class ExStaggeredGridLayoutManager extends StaggeredGridLayoutManager {    public ExStaggeredGridLayoutManager(int spanCount, int orientation) {        super(spanCount, orientation);    }    // 尺寸的数组,[0]是宽,[1]是高    private int[] measuredDimension = new int[2];    // 用来比较同行/列那个item罪宽/高    private int[] dimension;    @Override    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {        // 宽的mode+size        final int widthMode = View.MeasureSpec.getMode(widthSpec);        final int widthSize = View.MeasureSpec.getSize(widthSpec);        // 高的mode + size        final int heightMode = View.MeasureSpec.getMode(heightSpec);        final int heightSize = View.MeasureSpec.getSize(heightSpec);        // 自身宽高的初始值        int width = 0;        int height = 0;        // item的数目        int count = getItemCount();        // item的列数        int span = getSpanCount();        // 根据行数或列数来创建数组        dimension = new int[span];        for (int i = 0; i < count; i++) {            measureScrapChild(recycler, i,                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredDimension);            // 如果是竖直的列表,计算item的高,否则计算宽度            //Log.d("LISTENER", "position " + i + " height = " + measuredDimension[1]);            if (getOrientation() == VERTICAL) {                dimension[findMinIndex(dimension)] += measuredDimension[1];            } else {                dimension[findMinIndex(dimension)] += measuredDimension[0];            }        }        if (getOrientation() == VERTICAL) {            height = findMax(dimension);        } else {            width = findMax(dimension);        }        switch (widthMode) {            // 当控件宽是match_parent时,宽度就是父控件的宽度            case View.MeasureSpec.EXACTLY:                width = widthSize;                break;            case View.MeasureSpec.AT_MOST:                break;            case View.MeasureSpec.UNSPECIFIED:                break;        }        switch (heightMode) {            // 当控件高是match_parent时,高度就是父控件的高度            case View.MeasureSpec.EXACTLY:                height = heightSize;                break;            case View.MeasureSpec.AT_MOST:                break;            case View.MeasureSpec.UNSPECIFIED:                break;        }        // 设置测量尺寸        setMeasuredDimension(width, height);    }    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,                                   int heightSpec, int[] measuredDimension) {        // 挨个遍历所有item        if (position < getItemCount()) {            try {                View view = recycler.getViewForPosition(position);//fix 动态添加时报IndexOutOfBoundsException                if (view != null) {                    RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) view.getLayoutParams();                    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight(), lp.width);                    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), lp.height);                    // 子view进行测量,然后可以通过getMeasuredWidth()获得测量的宽,高类似                    view.measure(childWidthSpec, childHeightSpec);                    // 将item的宽高放入数组中                    measuredDimension[0] = view.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;                    measuredDimension[1] = view.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;                    recycler.recycleView(view);                }            } catch (Exception e) {                e.printStackTrace();            }        }    }    private int findMax(int[] array) {        int max = array[0];        for (int value : array) {            if (value > max) {                max = value;            }        }        return max;    }    /**     * 得到最数组中最小元素的下标     *     * @param array     * @return     */    private int findMinIndex(int[] array) {        int index = 0;        int min = array[0];        for (int i = 0; i < array.length; i++) {            if (array[i] < min) {                min = array[i];                index = i;            }        }        return index;    }}
遇到RecyclerView外部套用Scrollview的情况的处理方法:
方法一:
当还是存在滑动冲突的情况,有可能是你的item中的子view夺取了焦点,可以试着在recyclerview外层套一层relativelayout,然后给relativelayout加上android:descendantFocusability=”blocksDescendants”
descendantFocusability属性的值有三种:beforeDescendants:viewgroup会优先其子类控件而获取到焦点afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
方法二:
解决方法是重写最外层的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);          }      }
注:在4.x上重写布局管理器就可以解决了,但在5.1以上的版本中会缺少滑动的一个惯性的效果,这时我们就需要重写ScrollView中的onInterceptTouchEvent()方法来实现。

冲突处理的未完待续… …

参考网址:http://www.cnblogs.com/wangying222/p/5842438.html

原创粉丝点击