非常简单的一个列表管理器BaseQuickAdapter(可以添加头布局尾布局)

官方网站:www.recyclerview.org

推荐文章地址:url

1)框架引入:

首先在project的build.gradle中配置  maven { url "https://jitpack.io" 

allprojects {
    repositories {
        ****
        // 配置basequickadapter
        maven { url "https://jitpack.io" }
    }
}

然后在moudle中的build.gradle中添加依赖  api 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'(去官网查看最新版本)

dependencies {
    ****
    ***
    api 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
}

2)使用:

直接看代码吧,我的所有逻辑都写在 activity 中,只是为了放便测试和查看,如果项目中用的话可以自己自定义分包。

2.1)activity

package com.example.myapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    private RecyclerView recycle_view;
    ArrayList arrayList ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取数据
        initDatas();
        recycle_view = findViewById(R.id.recycle_view);
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        recycle_view.setLayoutManager(manager);
        MyAdapter adapter = new MyAdapter(R.layout.item_recycle,arrayList);
        recycle_view.setAdapter(adapter);
        //获取头布局尾布局的View
        View headView = getLayoutInflater().inflate(R.layout.item_head, null);
        View footView = getLayoutInflater().inflate(R.layout.item_foot, null);
        //添加头布局尾布局
        adapter.addHeaderView(headView);
        adapter.addFooterView(footView);
        //列表条目的点击事件
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "点击了第"+position+"条目", Toast.LENGTH_SHORT).show();

            }
        });
    }
    //假数据
    private void initDatas() {
        arrayList = new ArrayList();
        for (int i = 0; i < 25; i++) {
            Datas datas = new Datas();
            datas.setName("我是"+i);
            datas.setAge(i);
            arrayList.add(datas);
        }
    }

    //列表的adapter适配器
    public class MyAdapter extends BaseQuickAdapter {
        public MyAdapter(int layoutResId, @Nullable List data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Datas item) {
            helper.setText(R.id.tv_names,item.getName()).setText(R.id.tv_ages,item.getAge()+"");
        }
    }
}

2.2) bean 类

package com.example.myapplication;

/**
 * Created by pw on 2019/2/22 14:23
 * E-Mail Address: pw163.com
 */
class Datas {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

布局就不添加了,很简单因为项目需求不同,样式也不一样,灵活安排。

欢迎来扰哦。

你可能感兴趣的:(知识总结)