我找不到在 Android Studio 中创建的 recyclerview 的 setOnItemClickListener
Posted
技术标签:
【中文标题】我找不到在 Android Studio 中创建的 recyclerview 的 setOnItemClickListener【英文标题】:I can't find setOnItemClickListener for the recyclerview created in Android studio 【发布时间】:2020-12-08 04:04:30 【问题描述】:这是主要活动。如果我尝试将 setOnItemClickListener 添加到回收站视图,它会显示“无法在 'RecyclerView' 中解析方法 'setOnItemClickListener'”
公共类 MainActivity 扩展 AppCompatActivity
private static final String TAG = "MainActivity";
RecyclerView recyclerView;
linkRecViewAdapter adapter;
ArrayList<Link> newLink = new ArrayList<>();
private ActionMode currentActionMode;
private int currentListItemIndex;
private ActionMode.Callback modeCallBack;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate: ");
adapter = new linkRecViewAdapter(this);
adapter.setLinks(newLink);
recyclerView = findViewById(R.id.recView);
recyclerView.setAdapter(adapter);
recyclerView.setOnLongClickListener(new View.OnLongClickListener()
@Override
public boolean onLongClick(View view)
if (currentActionMode != null)
return false;
currentActionMode = startActionMode(modeCallBack);
view.setSelected(true);
return true;
);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
这是适配器类
公共类 linkRecViewAdapter 扩展 RecyclerView.Adapter
Context context;
ArrayList<Link> links;
public linkRecViewAdapter(Context context)
this.context = context;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(context).inflate(R.layout.link_rec_view, parent, false);
return new ViewHolder(view);
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position)
holder.txtFolderName.setText(links.get(position).getFolderName());
holder.txtItemCount.setText(links.get(position).getItemCount());
holder.cardView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(context, NewDirectory.class);
context.startActivity(intent);
);
holder.cardView.setOnLongClickListener(new View.OnLongClickListener()
@Override
public boolean onLongClick(View view)
Toast.makeText(context, "This is a cardView", Toast.LENGTH_SHORT).show();
return true;
);
@Override
public int getItemCount()
return links.size();
public void setLinks(ArrayList<Link> links)
this.links = links;
public class ViewHolder extends RecyclerView.ViewHolder
TextView txtFolderName, txtItemCount, txtDateCreated;
CardView cardView;
RelativeLayout edtRelLay;
ImageView imgLink, imgFolder;
public ViewHolder(@NonNull View itemView)
super(itemView);
txtFolderName = itemView.findViewById(R.id.txtFolderName);
txtItemCount = itemView.findViewById(R.id.txtItemCount);
txtDateCreated = itemView.findViewById(R.id.txtDateCreated);
cardView = itemView.findViewById(R.id.cardView);
edtRelLay = itemView.findViewById(R.id.edtRelLay);
imgFolder = itemView.findViewById(R.id.imgFolder);
imgLink = itemView.findViewById(R.id.imgLink);
【问题讨论】:
请详细说明您的问题并将您的代码粘贴到此处以便与您联系。这取决于你的代码! @MatinZD 我已经发布了我的代码。请帮帮我。 【参考方案1】:RecyclerView 内置没有点击监听器。你需要自己实现一个,就像你在这里做的那样:
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position)
holder.txtFolderName.setText(links.get(position).getFolderName());
holder.txtItemCount.setText(links.get(position).getItemCount());
holder.cardView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(context, NewDirectory.class);
context.startActivity(intent);
);
holder.cardView.setOnLongClickListener(new View.OnLongClickListener()
@Override
public boolean onLongClick(View view)
Toast.makeText(context, "This is a cardView", Toast.LENGTH_SHORT).show();
return true;
);
唯一的区别是您需要在父视图本身上设置点击侦听器 - 为此您首先需要将其存储在您的 ViewHolder 中
public class ViewHolder extends RecyclerView.ViewHolder
TextView txtFolderName, txtItemCount, txtDateCreated;
CardView cardView;
RelativeLayout edtRelLay;
ImageView imgLink, imgFolder;
View itemView;
public ViewHolder(@NonNull View itemView)
super(itemView);
this.itemView = itemView;
txtFolderName = itemView.findViewById(R.id.txtFolderName);
txtItemCount = itemView.findViewById(R.id.txtItemCount);
txtDateCreated = itemView.findViewById(R.id.txtDateCreated);
cardView = itemView.findViewById(R.id.cardView);
edtRelLay = itemView.findViewById(R.id.edtRelLay);
imgFolder = itemView.findViewById(R.id.imgFolder);
imgLink = itemView.findViewById(R.id.imgLink);
【讨论】:
以上是关于我找不到在 Android Studio 中创建的 recyclerview 的 setOnItemClickListener的主要内容,如果未能解决你的问题,请参考以下文章
如何膨胀由 Android Studio 向导在 Activity 中创建的片段(列表)?
Android Studio 2.2.2导入Eclipse中创建的项目
在 Windows 资源管理器中看不到在 android 中创建的文件 [重复]
如何在 Kotlin 中创建一个打开新活动(Android Studio)的按钮?