OnClick获取多次触发

时间:2018-06-06 15:58:31

标签: java android onclick

我有这个方法:

public void questions_makealist() {
    qAdapter = new QandARecyclerViewAdapter(qandas);

    qanda_View.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), qanda_View, new ClickListener() { // en as ons op n boksie druk
        @Override
        public void onClick(View view, int position) {
            Log.e("Heeeeeeeeeeeeeeeeeeeee","On Clicked");
            QandA c_qanda = qandas.get(position);

            flip_it.setDisplayedChild(6);
            reply_View = findViewById(R.id.replies);

            TextView Question_Q = findViewById(R.id.QuestionC);
            TextView Question_A = findViewById(R.id.asked_byC);

            Question_Q.setText(c_qanda.getQuestion());
            Question_A.setText(c_qanda.getABN());

            QID_Reply = c_qanda.getQID();

            maak_die_lys_vir_replies();

        }
        public void onLongClick(View view,int position) {}
        //moet dit hier sit vir die method
    }));

    RecyclerView.LayoutManager qLayoutManager = new LinearLayoutManager(getApplicationContext());
    qanda_View.setLayoutManager(qLayoutManager);
    qanda_View.setAdapter(qAdapter);
    //net vir testing purposes
    //add_item("Ds.Hennie", "40 Dae Reeks: Herstel", 1, 1, "6 Maart 2017");
    qandas.clear();
    qAdapter.notifyDataSetChanged();
    getQuestions getq = new getQuestions();
    getq.execute();
}

由于某种原因,点击后点击了我已点击它的次数。例如,如果这是第三次点击它,将在点击3次时轻轻地调用它。

请参阅下面我设置的错误日志。

06-06 17:56:32.584 24779-24779/ng.Wilgespruit.com E/Heeeeeeeeeeeeeeeeeeeee: On Clicked
06-06 17:56:32.592 24779-24779/ng.Wilgespruit.com E/Heeeeeeeeeeeeeeeeeeeee: On Clicked
06-06 17:56:32.593 24779-24779/ng.Wilgespruit.com E/Heeeeeeeeeeeeeeeeeeeee: On Clicked

请帮助

1 个答案:

答案 0 :(得分:0)

这是因为触摸视图时会多次触发onTouchListener(),而点击时只会触发onClickListener()一次。

如果要为RecyclerView中的每个项目添加onClickListener(),可以在适配器类中执行此操作。在适配器类中有两种方法。

  • onBindViewHolder()中的onClickListener设置为holder.itemView.setOnCLickListener()
  • 将它设置在适配器内的ViewHolder类中。

设置onClickListener后,您还可以使用接口将事件传递回适配器的活动。