RecyclerView列表项不响应事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView列表项不响应事件相关的知识,希望对你有一定的参考价值。
单击RecyclerView的列表项时,不会触发OnClick。
我创建了一个包含RecyclerView的片段。在Adapter类中,我为每个ViewHolder注册一个OnItemClickListener。
my dictionary fragment.Java
public class MyDictionaryFragment extends Fragment {
private static final String TAG = "MyDictionaryFragment";
private RecyclerView mWordsList;
private DictionaryAdapter mAdapter;
private static List<CardEntry> cards;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dictionary, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
setupViewModel();
//Recycler View
mWordsList = view.findViewById(R.id.rv_my_dictionary);
mWordsList.setLayoutManager(new LinearLayoutManager(getContext()));
mWordsList.setHasFixedSize(true);
//
super.onViewCreated(view, savedInstanceState);
}
private void setupViewModel() {
LearnWordsViewModel viewModel = ViewModelProviders.of(this).get(LearnWordsViewModel.class);
viewModel.getCards().observe(this, (cardEntries) -> {
if (cardEntries.isEmpty()) {
Log.d(TAG, "There is no cards retrieved from the DataBase");
return;
}
cards = cardEntries;
mAdapter = new DictionaryAdapter(cards);
mWordsList.setAdapter(mAdapter);
});
}
}
dictionary adapter.Java
public class DictionaryAdapter extends RecyclerView.Adapter<DictionaryAdapter.CardViewHolder> {
private static final String TAG = "DictionaryAdapter";
private static List<CardEntry> mCards;
public DictionaryAdapter(List<CardEntry> cards) {
mCards = cards;
}
@NonNull
@Override
public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
int dictItemLayoutId = R.layout.word_listitem;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(dictItemLayoutId, parent, false);
return new CardViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CardViewHolder holder, int position) {
if(position >= mCards.size()) {
return;
}
holder.word.setText(mCards.get(position).getWord());
}
@Override
public int getItemCount() {
return mCards.size();
}
class CardViewHolder extends RecyclerView.ViewHolder {
private static final String TAG = "CardViewHolder";
TextView word;
CardView cardView;
private CardViewHolder(@NonNull View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.dict_cardview);
word = itemView.findViewById(R.id.word_listitem);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(itemView.getContext(), "Test", Toast.LENGTH_SHORT).show();
}
});
}
}
}
我希望当我点击我的RecyclerView中的列表项时,事件将触发,但没有任何反应。为什么?
答案
你必须在onClick()
中实现按钮onBindViewHolder()
方法
public void onBindViewHolder(@NonNull CardViewHolder holder, int position) {
if(position >= mCards.size()) {
return;
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(itemView.getContext(), "Test", Toast.LENGTH_SHORT).show();
}
});
holder.word.setText(mCards.get(position).getWord());
}
另外,从我在你发布的帖子中你想添加点击监听器来查看自己?所以你必须声明你qazxsw poi构造函数,我会给你一个按钮示例:
CardViewHolder
然后在class CardViewHolder{
Button btn;
private CardViewHolder(View itemView){
...
btn = itemView.findViewById(R.id.btn);
}
你可以将btn称为onBindViewHolder
以上是关于RecyclerView列表项不响应事件的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView | 处理 RecyclerView 中的点击事件
Android:RecyclerView 不显示片段中的列表项