效果
圖片丑就丑吧,看效果就好了
使用
jitpack對(duì)開(kāi)源分享真是友好啊
Step 1.Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.PMMKing:MultiAdapter:1.1'
}
直接上代碼,簡(jiǎn)單粗暴
RecyclerView rlvTest = findViewById(R.id.rlv_test);
rlvTest.setLayoutManager(new LinearLayoutManager(this));
MultiAdapter<Integer> adapter = new MultiAdapter<Integer>(this).addTypeView(new ITypeView<Integer>() {
@Override
public boolean isForViewType(Integer item, int position) {
return position % 2 == 0;
}
@Override
public BaseViewHolder createViewHolder(Context mContext, ViewGroup parent) {
return new ViewHolderOne(mContext, LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false));
}
}).addTypeView(new ITypeView<Integer>() {
@Override
public boolean isForViewType(Integer item, int position) {
return position % 2 != 0;
}
@Override
public BaseViewHolder createViewHolder(Context mContext, ViewGroup parent) {
return new ViewHolderTwo(mContext, LayoutInflater.from(mContext).inflate(R.layout.item2_layout, parent, false));
}
});
rlvTest.setAdapter(adapter);
adapter.setOnItemClickListener(new OnItemClickListener<Integer>() {
@Override
public void onItemClickListener(View view, Integer data, int position) {
Toast.makeText(MainActivity.this, "onClick position is " + position + " data is " + data, Toast.LENGTH_SHORT).show();
}
});
adapter.setOnLongItemClickListener(new OnItemLongClickListener<Integer>() {
@Override
public boolean onItemLongClickListener(View view, Integer data, int position) {
Toast.makeText(MainActivity.this, "onLongClick position is " + position + " data is " + data, Toast.LENGTH_SHORT).show();
return true;
}
});
ArrayList<Integer> list = new ArrayList<>();
Random rand = new Random();
while (true) {
list.add(rand.nextInt());
if (list.size() > 30) {
break;
}
}
adapter.setData(list);
添加點(diǎn)擊和長(zhǎng)按事件德迹,每多一個(gè)布局在adapter后面添加一個(gè)addTypeView() 方法芽卿,方法里面實(shí)現(xiàn)一個(gè)ITypeView的實(shí)例,復(fù)寫(xiě)ITypeView的兩個(gè)方法胳搞,第一個(gè)判斷當(dāng)前item要顯示那個(gè)布局文件卸例,第二個(gè)返回一個(gè)ViewHolder称杨。
ViewHolder繼承BaseViewHolder
public class ViewHolderOne extends BaseViewHolder<Integer> {
private final TextView tvItem;
public ViewHolderOne(Context context, View itemView) {
super(context, itemView);
tvItem = itemView.findViewById(R.id.tv_item);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, Integer data, int position) {
tvItem.setText("布局1的item的position是:" + position + " data是:" + data);
}
}
不想講代碼,感興趣的自己看吧筷转,這個(gè)庫(kù)自己已經(jīng)用了一段時(shí)間了姑原,上拉加載,下拉刷新來(lái)了呜舒,
github