在RecyclerView中单击更改所选项目的颜色

时间:2016-10-20 11:11:32

标签: android android-recyclerview android-adapter

enter image description here我希望在我的项目中使用recyclerview获得此结果,此处有3种不同的文本视图,当我选择任何文本视图时,请更改其背景。

holder.llAns1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        holder.llAns1.setBackgroundResource(R.drawable.que_correct_back);
        holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);   
        allAns.add(holder.tvans1.getText().toString());
        correctans = userlist.get(position).get("correctans");

         /*
         check if selected option is correct
         * */
        if(holder.tvans1.getText().toString().equals(correctans)){
            CorrectAns.add(userlist.get(position).get("ans1"));
        } else {

                try {
                        CorrectAns.remove(position);
                        }catch (IndexOutOfBoundsException e){
                    }

                }
    }); 


holder.llAns2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        holder.llAns2.setTag(position);

        holder.llAns2.setBackgroundResource(R.drawable.que_correct_back);
        holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
        holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);correctans = userlist.get(position).get("correctans");

        /*
        check if selected option is correct
        * */
        if(holder.tvans2.getText().equals(correctans)){
            CorrectAns.add(userlist.get(position).get("ans2"));
            Log.d("corrAns1",String.valueOf(CorrectAns));

        } else {
                try {
                        CorrectAns.remove(correctans);
                    }
                    catch (IndexOutOfBoundsException e){}
                        Log.d("corrAns2",String.valueOf(CorrectAns));
                    }
                }
    }); 

holder.llAns3.setTag(position);

holder.llAns3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        holder.llAns3.setBackgroundResource(R.drawable.que_correct_back); /*set drawable if correct option is selected*/
        holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
        holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);

        correctans = userlist.get(position).get("correctans");

         /*
         check if selected option is correct
         * */

        if(holder.tvans3.getText().equals(correctans)){
            CorrectAns.add(userlist.get(position).get("ans3"));
            Log.d("corrAns1",String.valueOf(CorrectAns));

        }else {
            try {
                CorrectAns.remove(correctans);}catch (IndexOutOfBoundsException e){}
            Log.d("corrAns2",String.valueOf(CorrectAns));
        }
    }
});

1 个答案:

答案 0 :(得分:0)

要实现这样的Ui和功能,您可以使用viewpager而不是带有视图(项目视图)的回收器视图,该视图每次和当用户在Arraylist中选择答案(商店完整模型)商店时显示单个问题和答案。在View pager中,您还可以使用 setCurrentItem()从代码交换视图。在完成所有问题之后,您可以在新活动或片段中显示结果。