1 Star 0 Fork 0

raoqian156 / 万能适配器

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

RecyclerView 万能适配器

<一>导入依赖

项目 gradle.properties

android.useAndroidX=true
android.enableJetifier=true

项目根目录 build.gradle

    allprojects {
		repositories {
			...
			maven { url 'https://www.jitpack.io' }
		}
	}

项目 build.gradle

implementation 'com.gitee.raoqian156:RV-Adapter:v1.1.0'

混淆

-keep public class * extends com.rq.rvlibrary.BaseViewHolder { *;}

<二> 使用细则

初始化

1.最基础使用方式

    mAdapter = new BaseAdapter(this, R.layout.item_example) {
      @Override
      protected void onBindEasyHolder(BaseAdapter adapter, BaseViewHolder holder, int position, Object o) {
        holder.setItemText(R.id.txt, (String) o);
      }
    };
    new RecyclerUtil(mAdapter).set2View(recyclerView);
    List<String> debug = new ArrayList<>();
    debug.addAll(getDebugData());
    mAdapter.setData(debug);

2.复写ViewHolder引用

    mAdapter = new BaseAdapter(this, R.layout.item_example,ExampleViewHolder1.class);
//    当 ExampleViewHolder1 为内部类时使用下个方法
//    mAdapter = new BaseAdapter(this, R.layout.item_example,ExampleViewHolder1.class,this);
    new RecyclerUtil(mAdapter).set2View(recyclerView);
    List<String> debug = new ArrayList<>();
    debug.addAll(getDebugData());
    mAdapter.setData(debug);



    public class ExampleViewHolder1 extends BaseViewHolder<String> {

      public ExampleViewHolder1(View itemView) {
        super(itemView);
      }

      @Override
      public int inflateLayoutId() {
        return R.layout.item_example;
      }

      @Override
      public void fillData(BaseAdapter adapter, int position, String o) {
        setItemText(R.id.txt, o);
      }
    }

3.追加型布局

该布局模式下可接受不同数据列表 如下代码,新建 SparseArray 设置数据展示规则(布局文件为key),重写 getMultipleHolderType 给出布局文件,同样的修改 onBindMultipleHolder 优先级低于 ViewHolder#fillData

    SparseArray layList = new SparseArray();
    layList.append(R.layout.item_example3, EasyViewHolder.class);
    layList.append(R.layout.item_example, ExampleViewHolder1.class);
    layList.append(R.layout.item_example2, ExampleViewHolder2.class);
    mAdapter = new BaseAdapter(this, layList) {
         @Override
         protected int getMultipleHolderType(Object dataItem, int position) {
           if (dataItem instanceof Integer) {
             return R.layout.item_example3;
           }
           String data = (String) dataItem;
           if (position < 4 && position >= 0) return R.layout.item_example3;
           if (data.contains("1")) return R.layout.item_example2;
           return R.layout.item_example;
         }

         @Override
         protected void onBindMultipleHolder(BaseAdapter adapter, BaseViewHolder holder, int position,
                                             Object o) {
           LOG.e("MainActivity", holder.getClass()
                                       .getSimpleName() + ".onBindMultipleHolder.63:" + position);
           if (holder instanceof ExampleViewHolder2) {
             if (o instanceof Integer) {
               holder.setItemText(R.id.txt, "Holder2 数字:" + o);
             } else {
               holder.setItemText(R.id.txt, "Holder2:" + o);
             }
           } else if (position >= 0) if (o instanceof Integer) {
             holder.setItemText(R.id.txt, "数字:" + o);
           } else {
             holder.setItemText(R.id.txt, (String) o);
           }
         }
       };
    new RecyclerUtil(mAdapter).set2View(recyclerView);
    List debug = new ArrayList<>();
    debug.add("1  $$$$");
    debug.add("2  $$$$");
    debug.add("3   3");
    debug.add("4   24");
    debug.add("5   51");
    debug.add("13  3331678901");
    debug.add("14  2678902");
    debug.add("15  555555536789033");
    debug.add("16  66664678904");
    debug.add("17---***");
    debug.add(18);
    debug.add(19);
    debug.add("20***");
    debug.add(21);
    debug.add(22);
    debug.add("23333333333");
    debug.add("244444");
    mAdapter.setData(debug);

