RecyclerView列表项不响应事件

时间:2019-01-16 20:45:08

标签: java android android-fragments android-recyclerview android-viewholder

当我单击RecyclerView的列表项时,OnClick不会触发。

我创建了一个包含RecyclerView的片段。在Adapter类中,我为每个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中单击一个列表项时,该事件将触发,但是没有任何反应。为什么?

1 个答案:

答案 0 :(得分:0)

您必须像这样在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());
 }

从我在您的帖子中看到的内容来看,您还想添加clickListener来查看自身吗?因此,您必须将其声明为CardViewHolder构造函数,我将为您提供按钮的示例:

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

}

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