前言
由于项目需要,有个功能是后台动态配置布局页面,需要根据后台数据动态变化,所以考虑可以使用多布局。
效果图:
实现思路:
多布局原理是通过ListView或RecyclerView加载传入的不同类型的item来实现的,这里使用RecyclerView+BaseQuickAdapter的方法去实现。
BaseQuickAdapter
BaseQuickAdapter是一个强大的RecyclerAdapter框架,它能节约开发者大量的开发时间,集成了大部分列表常用需求解决方案。
框架引入
先在 build.gradle(Project:XXXX) 的 repositories 添加:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
然后在 build.gradle(Module:app) 的 dependencies 添加:
dependencies {
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
}
框架使用:
首先需要建一个用于解析type类型的Bean类,它需要实现框架中的MultiItemEntity接口,同时需要实现getItemType方法,这个方法是用来返回item类型的。
public class TestLayoutBean implements MultiItemEntity {
@Override
public int getItemType() {
return 0;
}
}
这个Bean类中需要定义一个字段来接收传入的itemType类型,同时定义代表item类型的几个常量:
//输入框
public static final int EDIT = 0;
//复选框
public static final int CHECK = 1;
//选择框
public static final int SELECT = 2;
//item类型
private int fieldType;
TestLayoutBean类:
public class TestLayoutBean implements MultiItemEntity {
//输入框
public static final int EDIT = 0;
//复选框
public static final int CHECK = 1;
//选择框
public static final int SELECT = 2;
//item类型
private int fieldType;
public TestLayoutBean(int fieldType) {
//将传入的type赋值
this.fieldType = fieldType;
}
@Override
public int getItemType() {
//返回传入的itemType
return fieldType;
}
}
Bean类写好之后开始写适配器,新建一个TestLayoutAdapter类继承自BaseMultiItemQuickAdapter<T,K>,实现它的convert()方法和构造方法。‘
TestLayoutAdapter类:
public class TestLayoutAdapter extends BaseMultiItemQuickAdapter<TestLayoutBean, BaseViewHolder> {
/**
* Same as QuickAdapter#QuickAdapter(Context,int) but with
* some initialization data.
*
* @param data A new list is created out of this one to avoid mutable list
*/
public TestLayoutAdapter(List<TestLayoutBean> data) {
super(data);
//设置当传入的itemType为某个常量显示不同的item
addItemType(TestLayoutBean.EDIT, R.layout.item_list_edit);
addItemType(TestLayoutBean.CHECK,R.layout.item_list_check);
addItemType(TestLayoutBean.SELECT,R.layout.item_list_select);
}
@Override
protected void convert(BaseViewHolder helper, TestLayoutBean item) {
switch (helper.getItemViewType()){
case TestLayoutBean.EDIT:
break;
case TestLayoutBean.CHECK:
break;
case TestLayoutBean.SELECT:
break;
}
}
}
在Activity的布局文件中写入一个RecyclerView
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
然后再Activity中实例化一个List
private List<TestLayoutBean> beanList = new ArrayList<>();
传入数据(itemType)给List:
beanList.add(new TestLayoutBean(TestLayoutBean.CHECK));
beanList.add(new TestLayoutBean(TestLayoutBean.EDIT));
beanList.add(new TestLayoutBean(TestLayoutBean.SELECT));
beanList.add(new TestLayoutBean(TestLayoutBean.EDIT));
beanList.add(new TestLayoutBean(TestLayoutBean.CHECK));
onCreate方法中初始化RecyclerView,写一个initAdapter()方法初始化适配器
TestLayoutAdapter adapter = new TestLayoutAdapter(beanList);
设置布局管理器:
recyclerList.setLayoutManager(new LinearLayoutManager(this));
设置适配器:
recyclerList.setAdapter(adapter);
adapter.notifyDataSetChanged();
MainActivity类:
public class MainActivity extends AppCompatActivity {
private List<TestLayoutBean> beanList = new ArrayList<>();
private RecyclerView recyclerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerList = findViewById(R.id.recycler_list);
beanList.add(new TestLayoutBean(TestLayoutBean.CHECK));
beanList.add(new TestLayoutBean(TestLayoutBean.EDIT));
beanList.add(new TestLayoutBean(TestLayoutBean.SELECT));
beanList.add(new TestLayoutBean(TestLayoutBean.EDIT));
beanList.add(new TestLayoutBean(TestLayoutBean.CHECK));
initAdapter();
}
private void initAdapter(){
TestLayoutAdapter adapter = new TestLayoutAdapter(beanList);
recyclerList.setLayoutManager(new LinearLayoutManager(this));
recyclerList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
要获取item中控件可以通过适配器中的convert()方法获取不同类型的ViewHolder对象进行对控件的操作。