how to make my regular layout click normally to respond to item. Now the first three item clicks are normal, and clicking the fourth item responds to a red multi-layout. Clicking "I am the third title" should show that the position is 3. Here are three classes
public class ManyLayoutActivity extends AppCompatActivity implements BaseQuickAdapter.OnItemClickListener {
private RecyclerView recyclerView;
private List<Model> datas;
private MultiDelegateAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_many_layout);
//RecyclerView
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//
datas = new ArrayList<>();
//
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
//
adapter = new MultiDelegateAdapter(datas);
Model model;
for (int i = 0; i < 5; iPP) {
model = new Model();
model.setTitle(" : " + i + "");
model.setContent(" : " + i + "");
if (i == 3) {
Model model2 = new Model();
model2.setType(Model.FIRST_TYPE);
datas.add(model2);
}
model.setType(Model.NORMAL_TYPE);
datas.add(model);
}
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(this);
}
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(this, "" + position, Toast.LENGTH_SHORT).show();
}
}
public class MultiDelegateAdapter extends BaseQuickAdapter<Model, BaseViewHolder> {
public MultiDelegateAdapter(@Nullable List<Model> data) {
super(data);
setMultiTypeDelegate(new MultiTypeDelegate<Model>() {
@Override
protected int getItemType(Model entity) {
//
return entity.type;
}
});
getMultiTypeDelegate()
.registerItemType(Model.FIRST_TYPE, R.layout.first_type_layout)
.registerItemType(Model.SECOND_TYPE, R.layout.second_type_layout)
.registerItemType(Model.NORMAL_TYPE, R.layout.item_many_layout);
}
@Override
protected void convert(BaseViewHolder helper, Model item) {
switch (helper.getItemViewType()) {
case Model.FIRST_TYPE:
break;
case Model.SECOND_TYPE:
break;
case Model.NORMAL_TYPE:
helper.setImageResource(R.id.iv_img, R.mipmap.ic_launcher)
.setText(R.id.tv_title, item.getTitle())
.setText(R.id.tv_content, item.getContent());
break;
}
}
}
public class Model {
public static final int FIRST_TYPE = 1;
public static final int SECOND_TYPE = 2;
public static final int NORMAL_TYPE = 3;
//
public int type;
private String title;
private String content;
private String imgUrl;
public static int getFirstType() {
return FIRST_TYPE;
}
public static int getSecondType() {
return SECOND_TYPE;
}
public static int getNormalType() {
return NORMAL_TYPE;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
}