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" },否则报错
二、使用
创建适配器,继承 BaseQuickAdapter <实体类, BaseViewHolder>
使用
MainActivity:
class MainActivityAdapter extends BaseQuickAdapter<Test, BaseViewHolder> {
public MainActivityAdapter(int layoutResId, @Nullable List<Test> 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<Test> 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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>