//万能适配器
//第一步Project下 配置maven { url "https://jitpack.io" }
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
//第二步 导依赖
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
//第三步 我的Activity页面:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mRecy = (RecyclerView) findViewById(R.id.mRecy);
ArrayList<Bean> list = new ArrayList<>();
list.add(new Bean("我的" , "16个" ));
list.add(new Bean("全部客户16个" , "更新时间2010/01/01"));
list.add(new Bean("龙图腾" , "最近联系" ));
list.add(new Bean("测试人" , "最近联系"));
list.add(new Bean("欧阳震华" , "最近联系" ));
list.add(new Bean("刘女士" , "最近联系" ));
list.add(new Bean("张先生" , "最近联系" ));
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecy.setLayoutManager(manager);
//这里是你要适配的数据,多布局只需要一个参数就行了
Apader apader = new Apader(R.layout.layout_1,list);
apader.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);//自带的动画
mRecy.setAdapter(apader);
apader.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();
}
});
}
}
//第四步 适配器,继承BaseQuickAdapter,第一个参数放的是你要解析的Bean类,第二个固定就是BaseViewHolder就行
public class Apader extends BaseQuickAdapter<Bean,BaseViewHolder> {
public Apader(int layoutResId, @Nullable List<Bean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, Bean item) {
// helper.getView(R.id.mName1)
helper.setText(R.id.mName1,item.getName());
helper.setText(R.id.mTitle1,item.getTitle());
}
}
//第五步 我的布局:
<?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"
android:orientation="vertical"
tools:context="com.tq.crm.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/mRecy"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
//第六步 Bean类:
package com.tq.crm;
import com.chad.library.adapter.base.entity.MultiItemEntity;
public class Bean {
private String name;
private String title;
public Bean() {
}
public Bean(String name, String title) {
this.name = name;
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
//第七步 item 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:gravity="center"
android:background="@drawable/crm_category_1"
android:id="@+id/mName1"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:gravity="center"
android:id="@+id/mTitle1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toEndOf="@+id/mName1"
android:layout_toRightOf="@+id/mName1"
android:text="aaa" />
<ImageView
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="@drawable/right_mark"
android:layout_width="30dp"
android:layout_height="30dp" />
</RelativeLayout>
//-----------------------------------多布局 ------------------------------
//万能适配器 多布局:
//第一步 RecycleView和万能适配器依赖:
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
implementation 'com.android.support:recyclerview-v7:26.1.0'
//第二步 我的Activity页面,Bean类构造方法里设置Type值,来适配不同的布局:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mRecy = (RecyclerView) findViewById(R.id.mRecy);
ArrayList<Bean> list = new ArrayList<>();
list.add(new Bean("我的" , "16个" , 1));
list.add(new Bean("全部客户16个" , "更新时间2010/01/01" , 2));
list.add(new Bean("龙图腾" , "最近联系" , 3));
list.add(new Bean("测试人" , "最近联系" , 3));
list.add(new Bean("欧阳震华" , "最近联系" , 3));
list.add(new Bean("刘女士" , "最近联系" , 3));
list.add(new Bean("张先生" , "最近联系" , 3));
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecy.setLayoutManager(manager);
//这里是你要适配的数据,多布局只需要一个参数就行了
Apader apader = new Apader(list);
apader.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);//自带的动画
mRecy.setAdapter(apader);
apader.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
if (view.getId()==R.id.mWo){
Toast.makeText(MainActivity.this, "我的客户", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();
}
}
});
}
}
//布局:
<?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"
android:orientation="vertical"
tools:context="com.tq.crm.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/mRecy"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
//第三步 我的Bean 类 需要实现MultiItemEntity,重写方法,返回itemType,一个int值。
public class Bean implements MultiItemEntity {
private String name;
private String title;
private int itemType;
public Bean() {
}
public Bean(String name, String title, int itemType) {
this.name = name;
this.title = title;
this.itemType = itemType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public int getItemType() {
return itemType;
}
}
//第四步 我的适配器:
public class Apader extends BaseMultiItemQuickAdapter<Bean,BaseViewHolder> {
public Apader(List<Bean> data) {
super(data);
addItemType(1,R.layout.layout_1);
addItemType(2,R.layout.layout_2);
addItemType(3,R.layout.layout_3);
}
@Override
protected void convert(BaseViewHolder helper, Bean item) {
switch (helper.getItemViewType()){
case 1:
helper.setText(R.id.mName1,item.getName());
helper.setText(R.id.mTitle1,item.getTitle());
break;
case 2:
helper.setText(R.id.mName2,item.getName());
helper.setText(R.id.mTitle2,item.getTitle());
break;
case 3:
helper.setText(R.id.mName3,item.getName());
helper.setText(R.id.mTitle3,item.getTitle());
break;
}
}
}
//第五步 三个布局页面:
//layout_1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/mWo"
>
<TextView
android:gravity="center"
android:background="@drawable/crm_category_1"
android:id="@+id/mName1"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:gravity="center"
android:id="@+id/mTitle1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toEndOf="@+id/mName1"
android:layout_toRightOf="@+id/mName1"
android:text="aaa" />
<ImageView
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="@drawable/right_mark"
android:layout_width="30dp"
android:layout_height="30dp" />
</RelativeLayout>
//layout_2:
<?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="wrap_content"
android:orientation="horizontal"
android:background="#bcb9b9"
>
<TextView
android:id="@+id/mName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/mTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
//layout_3:
<?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="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
>
<TextView
android:textStyle="bold"
android:text="qq"
android:textSize="18sp"
android:id="@+id/mName3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="22"
android:id="@+id/mTitle3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:background="#878484"
android:layout_width="match_parent"
android:layout_height="1dp"></View>
</LinearLayout>
//第二种 多布局方式,通过setItemType()设置Type值来适配不同的布局,只有Activity页面和Bean类页面有改变,其他的都一样:
//我的Activity布局:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mRecy = (RecyclerView) findViewById(R.id.mRecy);
//setItemType()设置不同的type值,适配不同的布局
ArrayList<Bean> list = new ArrayList<>();
Bean bean1 = new Bean();
bean1.setItemType(1);
for (int i = 0; i < 5; i++) {
bean1.setName("我的" + i);
bean1.setTitle("16个" + i);
list.add(bean1);
}
Bean bean2 = new Bean();
bean2.setItemType(2);
for (int i = 0; i < 5; i++) {
bean2.setName("全部客户" + i);
bean2.setTitle("更新时间2010/0/26" + i);
list.add(bean2);
}
Bean bean3 = new Bean();
bean3.setItemType(3);
for (int i = 0; i < 5; i++) {
bean3.setName("龙图腾" + i);
bean3.setTitle("最近联系" + i);
list.add(bean3);
}
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecy.setLayoutManager(manager);
//这里是你要适配的数据,多布局只需要一个参数就行了
Apader apader = new Apader(list);
apader.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);//自带的动画
mRecy.setAdapter(apader);
apader.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
if (view.getId() == R.id.mWo) {
Toast.makeText(MainActivity.this, "我的客户", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
}
});
}
}
//我的Bean类:
public class Bean implements MultiItemEntity {
private String name;
private String title;
private int itemType;
public Bean() {
}
public Bean( String name, String title) {
this.name = name;
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public int getItemType() {
return itemType;
}
public void setItemType(int itemType) {
this.itemType = itemType;
}
}
//---------------------------------------------------------------------------完---------------------------------------------------------------------------------
//简洁代码:
实体类要继承 MultiItemEntity
public class News implements MultiItemEntity {
public static final int TEXT = 1;//文本布局
public static final int IMAGE = 2;//图片布局
public int itemType;
public News(){
}
public News(int itemType){
this.itemType = itemType;
}
@Override
public int getItemType() {
return itemType;
}
}
//Adapter继承BaseMultiItemQuickAdapter
public class MuitlAdapter extends BaseMultiItemQuickAdapter<News,BaseViewHolder> {
public MuitlAdapter(List<News> data) {
super(data);
addItemType(News.TEXT, R.layout.text_view);//文本类型对应文本布局
addItemType(News.IMAGE,R.layout.image_layout);//图片类型对应图片布局
}
@Override
protected void convert(BaseViewHolder helper, News item) {
switch (helper.getItemViewType()){
case News.TEXT:
break;
case News.IMAGE:
break;
}
}
}
//Activity中的主要代码:
List<News> newsList = new ArrayList<>();
newsList.add(new News(News.TEXT));
newsList.add(new News(News.IMAGE));
newsList.add(new News(News.TEXT));
newsList.add(new News(News.TEXT));
newsList.add(new News(News.TEXT));
newsList.add(new News(News.TEXT));
MuitlAdapter muitlAdapter = new MuitlAdapter(newsList);
mRecycleView.setAdapter(muitlAdapter);
//---------
//----------------------------------------------------------------------完----------------------------------------------------------------------------