jps 23 jps 23 - 1 month ago 14
Android Question

How to use horizontal view pager inside recycler view

I want to use viewpager inside recycler view and set data on pager from api.

For more details, i'm adding my code in question.

My Code is

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

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


Set text on 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();
}
});


Check if first answer is equals to correct answer

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){}
}
}
});


Check if secound answer is equals to correct answer

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){}

}
}
});


Check if third answer is equals to correct answer

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){}

}

}
});


Check if forth answer is equals to correct answer

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));
}

}
});
}

Answer

You cannot use a viewpager inside a recyclerview. If you want to use horizontal swiping inside a recyclerview then you will have to use a recyclerview inside a recyclerview. This child recyclerview will be have a horizontal layout.

This question precisely addresses that.

For the 2nd issue:

Add the following lines

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);

below this line:

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

for 3rd issue: //add this below your adapter class private ArrayList selectedOptions;

in your onbindviewholder add this below 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;
     }
    } 

I have answered 3 things now. Please ask new stackoverflow question for further issues.