具有多个按钮的RecyclerView OnClickListener

时间:2019-02-04 17:37:33

标签: android android-recyclerview

我正在设计测验应用程序。我的RecycleView包含一个TextField(用于问题)和四个Button(用于答案)。用户必须选择其中一个按钮。我想为所有按钮添加OnClickListener,以便我可以跟踪特定的问题,即用户单击了哪个按钮。

activity_play_quiz.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PlayQuiz">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_play_quiz" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

    <android.support.v7.widget.RecyclerView

        android:id="@+id/recycler_view_quiz_display"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >

    </android.support.v7.widget.RecyclerView>

</android.support.design.widget.CoordinatorLayout>

这是我的recyclerview定制适配器

public class QuizAdapter extends 
        RecyclerView.Adapter<QuizAdapter.CustomViewHolder> {

    private List<DmQuiz> questionList;
    private Context context;

    public QuizAdapter(List<DmQuiz> questionList, Context context) {
        this.questionList = questionList;
        this.context = context;
    }

    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup 
            parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.quiz_display_format,parent,false);
        return new CustomViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
        DmQuiz questionsList = questionList.get(position);
        holder.tvquestion.getLayoutParams().width =
                LinearLayout.LayoutParams.WRAP_CONTENT;
        holder.tvquestion.setText(questionsList.getQuestion());
        holder.optA.setText(questionsList.getOpta());
        holder.optB.setText(questionsList.getOptb());
        holder.optC.setText(questionsList.getOptc());
        holder.optD.setText(questionsList.getOptd());

    }

    @Override
    public int getItemCount() {
        return questionList.size();
    }

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        TextView tvquestion;
        Button optA,optB,optC,optD;



        public CustomViewHolder(View itemView) {
            super(itemView);
            tvquestion = (TextView)itemView
                    .findViewById(R.id.tvQuestion);
            optA = (Button)itemView.findViewById(R.id.button1);
            optB = (Button)itemView.findViewById(R.id.button2);
            optC = (Button)itemView.findViewById(R.id.button3);
            optD = (Button)itemView.findViewById(R.id.button4);


        }
    }

    public QuizAdapter(List<DmQuiz> questionList) {
        this.questionList = questionList;
    }

    public void setSearchOperation(List<DmQuiz> newList) {
        questionList = new ArrayList<>();
        questionList.addAll(newList);
        notifyDataSetChanged();
    }

}

这是我使用RecyclerView的PlayQuiz.java活动

public class PlayQuiz extends AppCompatActivity {

    private RecyclerView recyclerView;
    DataBaseHelper database;
    private List<DmQuiz> quizList;
    private QuizAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_quiz);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        recyclerView = (RecyclerView) 
                findViewById(R.id.recycler_view_quiz_display);
        database = new DataBaseHelper(PlayQuiz.this);
        quizList = database.fillObjQuesList();
        adapter = new QuizAdapter(quizList);
        RecyclerView.LayoutManager mLayoutManager = new 
                LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

    }

}

1 个答案:

答案 0 :(得分:0)

在您的PlayQuiz活动中,实现了View.OnClickListener,如下所示

public class PlayQuiz extends AppCompatActivity  implements View.OnClickListener{

@Override
public void onClick(View v) {
    Intent intent;
    switch (v.getId()) {
        case R.id.button1:
            // selected question 1
            break;
        case R.id.button1:
            // selected question 2
            break;
        case R.id.button1:
            // selected question 3
            break;
        case R.id.button1:
            // selected question 4
            break;
    }
}
}

然后在您的View Holder中添加以下内容

public CustomViewHolder(View itemView) {
    super(itemView);
    tvquestion = (TextView)itemView.findViewById(R.id.tvQuestion);
    optA = (Button)itemView.findViewById(R.id.button1);
    optB = (Button)itemView.findViewById(R.id.button2);
    optC = (Button)itemView.findViewById(R.id.button3);
    optD = (Button)itemView.findViewById(R.id.button4);
    optA.setOnClickListener( itemView.getContext() );
    optB.setOnClickListener( itemView.getContext() );
    optC.setOnClickListener( itemView.getContext() );
    optD.setOnClickListener( itemView.getContext() );
}