开源库BaseRecyclerViewAdapterHelper的简单用法

前言

BaseRecyclerViewAdapterHelper 是用于对RecycleView的数据绑定以及定义事件的一系列超级简单的一个adapter,下面我们就来学习他的用法

用法(介绍两种)

配置环境

标准配置

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

 dependencies {
            compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:VERSION_CODE'
    }

可能由于我的网络环境有问题,这种配置方式我的gradle并不能成功,于是就采用了下载源码的方式,具体方法:

  • 先从github上下载源码:https://github.com/CymChad/BaseRecyclerViewAdapterHelper
  • 找到源码内的library,copy到你的工程中,之后修改library中的gradle配置和app里的一样,并在项目dependencies添加刚刚copy的library,这样我们就可以使用了,这里就不贴图了,因为比较简单。
使用方法
  • 先创建一个实体类用于给item添加数据
public class Message {
    private String title;         //这里声明两个text,我们的item里面也只有两个TextView
    private String message;

    public Message(String title, String message) {
        this.title = title;
        this.message = message;
    }

    public Message(){

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
  • 配置Adapter,我们创建一个QucikAdapter
public class QuickAdapter extends BaseQuickAdapter<Message,BaseViewHolder>{

    //这个是构造,用来初始化要输入的数据
    public QuickAdapter(List<Message> data) {
        super(R.layout.item, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Message item) {
           //将每一个item的数据绑定到对应的TextView上
                helper.setText(R.id.title,item.getTitle())
                      .setText(R.id.message,item.getMessage());

    }
}

我们用CardView来作为item

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="50dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"
    android:layout_marginTop="@dimen/dp_10"
    android:elevation="10dp"
    app:cardCornerRadius="10dp">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
   <TextView
       android:id="@+id/title"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

   <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
</android.support.v7.widget.CardView>

activity_main.xml的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal"
    tools:context="com.example.rcycleadapterdemo.activity.MainActivity">

   <android.support.v7.widget.RecyclerView

       android:id="@+id/recycleview"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">


   </android.support.v7.widget.RecyclerView>

</LinearLayout>

MainAcitivy的内容

public class MainActivity extends AppCompatActivity {
    private List<Message> list = new ArrayList<>();
    private QuickAdapter quickAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<10;i++){
            list.add(new Message("haha","heheheheheheh"));
        }
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycleview);
        GridLayoutManager manager = new GridLayoutManager(this,1);
        recyclerView.setLayoutManager(manager);
        quickAdapter = new QuickAdapter(list);
        recyclerView.setAdapter(quickAdapter);
       // quickAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN); //指定缩放动画
        //自定义动画
        quickAdapter.openLoadAnimation(new BaseAnimation() {
            @Override
            public Animator[] getAnimators(View view) {
                return new Animator[]{
                        ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),
                        ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
                };
            }
        });
       //添加点击事件
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
      public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

          Toast.makeText(MainActivity.this,"点击了"+position,Toast.LENGTH_SHORT).show();

            }
        });

    }
}

可以看出来这个用法还是很简单的,支持各种自定义内容,甚至可以给item里的控件添加点击事件,这个就不演示了。附上截图
这里写图片描述
这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值