美文网首页
Android-RecyclerView 分析

Android-RecyclerView 分析

作者: 荞麦穗 | 来源:发表于2019-05-15 17:51 被阅读0次
RecyclerView的使用主要分为4大块
//1、设置布局管理器
recycler.setLayoutManager(layout);
//2、设置adapter
recycler.setAdapter(adapter)
//3、设置Item增加、移除动画
recycler.setItemAnimator(new DefaultItemAnimator());
//4、分割线
recycler.addItemDecoration(new DividerItemDecoration(getActivity(), ItemDecoration.HORIZONTAL));
//5、拖拽帮助类
itemTouchHelper.attachToRecyclerView(mRecyclerView);

RecyclerView 没有addHeaderView、addFooterView等方法。可以让RecyclerView 加载不同类型的视图解决,也可以加载和RecyclerView同级的父View里解决。

1、分割线
LinearLayoutManager manager = new LinearLayoutManager(this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(4,LinearLayout.VERTICAL);
2、适配器
RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>
MyViewHolder extends RecyclerView.ViewHolder
3、Item动画
DefaultItemAnimator
4、分割线
class ItemDecoration extends RecyclerView.ItemDecoration {
        private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
        public static final int HORIZONTAL = LinearLayoutManager.HORIZONTAL;
        public static final int VERTICAL = LinearLayoutManager.VERTICAL;
        private Drawable mDivider;
        private int mOrientation;

        public ItemDecoration(Context context, int orientation) {
            TypedArray a = context.obtainStyledAttributes(ATTRS);
            mDivider = a.getDrawable(0);
            a.recycle();
            setOrientation(orientation);
        }

        private void setOrientation(int orientation) {
            if (orientation != HORIZONTAL && orientation != VERTICAL) {
                throw new IllegalStateException("invalid orientation");
            }
            mOrientation = orientation;
        }

        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDrawOver(c, parent, state);
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
            if (mOrientation == HORIZONTAL) {
                drawHorizontal(c, parent);
            } else {
                drawVertical(c, parent);
            }
        }

        private void drawHorizontal(Canvas c, RecyclerView parent) {
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                int left = child.getLeft();
                int right = child.getRight();
                int top = child.getBottom();
                int bottom = top + mDivider.getIntrinsicHeight();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }

        private void drawVertical(Canvas c, RecyclerView parent) {
            int top = parent.getTop() - parent.getPaddingTop();
            int bottom = parent.getHeight() - parent.getPaddingBottom();
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                int left = child.getRight();
                int right = left + mDivider.getIntrinsicWidth();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            if (mOrientation == HORIZONTAL) {
                outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
            } else {
                outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
            }
        }


    }
Item拖拽动画
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //actionState : action状态类型,有三类 ACTION_STATE_DRAG (拖曳),ACTION_STATE_SWIPE(滑动),ACTION_STATE_IDLE(静止)
//                int dragFlags = makeFlag(ItemTouchHelper.ACTION_STATE_DRAG
//                //支持上下左右的拖曳
//                ,ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT);
        int dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        return makeMovementFlags(dragFlag,0);//直接返回0表示不支持拖曳和滑动
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int fromPosition = viewHolder.getAdapterPosition();//要拖曳的位置
        int toPosition = target.getAdapterPosition();//要放置的目标位置
        Collections.swap(list, fromPosition, toPosition);//做数据的交换
        adapter.notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int position = viewHolder.getAdapterPosition();//获取要滑动删除的Item位置
        list.remove(position);//删除数据
        adapter.notifyItemRemoved(position);
    }
});
itemTouchHelper.attachToRecyclerView(recycler);

相关文章

网友评论

      本文标题:Android-RecyclerView 分析

      本文链接:https://www.haomeiwen.com/subject/zvghuqtx.html