gpt4 book ai didi

android - 处理 recyclerview 内部项目的点击

转载 作者:行者123 更新时间:2023-11-30 01:54:22 25 4
gpt4 key购买 nike

我有一个 recyclerview 来显示 facebook,比如带有评论按钮的新闻源。当用户单击评论按钮时,我无法弄清楚如何显示对话框。我在这里找到的所有解决方案都是处理整个项目的点击而不是它的内部 View 。我也试过这个灵魂。 How to handle multiple layout clicks in recyclerView in Android

这是我的适配器类。

public class NewsfeedAdapter extends RecyclerView.Adapter<NewsfeedAdapter.NewsfeedViewHolder> {

private final LayoutInflater inflater;
private Context context;

ArrayList<NewsfeedItem> data= new ArrayList<NewsfeedItem>();

public NewsfeedAdapter(Context context,ArrayList<NewsfeedItem> data){
inflater= LayoutInflater.from(context);
this.data=data;
this.context = context;
}
@Override
public NewsfeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_newsfeed_row, parent, false);
NewsfeedViewHolder holder = new NewsfeedViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(NewsfeedViewHolder newsfeedViewHolder, int i) {
NewsfeedItem current = data.get(i);
newsfeedViewHolder.username.setText(current.username);
newsfeedViewHolder.icon.setImageResource(current.iconid);
newsfeedViewHolder.timestamp.setText((CharSequence) current.timestamp);
newsfeedViewHolder.news.setText(current.news);
}


@Override
public int getItemCount()
{
return data.size();
}


class NewsfeedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

TextView news, username, timestamp, comment_btn, like_btn;
ImageView icon;
private NewsfeedClickInterface clickListener;

public NewsfeedViewHolder(View itemView) {
super(itemView);
news = (TextView) itemView.findViewById(R.id.news);
timestamp = (TextView) itemView.findViewById(R.id.timestamp);
username = (TextView) itemView.findViewById(R.id.username);
icon = (ImageView) itemView.findViewById(R.id.profile_img);
comment_btn = (TextView) itemView.findViewById(R.id.comment_btn);
comment_btn.setTag("comment");
comment_btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.onItemClicked(getPosition(), v.getTag().toString());
}
}
}
public interface NewsfeedClickInterface {
public void onItemClicked(int position, String tag);
}

最佳答案

首先在您的viewholder 中定义一个界面。当按钮被点击时,调用该接口(interface)。

 public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

public interface ViewHolderClickListener {
public void onItemClick(View caller, int position);
}

public ViewHolderClickListener mListener;

public ViewHolder(View itemView, ViewHolderClickListener listener) {
super(itemView);
mListener = listener;
//other initializations...
}

@Override
public void onClick(View v) {
mListener.onItemClick(v, getPosition());
}
}

然后在单独的文件中创建另一个接口(interface),假设您在名为 Commons 的类中创建此接口(interface):

public class Commons {

public interface OnRecyclerItemClickedListener{
public void onRecyclerItemClicked(String parameter);
}

}

然后让您的 fragment/Activity 实现该接口(interface)。并将此接口(interface)作为参数传递给适配器的构造函数。

public class MyFragment extends Fragment implements Commons.OnRecyclerItemClickedListener{

@Override
public void onRecyclerItemClicked(String productId) {
//do whatever you want on click
}
}

将此接口(interface)传递给适配器的构造函数:

//here "this" parameter is your listener since your fragment implements
//the interface you defined
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(items, R.layout.yourlayout, getContext(), this);

然后在您的适配器中,创建您在 Commons 类中定义的接口(interface)的一个实例,并将其设置在您的构造函数中。然后在 onCreateViewHolder 方法中创建您的 viewholder。最后,在你的 Viewholder 的点击接口(interface)中调用你在 Commons 类中定义的接口(interface):

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>{

private OnRecyclerItemClickedListener onRecyclerItemClickedListener;

public MyRecyclerViewAdapter(List<Item> items, int rowLayout, Context context, OnRecyclerItemClickedListener onRecyclerItemClickedListener){
this.items = items;
this.rowLayout = rowLayout;
this.mContext = context;
this.onRecyclerItemClickedListener = onRecyclerItemClickedListener;
}



@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
ViewHolder holder = new ViewHolder(v, new ViewHolder.ViewHolderClickListener() {

@Override
public void onItemClick(View caller, int position) {
onRecyclerItemClickedListener.onRecyclerItemClicked(put_parameters_if_you_want);
}});
return holder;
}

}

这有点复杂,但这里是对您所做操作的解释:单击按钮时,您的按钮单击监听器调用另一个监听器(在您的 viewholder 中定义),并且该监听器调用另一个在中实现的监听器你的 fragment 。

这就是您需要做的所有事情。

关于android - 处理 recyclerview 内部项目的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32502215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com