RecycleView 万能设配器用法

BaseRecyclerViewAdapterHelper (万能设配器)

文档:https://www.jianshu.com/p/b343fcff51b0

githup:https://github.com/CymChad/BaseRecyclerViewAdapterHelper

一、导入

build.gradle:

implementation'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.49' (第三方库)

implementation'com.android.support:recyclerview-v7:28.0.0'

implementation'androidx.recyclerview:recyclerview:1.0.0'(AndroidX版本)

在app外的build.gradle加上 :maven { url"https://jitpack.io" },否则报错

image

二、使用

创建适配器,继承 BaseQuickAdapter <实体类, BaseViewHolder>

image

使用

image

MainActivity:

class MainActivityAdapter extends BaseQuickAdapter {


    public MainActivityAdapter(int layoutResId, @Nullable List data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, Test item) {
        helper.setText(R.id.txt, item.getText());
    }
}

public class MainActivity extends AppCompatActivity {
    private List testList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDate();
        initView();
    }

    private void initDate() {
        testList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Test test = new Test();
            test.setText("适配器");
            testList.add(test);
        }
    }

    private void initView() {
        RecyclerView recyclerView = findViewById(R.id.recycle);
        MainActivityAdapter mainActivityAdapter = new MainActivityAdapter(R.layout.main_item, testList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mainActivityAdapter);
        mainActivityAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "" + testList.get(position).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Test(实体类):


public class Test {

private StringText;

public String getText() {

return Text;

}

public void setText(String text) {

Text = text;

}

}

activity_main.xml:









你可能感兴趣的:(RecycleView 万能设配器用法)