调用

显示/隐藏 数据

BaseAdapter.setDisplay() 设置数据本地展示条件,BaseAdapter.display(Object rule)根据规则 内容展示数据,null-清除展示条件,全部展示

   mAdapter.setDisplay(new BaseAdapter.DisplayOption<String>() {
        @Override
        public boolean show(String data,@NonNull Object rule, int position) {
            //自定义显示条件 true->显示 false->隐藏
            if (rule instanceof String) {
                return data.contains((String) rule);
            }
            return true;
        }
   });

   //调用
   Object rule = null;//rule = "2";展示包含数据"2"的条目
   mAdapter.display(rule);//展示全部数据

1.添加点击事件

<1>BaseAdapter 实体类 调用 addOnItemClickListener ,具体使用方法见Java文件
<2>继承 ViewHolder 实现 OnInterceptClick 接口,具体使用方法见Java文件;此方法优先级高于方法<1>,intercept() 返回 true 则不会回调 方法<1> 接口.

2.RecyclerUtil - 修改显示方式

RecyclerUtil 中封装了大量的显示方法,该工具的创建 需要传入创建好的 BaseAdapter
1.RecyclerUtil.set2View() 此方法为绑定视图作用,将传入的Adapter 与 RecyclerView 绑定
2.RecyclerUtil.row() 此方法修改显示列数 默认为 1 为线性布局
3.RecyclerUtil.orientation() 此方法修改显示布局方向 默认为垂直方向
4.若以上方法不能满足使用,可使用 RecyclerUtil.setLayoutManager() 传入自定义LayoutManager 进行布局控制

其他说明

BaseViewHolder 的继承使用时,必须实现inflateLayoutId方法,该方法的实现主要是为了后期维护的方便

HeadViewHolder代码

public class HeadViewHolder extends BaseViewHolder implements OnInterceptClick {

  @Override
  public boolean intercept(Object object, View v, int position) {
    if (v.getId() == R.id.txt_left) {
      Toast.makeText(v.getContext(), "LEFT", Toast.LENGTH_LONG).show();
    } else if (v.getId() == R.id.txt_right) {
      Toast.makeText(v.getContext(), "RIGHT", Toast.LENGTH_LONG).show();
    }
    return false;
  }

  @Override
  public int[] clickIds() {
    return new int[]{R.id.txt_left, R.id.txt_right};
  }

  public static class DebugData {
    public DebugData(boolean isLeft, String realContent) {
      this.isLeft = isLeft;
      this.realContent = realContent;
    }

    boolean isLeft = false;
    String realContent;
  }

  public HeadViewHolder(View itemView) {
    super(itemView);

  }

  @Override
  public int inflateLayoutId() {
    return R.layout.item_head_view;
  }

  @Override
  public void fillData(BaseAdapter adapter, int position, Object data) {
    super.fillData(adapter, position, data);
    if (data instanceof DebugData) {
      DebugData bean = (DebugData) data;
      if (bean.isLeft) {
        setItemText(R.id.txt_left, bean.realContent);
      } else {
        setItemText(R.id.txt_right, bean.realContent);
      }
    }
  }

}

Comments ( 0 )

Sign in for post a comment

About

一款用于RecyclerView的通用适配器 spread retract
Cancel

万能适配器

Contributors

All

Activities

load more
can not load any more
1
https://gitee.com/raoqian156/RV-Adapter.git
git@gitee.com:raoqian156/RV-Adapter.git
raoqian156
RV-Adapter
万能适配器
master

Search