gpt4 book ai didi

android - RecyclerView单击不起作用,但单击两次时起作用

转载 作者:IT老高 更新时间:2023-10-28 23:31:23 27 4
gpt4 key购买 nike

我正在尝试接收对我正在使用回调机制的 RecyclerView 项目的点击。我已经创建了 OnItemClickListener 接口(interface)并在自定义适配器内部,我已经编写了在 View.OnClickListener 的帮助下检测点击的逻辑。但是在双击列表中的任何项目后,我在 myActivity 中收到了回调。不知道这里发生了什么!

Activity 中的代码:

mAdapter = new AppAdapter(this, mAppList, new OnItemClickListener() {
@Override
public void onItemClick(View v, int position) {

Toast.makeText(SelectAppActivity.this, "Hello", Toast.LENGTH_SHORT).show();
}
});

recyclerview.setAdapter(mAdapter);

接口(interface)代码:

public interface OnItemClickListener {

void onItemClick(View v, int position);
}

自定义适配器代码:

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ItemViewHolder> {

List<App> mAppList;
List<App> mFilterAppList;
Activity context;
OnItemClickListener onItemClickListener;

public AppAdapter(Activity context, List<App> appList, OnItemClickListener onItemClickListener) {
super();
this.context = context;
this.mAppList = appList;
this.mFilterAppList = appList;
this.onItemClickListener = onItemClickListener;
}

@Override
public ItemViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app_list, parent, false);

final ItemViewHolder viewHolder = new ItemViewHolder(view);

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(v,4);
}
});
return viewHolder;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
final App app = mAppList.get(position);
holder.name.setText(app.getmName());
holder.icon.setImageDrawable(app.getmAppIcon());
}

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

public void setFilter(List<App> appList) {
mAppList = new ArrayList<>();
mAppList.addAll(appList);
notifyDataSetChanged();
}

public static class ItemViewHolder extends RecyclerView.ViewHolder {

public TextView name;
public ImageView icon;

public ItemViewHolder(View view) {
super(view);
name = (TextView) itemView.findViewById(R.id.app_name);
icon = (ImageView) itemView.findViewById(R.id.app_icon);
}
}
}

最佳答案

问题与我的布局中的 android:focusable="true"有关,因为我删除了它工作正常,所有建议的答案也工作正常。谢谢,所有。

关于android - RecyclerView单击不起作用,但单击两次时起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38242530/

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