android RecyclerView

引用

在项目的build.gradle添加依赖

compile 'com.android.support:recyclerview-v7:23.4.0'

RecyclierView使用的基本方法

recyclerView.setAdapter(); 添加适配器(必须)

recyclerView.setLayoutManager(); 选择一种布局(必须)

recyclerAdapter.setHeaderView(); 添加头布局

recyclerAdapter.setFooterView(); 添加底部布局

recyclerView.setItemAnimator(new DefaultItemAnimator()); 添加默认动画

recyclerView.addItemDecoration(); 添加分割线

Layout Manager布局管理器

1.三种布局管理器:

new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) 以垂直或者水平列表方式展示Item,第三个参数是否颠倒数据显示

new GridLayoutManager(this,4)  以网格方式展示Item,第二个参数代表列数

new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL)  以瀑布流方式展示Item,第一个参数代表列数

2.常用方法

canScrollHorizontally();//能否横向滚动
canScrollVertically();//能否纵向滚动
scrollToPosition(int position);//滚动到指定位置
setOrientation(int orientation);//设置滚动的方向
getOrientation();//获取滚动方向
findViewByPosition(int position);//获取指定位置的Item View
findFirstCompletelyVisibleItemPosition();//获取第一个完全可见的Item位置
findFirstVisibleItemPosition();//获取第一个可见Item的位置
findLastCompletelyVisibleItemPosition();//获取最后一个完全可见的Item位置
findLastVisibleItemPosition();//获取最后一个可见Item的位置

 Adapter适配器模板

RecyclerView必须自定义适配器,并且自己创建事件监听,布局和数据的绑定顺序,每个Item都会执行一遍:getIItemViewType(绑定类型) > onCreateViewHolder(选择布局类型) > onBindViewHolder(绑定数据)

notifyItemInserted(position);插入一个item

notifyItemRemoved(position); 移除一个item

notifyItemChanged(position) 刷新item

public class DataRecyclerAdapter extends RecyclerView.Adapter<DataRecyclerAdapter.MyViewHolder>{
    private List<String> list;
    private Context context;
    private List<Integer> mHeights;
    private static int TYPE_TITLE=0;
    private static int TYPE_FOOTER=1;
    private static int TYPE_CONTENT =2;
    private View mHeaderView;
    private View mFooterView;

    public DataRecyclerAdapter(Context context, List<String> list) {
        this.list=list;
        mHeights = new ArrayList<>();
        this.context=context;
    }

    //根据不同的类型适应不用的布局
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == TYPE_TITLE) {
            return new MyViewHolder(mHeaderView);
        }
        if(mFooterView != null && viewType == TYPE_FOOTER) {
            return new MyViewHolder(mFooterView);
        }
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.options_item3,parent,false));
    }

    //给item分布不同的类型
    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return TYPE_TITLE;
         }else if(position==list.size()){
            return TYPE_FOOTER;
        }else{
            return TYPE_CONTENT;
        }
    }

    public View getHeaderView() {
        return mHeaderView;
    }
    //添加自定义的头部
    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getFooterView() {
        return mFooterView;
    }
    //添加自定义底部
    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(list.size()+1);
    }
    //主体布局
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        if(getItemViewType(position) != TYPE_CONTENT) {
            return;
        }
            holder.textView.setText(list.get(position-1));

        if (mOnItemClickLitener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener(){
                @Override
                public boolean onLongClick(View v){
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return true;
                }
            });
        }
    }

    //获取item总数
    @Override
    public int getItemCount() {
         if(mFooterView !=null){
            return list.size()+1;
        }else {
            return list.size();
        }
    }
    //监听器接口
    public interface OnItemClickLitener{
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnMyItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

   //头和底部的布局
    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        MyViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView||itemView == mFooterView){
                return;
            }
            textView= (TextView) itemView.findViewById(R.id.options_tv);
            textView.setText("UUU");
        }
    }
    //模拟新增的数据
    public void addData(int position) {
        list.add(position, "Insert 数据");
        notifyItemInserted(position+1);
        Toast.makeText(context,""+list.size(),Toast.LENGTH_SHORT).show();
    }
    //移除一个布局
    public void removeData(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }

实例

private void initDataRecyclerAdapter(){
    recyclerAdapter=new DataRecyclerAdapter(this,mData);
    //布局管理器
    recyclerView.setLayoutManager( new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    //分割线
    recyclerView.addItemDecoration(new DividerItemDecoration(this,
            DividerItemDecoration.HORIZONTAL));
    //使用默认动画
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    //不使用默认的布局类型,添加自定义头部布局
    recyclerAdapter.setHeaderView(LayoutInflater.from(this).inflate(R.layout.options_title, recyclerView, false));
    //不使用默认的布局类型,添加自定义底部布局
    LayoutInflater inflater= LayoutInflater.from(this);
    View footerView = inflater.inflate(R.layout.options_footer, null);
    recyclerAdapter.setFooterView(footerView);
    //底部布局事件,增加一个item
    footerView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this,"展开",Toast.LENGTH_SHORT).show();
            recyclerAdapter.addData(mData.size());
        }
    });
    recyclerView.setAdapter(recyclerAdapter);
    recyclerAdapter.setOnMyItemClickLitener(new DataRecyclerAdapter.OnItemClickLitener() {
        //item的点击事件,移除一个item
        @Override
        public void onItemClick(View view, int position) {
            recyclerAdapter.removeData(position);
            Toast.makeText(MainActivity.this,""+mData.size(),Toast.LENGTH_SHORT).show();
        }
        //item的长按事件,显示item下标
        @Override
        public void onItemLongClick(View view, int position) {
            Toast.makeText(MainActivity.this,"第"+position+"个",Toast.LENGTH_SHORT).show();
        }
    });
}
//模拟数据源
private void addData(){
    mData= new ArrayList<>();
    for (int i=1;i<20;i++) {
        mData.add("数据"+i+"个");
    }
}

