OnclickItem RecycleView

时间:2018-12-07 15:39:27

标签: java android

在我的recycleview适配器中,我有这个类,我想从我的recycleview中获取文本,但是当我喜欢项目时,什么也没发生,我认为我应该在片段中调用methode,但是我不知道怎么做!

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView myTextView;

    ViewHolder(View itemView) {
        super(itemView);
        myTextView = itemView.findViewById(R.id.comicName);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (mClickListener != null)
            {
                mClickListener.onItemClick(view, getAdapterPosition());
            Toast.makeText(ApplicationContextProvider.getContext(),getItem(getAdapterPosition()),Toast.LENGTH_LONG).show();
        }
    }
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
    void onItemClick(View view, int position);
}

1 个答案:

答案 0 :(得分:0)

您必须为自己的recycleritemclick创建自己的回调方法,然后在您的脆弱环境中注册该回调并通过该回调解析所需的任何数据。有关代码和其他详细信息,请参阅以下答案。
Refer to this answer for implementation details.