如何在Recycler视图中使用水平视图寻呼机

时间:2016-10-21 06:37:51

标签: android android-viewpager android-recyclerview android-adapter

我想在回收站视图中使用viewpager,并在api上的pager上设置数据。

有关详细信息,我正在添加我的相关代码。

我的代码是

 public void onBindViewHolder(final worksheetAdapter.CustomViewHolder holder, final int position) {

        final int a = position;            rec_worksheet.setTag(position);

在TextView上设置文本

        holder.question.setText(userlist.get(position).get("ques"));
        holder.question.startAnimation(animFadein);
        holder.tvans1.setText(userlist.get(position).get("ans1"));
        holder.tvans1.startAnimation(animFadein);
        holder.tvans2.setText(userlist.get(position).get("ans2"));
        holder.tvans2.startAnimation(animFadein);
        holder.tvans3.setText(userlist.get(position).get("ans3"));
        holder.tvans3.startAnimation(animFadein);
        holder.tvans4.setText(userlist.get(position).get("ans4"));
        holder.tvans4.startAnimation(animFadein);
        holder.submitbtn.setVisibility(View.GONE);
        if(position == userlist.size()  - 1){
            holder.submitbtn.setVisibility(View.VISIBLE);
        }

        holder.queNo.setText("Question No "+String.valueOf(position+1));
        holder.tvTotalQue.setText("Total Ques. "+String.valueOf(userlist.size()));
        holder.tvSubName.setText(userlist.get(position).get("subject_name"));

        holder.submitbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });

检查第一个答案是否等于正确答案

        holder.llAns1.setTag(position);
        holder.llAns1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 v.getTag(position);
                   holder.llAns1.setBackgroundResource(R.drawable.que_correct_back); 
                   holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
                   holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
                   holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);

                allAns.add(userlist.get(position).get("ans1"));

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

                if(holder.tvans1.getText().toString().equals(correctans)){
                    CorrectAns.add(userlist.get(position).get("ans1"));     
                }else {
                    try {
                        wrongAns.add(userlist.get(position).get("ans1"));
                        CorrectAns.remove(position);
                    }catch (IndexOutOfBoundsException e){}
                }
            }
        });

检查secound答案是否等于正确答案

        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);
                holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);

                correctans = userlist.get(position).get("correctans");
                if(holder.tvans2.getText().equals(correctans)){
                    CorrectAns.add(userlist.get(position).get("ans2"));                    
                }else {
                    try {
                        wrongAns.add(userlist.get(position).get("ans2"));
                        CorrectAns.remove(correctans);
                    }catch (IndexOutOfBoundsException e){}

                }
            }
        });

检查第三个答案是否等于正确答案

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

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

                if(holder.tvans3.getText().equals(correctans)){
                    CorrectAns.add(userlist.get(position).get("ans3"));


                }else {
                    try {
                        wrongAns.add(userlist.get(position).get("ans3"));
                        CorrectAns.remove(correctans);
                    }catch (IndexOutOfBoundsException e){}

                }

            }
        });

检查答案是否等于正确答案

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

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


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

                }else {
                    try {
                        wrongAns.add(userlist.get(position).get("ans1"));
                        CorrectAns.remove(correctans);
                    }catch (IndexOutOfBoundsException e){}
                    Log.d("corrAns2",String.valueOf(CorrectAns));
                }

            }
        });
    }

1 个答案:

答案 0 :(得分:0)

您无法在recyclerview中使用viewpager。如果要在recyclerview中使用水平滑动,则必须在recyclerview中使用recyclelerview。这个儿童Recyclerview将采用水平布局。

This question正好解决了这个问题。

第二期:

添加以下行

holder.llAns2.setBackgroundResource(R.drawable.round_strock_green); 
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);

在这一行之下:

if(position == userlist.size()  - 1){
    holder.submitbtn.setVisibility(View.VISIBLE);
}

第3期:     //在适配器类下面添加此项     private ArrayList selectedOptions;

在您的onbindviewholder

中添加以下holder.llAns4.setBackgroundResource(R.drawable.round_strock_green)

if(selectedOptions.get(position) != null){
    switch (selectedOptions.get(position)){
       case 1:
        holder.llAns1.setBackgroundResource(R.drawable.que_correct_back);
        break;
       case 2:
        holder.llAns2.setBackgroundResource(R.drawable.que_correct_back);
        break;
       case 3:
        holder.llAns3.setBackgroundResource(R.drawable.que_correct_back);
        break;
       case 4:
        holder.llAns4.setBackgroundResource(R.drawable.que_correct_back);
        break;
     }
    } 

我现在回答了3件事。有关更多问题,请询问新的stackoverflow问题。