ItemTouchHelper滑动拖拽

1.getMovementFlags

用于设置是否处理拖拽事件和滑动事件,如果是列表类型的,拖拽只有ItemTouchHelper.UP、ItemTouchHelper.DOWN两个方向
如果是网格类型的,拖拽则有UP、DOWN、LEFT、RIGHT四个方向
最后,需要调用return makeMovementFlags(dragFlags, swipeFlags);将设置的标志位return回去

   @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
            final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
           //网格布局不处理滑动事件
            final int swipeFlags = 0;
        } else {
            final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            //列表布局不处理滑动事件
            final int swipeFlags = 0; 
        }
        return makeMovementFlags(dragFlags, swipeFlags);
    }

2.onMove

长按item的时候就会进入拖拽,并在拖拽过程中不断回调
viewHolder.getAdapterPosition();  得到拖动ViewHolder的position
target.getAdapterPosition();  得到目标ViewHolder的position

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
     //交换item位置
        Collection.swap(mData,viewHolder.getAdapterPosition()-1,target.getAdapterPosition()-1;
        recyclerAdapter.notifyItemMoved(fromPosition, toPosition);
        return true;
 }

3.onSwiped

移动item的时候就会进入滑动并在滑动过程中不断回调
viewHolder.getAdapterPosition();//得到拖动ViewHolder的position
target.getAdapterPosition();//得到目标ViewHolder的position

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    int position = viewHolder.getAdapterPosition()-1;
    //执行移除操作
    mData.remove(position);
    recyclerAdapter.notifyItemRemoved(position);
}

4.onChildDraw

滑动时会不断回调,返回X、Y的坐标

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        //滑动时改变Item的透明度
        final float alpha = 1 - Math.abs(dX) / (float)viewHolder.itemView.getWidth();
        viewHolder.itemView.setAlpha(alpha);
        viewHolder.itemView.setTranslationX(dX);
    }
}

5.onSelectedChanged

当选中Item时候会调用该方法

 @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
            //选中item开始拖拽时,改变颜色
            viewHolder.itemView.setBackgroundColor(Color.blue);
        
    }

6.clearView

item被放开或者动画完成的回调

 @Override
 public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
     super.clearView(recyclerView, viewHolder);
     //拖拽或滑动完成时,变回白色
     viewHolder.itemView.setBackgroundColor(0);
 }

7.isLongPressDragEnabled

是否支持拖拽操作,默认是支持返回true。如果想指定item可以拖拽,需要重写返回false,在item的长按监听事件里调用startDrag(ViewHolder)开启拖拽

 @Override
 public boolean isLongPressDragEnabled() {
   return true;
 }

8.isItemViewSwipeEnabled

是否支持滑动操作,默认是支持返回true。如果想指定item可以滑动,需要重写返回false,在item的点击事件里调用startSwipe(ViewHolder)开启滑动。

@Override
public boolean isItemViewSwipeEnabled() {
    return true;
}

9.实例

ItemTouchHelper.Callback mCallback2 = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP|ItemTouchHelper.DOWN,ItemTouchHelper.RIGHT) {
   
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
     //交换item位置 Collection.swap(mData,viewHolder.getAdapterPosition()-1,target.getAdapterPosition()-1;
recyclerAdapter.notifyItemMoved(fromPosition, toPosition); return true; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
   //删除此行
int position = viewHolder.getAdapterPosition()-1; mData.remove(position); recyclerAdapter.notifyItemRemoved(position); } @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { //滑动时改变Item的透明度 final float alpha = 1 - Math.abs(dX) / (float)viewHolder.itemView.getWidth(); viewHolder.itemView.setAlpha(alpha); viewHolder.itemView.setTranslationX(dX); } } @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { super.onSelectedChanged(viewHolder, actionState); } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); viewHolder.itemView.setBackgroundColor(0); } }; //添加ItemTouchHelper接口 ItemTouchHelper itemTouchHelper = new ItemTouchHelper(mCallback); //指定RecyclerView itemTouchHelper.attachToRecyclerView(recyclerView);

SnapHelper 页卡滑动

1.LinearSnapHelper 一次能滑动多页

LinearSnapHelper mLinearSnapHelper = new LinearSnapHelper();
mLinearSnapHelper.attachToRecyclerView(recyclerView);

2.PagerSnapHelper 一次只能滑动一页

PagerSnapHelper mPagerSnapHelper = new PagerSnapHelper();
mPagerSnapHelper.attachToRecyclerView(recyclerView);

 

android RecyclerView

全文结束