重写getItemViewType方法
重写getItemViewType方法,根据条件返回条目的类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Override public int getItemViewType(int position) {
MoreTypeBean moreTypeBean = mData.get(position); if (moreTypeBean.type == 0) { return TYPE_PULL_IMAGE; } else if (moreTypeBean.type == 1) { return TYPE_RIGHT_IMAGE; } else { return TYPE_THREE_IMAGE; }
}
|
新建对应数目的ViewHolder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| private class PullImageHolder extends RecyclerView.ViewHolder {
public PullImageHolder(View itemView) { super(itemView); } }
private class RightImageHolder extends RecyclerView.ViewHolder {
public RightImageHolder(View itemView) { super(itemView); } }
private class ThreeImageHolder extends RecyclerView.ViewHolder {
public ThreeImageHolder(View itemView) { super(itemView); } }
|
在onCreateViewHolder里根据viewtype来判断 所引用的item布局类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view;
if (viewType == TYPE_PULL_IMAGE) { view =View.inflate(parent.getContext(),R.layout.item_pull_img,null); return new PullImageHolder(view); } else if (viewType == TYPE_RIGHT_IMAGE) { view =View.inflate(parent.getContext(),R.layout.item_right_img,null); return new RightImageHolder(view); } else { view =View.inflate(parent.getContext(),R.layout.item_three_img,null); return new ThreeImageHolder(view); } }
|