RecyclerView 列表项不响应事件

RecyclerView list items doesn't respond to events

当我点击 RecyclerView 的列表项时,OnClick 没有触发。

我创建了一个包含 RecyclerView 的片段。在 Adapter class 中,我为每个 ViewHolder 注册了一个 OnItemClickListener。

MyDictionaryFragment.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);
        });
    }
}

DictionaryAdapter.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 单击一个列表项时,该事件会触发,但没有任何反应。为什么?

您必须像这样在 onBindViewHolder() 中实现按钮 onClick() 方法

 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());
 }

另外从我在你身上看到的post你想添加clickListener来查看自己吗?所以你必须声明它你 CardViewHolder 构造函数,我会给你一个按钮的例子:

class CardViewHolder{
Button btn;
private CardViewHolder(View itemView){
...
btn = itemView.findViewById(R.id.btn);

}

然后在 onBindViewHolder 中你可以将 btn 称为 viewholder.btn