package com.lkh.multiadapter;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* 列表單項(xiàng)布局與數(shù)據(jù)綁定
* Created by luokanghui on 2017/5/24.
*/
public abstract class ItemViewAdapter{
/**
* 返回列表單項(xiàng)View,如果View由資源layout加載而來(lái),直接重寫{@link #onGetLayoutId()}即可
* @param parent 父view,一般為RecyclerView
* @return 列表單項(xiàng)View
*/
public View onCreateView(ViewGroup parent){
return LayoutInflater.from(parent.getContext()).inflate(onGetLayoutId()
, parent, false);
}
/**
* 當(dāng)RecyclerViewHolder創(chuàng)建成功后調(diào)用,只會(huì)調(diào)用一次
* @param viewHolder 單項(xiàng)view集合
*/
public void onCreate(RecyclerViewHolder viewHolder){
}
/**
* 返回單項(xiàng)布局的資源id,如果重寫了{(lán)@link #onCreateView(ViewGroup)},則此方法可能失效
* @return 單項(xiàng)布局layout id
*/
@LayoutRes
protected abstract int onGetLayoutId();
/**
* 把數(shù)據(jù)與view進(jìn)行綁定,滑動(dòng)時(shí)都會(huì)調(diào)用
* @param viewHolder 單項(xiàng)view集合
* @param data 具體數(shù)據(jù)
* @param position 在列表中的位置
*/
public abstract void bindData(RecyclerViewHolder viewHolder, E data, int position);
/**
* 局部更新時(shí)調(diào)用
* @param viewHolder 單項(xiàng)view集合
* @param data 具體數(shù)據(jù)
* @param position 在列表中的位置
* @param payloads 局部更新標(biāo)志,不會(huì)為空(isEmpty()==false)
*/
public void bindData(RecyclerViewHolder viewHolder, E data, int position, List
卡片1:
package com.lkh.multiadapter.sample;
import android.widget.TextView;
import com.lkh.multiadapter.ItemViewAdapter;
import com.lkh.multiadapter.R;
import com.lkh.multiadapter.RecyclerViewHolder;
/**
* 卡片1實(shí)現(xiàn)
* Created by luokanghui on 2019/3/18
*/
public class SampleOneItemViewAdapter extends ItemViewAdapter {
@Override
protected int onGetLayoutId() {
//布局layout資源id
return R.layout.item_one;
}
@Override
public void bindData(RecyclerViewHolder viewHolder, DataOne data, int position) {
//根據(jù)id獲取view
TextView tvContent = viewHolder.getView(R.id.tv_content);
//數(shù)據(jù)綁定
tvContent.setText(data.getContent());
}
}
package com.lkh.multiadapter.sample;
/**
* 卡片1數(shù)據(jù)
* Created by luokanghui on 2019/3/18
*/
public class DataOne {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
item_one.xml:
<?xml version="1.0" encoding="utf-8"?>
卡片2
package com.lkh.multiadapter.sample;
import android.widget.TextView;
import com.lkh.multiadapter.ItemViewAdapter;
import com.lkh.multiadapter.R;
import com.lkh.multiadapter.RecyclerViewHolder;
/**
* 卡片2實(shí)現(xiàn)
* Created by luokanghui on 2019/3/18
*/
public class SampleTwoItemViewAdapter extends ItemViewAdapter {
@Override
protected int onGetLayoutId() {
//布局layout資源id
return R.layout.item_two;
}
@Override
public void bindData(RecyclerViewHolder viewHolder, DataTwo data, int position) {
//根據(jù)id獲取view
TextView tvNum = viewHolder.getView(R.id.tv_num);
//數(shù)據(jù)綁定
tvNum.setText("num="+data.getNum());
}
}
package com.lkh.multiadapter.sample;
/**
* 卡片2數(shù)據(jù)
* Created by luokanghui on 2019/3/18
*/
public class DataTwo {
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
item_two.xml:
<?xml version="1.0" encoding="utf-8"?>
總Adapter,繼承MultiRecyclerViewAdapter
package com.lkh.multiadapter.sample;
import com.lkh.multiadapter.ItemViewAdapter;
import com.lkh.multiadapter.MultiRecyclerViewAdapter;
/**
* 多布局adapter,根據(jù)不同data及position,使用不同ItemViewAdapter卡片
* Created by luokanghui on 2019/3/18
*/
public class SampleMultiAdapter extends MultiRecyclerViewAdapter