recyclerview多條目佈局
阿新 • • 發佈:2018-12-22
public class MyrecyclerAdater extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context mcontext; private List<Person.ResultBean.DataBean> list; private static final int type1 = 0; private static final int type2 = 1; private static final int type3 = 2; public MyrecyclerAdater(Context mcontext, List<Person.ResultBean.DataBean> list) { this.mcontext = mcontext; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) { if (type == type1) { View view1 = LayoutInflater.from(mcontext).inflate(R.layout.list_view3, parent, false); return new ThreeViewholder(view1); } else if (type == type2) { View view2 = LayoutInflater.from(mcontext).inflate(R.layout.list_view2, parent, false); return new TwoViewholder(view2); } else { View view3 = LayoutInflater.from(mcontext).inflate(R.layout.list_view1, parent, false); return new FristViewholder(view3); } } //介面回撥(長按) private OnItemClickListener onItemClickListener; public interface OnItemClickListener { void onItemLongClick(View view, int position); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { int type = getItemViewType(position); switch (type) { case type1: ThreeViewholder threeViewholder = (ThreeViewholder) holder; threeViewholder.tv_text3.setText(list.get(position).getTitle()); Picasso.with(mcontext).load(list.get(position).getThumbnail_pic_s()).into(threeViewholder.tv_image2); Picasso.with(mcontext).load(list.get(position).getThumbnail_pic_s02()).into(threeViewholder.tv_image3); Picasso.with(mcontext).load(list.get(position).getThumbnail_pic_s03()).into(threeViewholder.tv_image4); break; case type2: TwoViewholder twoViewholder = (TwoViewholder) holder; twoViewholder.tv_text2.setText(list.get(position).getTitle()); Picasso.with(mcontext).load(list.get(position).getThumbnail_pic_s03()).into(twoViewholder.tv_image1); break; case type3: FristViewholder fristViewholder = (FristViewholder) holder; fristViewholder.tv_text1.setText(list.get(position).getTitle()); break; } if (onItemClickListener != null) { holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int pos = holder.getLayoutPosition(); onItemClickListener.onItemLongClick(holder.itemView, pos); return true; } }); } } //刪除條目 public void removeItem(int position) { if (position > list.size() - 1) { return; } list.remove(position); notifyItemRemoved(position); } @Override public int getItemViewType(int position) { if (position % 3 == 1) { return type1; } else if (position % 3 == 2) { return type2; } else { return type3; } } @Override public int getItemCount() { return list.size(); } class FristViewholder extends RecyclerView.ViewHolder { private final TextView tv_text1; public FristViewholder(View itemView) { super(itemView); tv_text1 = itemView.findViewById(R.id.tv_text1); } } class TwoViewholder extends RecyclerView.ViewHolder { private final TextView tv_text2; private final ImageView tv_image1; public TwoViewholder(View itemView) { super(itemView); tv_text2 = itemView.findViewById(R.id.tv_text2); tv_image1 = itemView.findViewById(R.id.tv_image1); tv_image1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv_image1, "alpha", 1f, 0f, 1f); objectAnimator.setDuration(2000); objectAnimator.start(); } }); } } class ThreeViewholder extends RecyclerView.ViewHolder { private final TextView tv_text3; private final ImageView tv_image2; private final ImageView tv_image3; private final ImageView tv_image4; public ThreeViewholder(View itemView) { super(itemView); tv_text3 = itemView.findViewById(R.id.tv_text3); tv_image2 = itemView.findViewById(R.id.tv_image2); tv_image3 = itemView.findViewById(R.id.tv_image3); tv_image4 = itemView.findViewById(R.id.tv_image4); tv_image2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv_image2, "alpha", 1f, 0f, 1f); objectAnimator.setDuration(2000); objectAnimator.start(); } }); tv_image3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv_image3, "alpha", 1f, 0f, 1f); objectAnimator.setDuration(2000); objectAnimator.start(); } }); tv_image4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv_image4, "alpha", 1f, 0f, 1f); objectAnimator.setDuration(2000); objectAnimator.start(); } }); } } }