如何设置列表项异构的recyclerview的onclick侦听器
how to set on click listener of recyclerview where list items are heterogenous
我有一个带有 2 种类型的查看器的 recyclerview,用于显示 2 种类型的项目
我想在项目中的子项目上实现点击监听器
现有解决方案显示 viewholder class 应该在适配器内部实现为静态,但我已经在单独的文件中声明了它们(建议使用教程),
所以我不能在 viewholders 上实现 onlick 监听器
我的 viewholder1 class 是
public class ViewHolder1 extends RecyclerView.ViewHolder {
private TextView label1, label2;
public ImageView postImage;
public ViewHolder1(View v) {
super(v);
label1 = (TextView) v.findViewById(R.id.text11);
label2 = (TextView) v.findViewById(R.id.text22);
postImage = (ImageView) v.findViewById(R.id.dummyImage);
}
public TextView getLabel1() {
return label1;
}
public void setLabel1(TextView label1) {
this.label1 = label1;
}
public TextView getLabel2() {
return label2;
}
public void setLabel2(TextView label2) {
this.label2 = label2;
}
}
viewholder2 是
public class ViewHolder2 extends RecyclerView.ViewHolder {
private ImageView ivExample;
public ViewHolder2(View v) {
super(v);
ivExample = (ImageView) v.findViewById(R.id.ivExample);
}
public ImageView getImageView() {
return ivExample;
}
public void setImageView(ImageView ivExample) {
this.ivExample = ivExample;
}
}
适配器是
public class ComplexRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// The items to display in your RecyclerView
private List<Object> items;
private final int USER = 0, IMAGE = 1;
// Provide a suitable constructor (depends on the kind of dataset)
public ComplexRecyclerViewAdapter(List<Object> items) {
this.items = items;
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return this.items.size();
}
@Override
public int getItemViewType(int position) {
if (items.get(position) instanceof User) {
return USER;
} else if (items.get(position) instanceof String) {
return IMAGE;
}
return -1;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case USER:
View v1 = inflater.inflate(R.layout.layout_viewholder1, viewGroup, false);
viewHolder = new ViewHolder1(v1);
break;
case IMAGE:
View v2 = inflater.inflate(R.layout.layout_viewholder2, viewGroup, false);
viewHolder = new ViewHolder2(v2);
break;
default:
View v3 = inflater.inflate(R.layout.layout_viewholder2, viewGroup, false);
viewHolder = new ViewHolder2(v3);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
switch (viewHolder.getItemViewType()) {
case USER:
ViewHolder1 vh1 = (ViewHolder1) viewHolder;
configureViewHolder1(vh1, position);
break;
case IMAGE:
ViewHolder2 vh2 = (ViewHolder2) viewHolder;
configureViewHolder2(vh2);
break;
default:
ViewHolder2 vh3 = (ViewHolder2) viewHolder;
configureViewHolder2(vh3);
break;
}
}
// private void configureDefaultViewHolder(RecyclerViewSimpleTextViewHolder vh, int position) {
// vh.getLabel().setText((CharSequence) items.get(position));
// }
private void configureViewHolder1(ViewHolder1 vh1, int position) {
User user = (User) items.get(position);
if (user != null) {
vh1.getLabel1().setText(user.getMFName());
vh1.getLabel2().setText( user.getMName());
//vh1.postImage.setImageResource(R.drawable.universe);
Picasso.with(vh1.postImage.getContext())
.load(R.drawable.universe)
.resize(400,200) // resizes the image to these dimensions (in pixel)
.centerCrop()
.placeholder(R.drawable.progress_animation)
.error(R.drawable.error_image)
.into(vh1.postImage);
}
}
private void configureViewHolder2(ViewHolder2 vh2) {
// vh2.getImageView().setImageResource(R.drawable.sample_golden_gate);
Picasso.with(vh2.getImageView().getContext())
.load(R.drawable.sample_golden_gate)
.resize(400,200) // resizes the image to these dimensions (in pixel)
.centerCrop()
.placeholder(R.drawable.progress_animation)
.error(R.drawable.error_image)
.into(vh2.getImageView());
}
}
使用全部的片段
public class OneFragment extends Fragment {
private static final int VERTICAL_ITEM_SPACE = 48;
Button button;
RecyclerView rvMixed;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
// Lookup the recyclerview in activity layout
rvMixed = (RecyclerView) rootView.findViewById(R.id.rvContacts);
rvMixed.setAdapter(new ComplexRecyclerViewAdapter(getSampleArrayList()));
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
// Control orientation of the items
// also supports LinearLayoutManager.HORIZONTAL
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// Optionally customize the position you want to default scroll to
layoutManager.scrollToPosition(0);
// Attach layout manager to the RecyclerView
rvMixed.setLayoutManager(layoutManager);
//adding spacing between recycler view items
// rvMixed.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
// That's all!
return rootView;
}
private void bindDataToAdapter() {
// Bind adapter to recycler view object
rvMixed = (RecyclerView) getView().findViewById(R.id.rvContacts);
ComplexRecyclerViewAdapter mAdapter = new ComplexRecyclerViewAdapter(getSampleArrayList());
rvMixed.setAdapter(mAdapter);
}
private ArrayList<Object> getSampleArrayList() {
ArrayList<Object> items = new ArrayList<>();
items.add(new User("Dany Targaryen", "Valyria"));
items.add(new User("Rob Stark", "Winterfell"));
items.add("image");
items.add(new User("Jon Snow", "Castle Black"));
items.add("image");
items.add(new User("Tyrion Lanister", "King's Landing"));
return items;
}
}
您可以在 ComplexRecyclerViewAdapter 初始化时通过其构造函数简单地传递点击监听器。然后您可以为每个 ViewHolder 重复使用相同的点击侦听器引用。
我有一个带有 2 种类型的查看器的 recyclerview,用于显示 2 种类型的项目 我想在项目中的子项目上实现点击监听器 现有解决方案显示 viewholder class 应该在适配器内部实现为静态,但我已经在单独的文件中声明了它们(建议使用教程), 所以我不能在 viewholders 上实现 onlick 监听器 我的 viewholder1 class 是
public class ViewHolder1 extends RecyclerView.ViewHolder {
private TextView label1, label2;
public ImageView postImage;
public ViewHolder1(View v) {
super(v);
label1 = (TextView) v.findViewById(R.id.text11);
label2 = (TextView) v.findViewById(R.id.text22);
postImage = (ImageView) v.findViewById(R.id.dummyImage);
}
public TextView getLabel1() {
return label1;
}
public void setLabel1(TextView label1) {
this.label1 = label1;
}
public TextView getLabel2() {
return label2;
}
public void setLabel2(TextView label2) {
this.label2 = label2;
}
}
viewholder2 是
public class ViewHolder2 extends RecyclerView.ViewHolder {
private ImageView ivExample;
public ViewHolder2(View v) {
super(v);
ivExample = (ImageView) v.findViewById(R.id.ivExample);
}
public ImageView getImageView() {
return ivExample;
}
public void setImageView(ImageView ivExample) {
this.ivExample = ivExample;
}
}
适配器是
public class ComplexRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// The items to display in your RecyclerView
private List<Object> items;
private final int USER = 0, IMAGE = 1;
// Provide a suitable constructor (depends on the kind of dataset)
public ComplexRecyclerViewAdapter(List<Object> items) {
this.items = items;
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return this.items.size();
}
@Override
public int getItemViewType(int position) {
if (items.get(position) instanceof User) {
return USER;
} else if (items.get(position) instanceof String) {
return IMAGE;
}
return -1;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case USER:
View v1 = inflater.inflate(R.layout.layout_viewholder1, viewGroup, false);
viewHolder = new ViewHolder1(v1);
break;
case IMAGE:
View v2 = inflater.inflate(R.layout.layout_viewholder2, viewGroup, false);
viewHolder = new ViewHolder2(v2);
break;
default:
View v3 = inflater.inflate(R.layout.layout_viewholder2, viewGroup, false);
viewHolder = new ViewHolder2(v3);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
switch (viewHolder.getItemViewType()) {
case USER:
ViewHolder1 vh1 = (ViewHolder1) viewHolder;
configureViewHolder1(vh1, position);
break;
case IMAGE:
ViewHolder2 vh2 = (ViewHolder2) viewHolder;
configureViewHolder2(vh2);
break;
default:
ViewHolder2 vh3 = (ViewHolder2) viewHolder;
configureViewHolder2(vh3);
break;
}
}
// private void configureDefaultViewHolder(RecyclerViewSimpleTextViewHolder vh, int position) {
// vh.getLabel().setText((CharSequence) items.get(position));
// }
private void configureViewHolder1(ViewHolder1 vh1, int position) {
User user = (User) items.get(position);
if (user != null) {
vh1.getLabel1().setText(user.getMFName());
vh1.getLabel2().setText( user.getMName());
//vh1.postImage.setImageResource(R.drawable.universe);
Picasso.with(vh1.postImage.getContext())
.load(R.drawable.universe)
.resize(400,200) // resizes the image to these dimensions (in pixel)
.centerCrop()
.placeholder(R.drawable.progress_animation)
.error(R.drawable.error_image)
.into(vh1.postImage);
}
}
private void configureViewHolder2(ViewHolder2 vh2) {
// vh2.getImageView().setImageResource(R.drawable.sample_golden_gate);
Picasso.with(vh2.getImageView().getContext())
.load(R.drawable.sample_golden_gate)
.resize(400,200) // resizes the image to these dimensions (in pixel)
.centerCrop()
.placeholder(R.drawable.progress_animation)
.error(R.drawable.error_image)
.into(vh2.getImageView());
}
}
使用全部的片段
public class OneFragment extends Fragment {
private static final int VERTICAL_ITEM_SPACE = 48;
Button button;
RecyclerView rvMixed;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
// Lookup the recyclerview in activity layout
rvMixed = (RecyclerView) rootView.findViewById(R.id.rvContacts);
rvMixed.setAdapter(new ComplexRecyclerViewAdapter(getSampleArrayList()));
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
// Control orientation of the items
// also supports LinearLayoutManager.HORIZONTAL
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// Optionally customize the position you want to default scroll to
layoutManager.scrollToPosition(0);
// Attach layout manager to the RecyclerView
rvMixed.setLayoutManager(layoutManager);
//adding spacing between recycler view items
// rvMixed.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
// That's all!
return rootView;
}
private void bindDataToAdapter() {
// Bind adapter to recycler view object
rvMixed = (RecyclerView) getView().findViewById(R.id.rvContacts);
ComplexRecyclerViewAdapter mAdapter = new ComplexRecyclerViewAdapter(getSampleArrayList());
rvMixed.setAdapter(mAdapter);
}
private ArrayList<Object> getSampleArrayList() {
ArrayList<Object> items = new ArrayList<>();
items.add(new User("Dany Targaryen", "Valyria"));
items.add(new User("Rob Stark", "Winterfell"));
items.add("image");
items.add(new User("Jon Snow", "Castle Black"));
items.add("image");
items.add(new User("Tyrion Lanister", "King's Landing"));
return items;
}
}
您可以在 ComplexRecyclerViewAdapter 初始化时通过其构造函数简单地传递点击监听器。然后您可以为每个 ViewHolder 重复使用相同的点击侦听器引用。