RecyclerView OnClick事件不起作用

时间:2018-04-04 19:54:06

标签: android android-recyclerview

将onclicklistener设置为我的recyclerview时遇到了一些麻烦。我看到类似的问题已被问到,我尝试了所有可以找到的方法,但我没有得到任何结果。以下是我实现它的方法:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private List<Question> values;
private Context context;

public class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener{

    public TextView txtHeader;
    public View layout;

    public ViewHolder(View v) {
        super(v);
        layout = v;
        txtHeader = (TextView) v.findViewById(R.id.header);
    }

    @Override
    public void onClick(View view) {
        Log.d("Tag:", "Here");
        int position = getLayoutPosition();
        String question = values.get(position).getQuestion();
        String choiceA = values.get(position).getChoiceA();
        String choiceB = values.get(position).getChoiceB();
        String choiceC = values.get(position).getChoiceC();
        Integer questionID = values.get(position).getQuestionID();

        Intent intent = new Intent(context,SingleQuestionActivity.class);
        intent.putExtra("question", question);
        intent.putExtra("choiceA", choiceA);
        intent.putExtra("choiceB", choiceB);
        intent.putExtra("choiceC", choiceC);
        intent.putExtra("questionID", questionID);

        QuestionActivity questionActivity =
                (QuestionActivity) context;

        questionActivity.goToSingleQuestionScreen(intent);
    }
}
...

正如你所看到的,我放了一个Log.d来查看它是否适用于onClick方法。我在QuestionActivity类中设置了这个适配器,如下所示:

public class QuestionActivity extends AppCompatActivity{

...
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question);
    initializeComponents();

    recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setBackgroundColor(Color.YELLOW);

    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    QuestionRepository questionRepository =
            QuestionRepository.getInstance(getApplicationContext());

    Intent intent = getIntent();
    questions = questionRepository
            .getCategorizedQuestions(intent.getIntExtra("category", 1));

    mAdapter = new MyAdapter(questions, this);
    recyclerView.setAdapter(mAdapter);
...

我在哪里做错了?

2 个答案:

答案 0 :(得分:1)

尝试:

public ViewHolder(View v) {
        super(v);
        layout = v;
        txtHeader = (TextView) v.findViewById(R.id.header);
        v.setOnClickListener(this);
    }

希望这有帮助

答案 1 :(得分:0)

使用Butterknife可以轻松,干净地完成。

添加到您的gradle文件依赖项和同步项目。

implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

现在,设置侦听器和绑定视图更容易。您可以执行以下操作:

public class ViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.header) 
    TextView header;

    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    @OnClick
    void onClick(View view) {
        Log.d("pos", getAdapterPosition());
        Intent intent ...
        ...
        view.getContext().startActivity(intent);
    }
}

使用@BindView注释,您不必每次都致电findViewById() - Butterknife会为您做到这一点。此外,您不必实现侦听器 - 只需添加@OnClick注释。

此外,无需获取QuestionActivity并从适配器调用其方法(我假设它正在启动另一个活动)。可以从view.getContext()获取上下文,因为每个视图都包含对上下文的引用,然后您可以调用例如startActivity(intent)

如需进一步阅读,我建议

https://medium.com/@pranaypatel/butterknife-a-viewbinding-library-for-android-beginner-guide-fd92caf8e505

http://www.vogella.com/tutorials/AndroidButterknife/article.html