zoukankan      html  css  js  c++  java
  • recyclerview

    文章目录

    一、概述

    这篇文章记录:

    • recyclerView的基本用法
    • 为recyclerview添加点击事件
    • 添加上拉加载更多
    • 拖拽排序&滑动删除(初稿)

    二、recyclerview基本用法:

    • 写item和recyclerview布局
    • 写Adapter
    • 绑定数据、适配器、recyclerview

    1、基本操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    	
    mRecyclerView = findView(R.id.id_recyclerview);
    //2、设置布局管理器
    mRecyclerView.setLayoutManager(layout);
    //3、设置adapter
    mRecyclerView.setAdapter(adapter)
    //4、设置Item动画
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    //5、添加分割线
    mRecyclerView.addItemDecoration(new DividerItemDecoration( getActivity(), DividerItemDecoration.HORIZONTAL_LIST));
    //6、保持固定的大小,提高性能
    mRecyclerView.setHasFixedSize(true);

    2、适配器&ViewHolder

    • 继承RecyclerView.Adapter
    • 复写下面三个方法:
    1
    2
    3
    4
    5
    public RecyclerView.ViewHolder (ViewGroup viewGroup, int i){}

    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i){}

    public int getItemCount(){}
    • 写ViewHolder继承RecyclerView.ViewHolder

    3、Click和LongClick事件

    • 1、在Adapter中定义如下接口:
    1
    2
    3
    public static interface OnRecyclerViewItemClickListener {
    void onItemClick(View view , String data);
    }
    • 2、在onCreateViewHolder()中为每个item添加点击事件
    1
    2
    3
    4
    5
    6
    7
    8
    @Override
    public ViewHolder (ViewGroup viewGroup, int viewType) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
    ViewHolder vh = new ViewHolder(view);
    //将创建的View注册点击事件
    view.setOnClickListener(this);
    return vh;
    }
    • 3、将点击事件转移给外面的调用者:
      1
      2
      3
      4
      5
      6
      7
       @Override
      public void onClick(View v) {
      if (mOnItemClickListener != null) {
      //注意这里使用getTag方法获取数据
      mOnItemClickListener.onItemClick(v,(String)v.getTag());
      }
      }

    注意: onBindViewHolder()方法中设置和item相关的数据

    1
    2
    3
    4
    5
    6
    7
       
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
    viewHolder.mTextView.setText(datas[position]);
    //将数据保存在itemView的Tag中,以便点击时进行获取
    viewHolder.itemView.setTag(datas[position]);
    }
    • 4、最后暴露给外面的调用者,定义一个设置Listener的方法()
    1
    2
    3
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
    this.mOnItemClickListener = listener;
    }

    4、上拉加载更多

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    int lastVisibleItem = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
    int totalItemCount = mLayoutManager.getItemCount();
    //lastVisibleItem >= totalItemCount - 4 表示剩下4个item自动加载,各位自由选择
    // dy>0 表示向下滑动
    if (lastVisibleItem >= totalItemCount - 4 && dy > 0) {
    if(isLoadingMore){
    Log.d(TAG,"ignore manually update!");
    } else{
    loadPage();//这里多线程也要手动控制isLoadingMore
    isLoadingMore = false;
    }
    }
    }
    });

    5、ItemTouchHelper

    大专栏  recyclerviewlity_class_to_add_swipe_to_dismiss_and_drag__26amp_3B_drop_support_to_RecyclerView_28from_google_docs_29" class="headerlink" title="This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView(from google docs)">This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView(from google docs)

    • 类的大概声明

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      public class ItemTouchHelper extends RecyclerView.ItemDecoration
      implements RecyclerView.OnChildAttachStateChangeListener {


      //构造函数
      public ItemTouchHelper(Callback callback) {//传入一个Callback,两个内部类可用
      mCallback = callback;
      }
      //与recyclerview绑定
      public void attachToRecyclerView(RecyclerView recyclerView){}

      //some code here

      public abstract static class Callback {} //嵌套类
      public abstract static class SimpleCallback extends Callback{} //嵌套类
      }
    • SimpleCallback

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      /**
      *dragDirs- 表示拖拽的方向,有六个类型的值:LEFT、RIGHT、START、END、UP、DOWN
      *swipeDirs- 表示滑动的方向,有六个类型的值:LEFT、RIGHT、START、END、UP、DOWN
      */

      ItemTouchHelper.Callback mCallback = new ItemTouchHelper.SimpleCallback(int dragDirs, int swipeDirs) {
      /**
      * @param recyclerView
      * @param viewHolder 拖动的ViewHolder
      * @param target 目标位置的ViewHolder
      * @return
      */

      @Override
      public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
      //...
      return false;
      }
      /**
      * @param viewHolder 滑动的ViewHolder
      * @param direction 滑动的方向
      */

      @Override
      public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
      //...
      }
      };
    • Examples

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    //0则不执行拖动或者滑动
    ItemTouchHelper.Callback mCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP|ItemTouchHelper.DOWN,ItemTouchHelper.RIGHT) {

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    int fromPosition = viewHolder.getAdapterPosition();//得到拖动ViewHolder的position
    int toPosition = target.getAdapterPosition();//得到目标ViewHolder的position
    if (fromPosition < toPosition) {
    //分别把中间所有的item的位置重新交换
    for (int i = fromPosition; i < toPosition; i++) {
    Collections.swap(datas, i, i + 1);
    }
    } else {
    for (int i = fromPosition; i > toPosition; i--) {
    Collections.swap(datas, i, i - 1);
    }
    }
    mAdapter.notifyItemMoved(fromPosition, toPosition);
    //返回true表示执行拖动
    return true;
    }
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    int position = viewHolder.getAdapterPosition();
    datas.remove(position);
    mAdapter.notifyItemRemoved(position);
    }
    @Override//画每一个Item时回调
    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);
    }
    }
    };
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(mCallback);
    itemTouchHelper.attachToRecyclerView(mRecyclerView);

    参考链接:

    https://github.com/devunwired/recyclerview-playground

    https://github.com/AleBarreto/DragRecyclerView.git

  • 相关阅读:
    Nexus 3048的NX-OS升级方法
    ASA防火墙忘记密码之后的恢复步骤
    关闭AnyConnect登录安全警告窗口
    使用vASA842配置ASDM645
    SSL 证书格式普及,PEM、CER、JKS、PKCS12
    Python基础练习
    理解管理信息系统
    各大搜索引擎蜘蛛的UserAgent
    iOS应用程序内购/内付费
    iOS开发解决:iOS8.1中UIBarButtonItem的setTitleTextAttributes对Disabled颜色设置无效问题
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12284786.html
Copyright © 2011-2022 走看看