How to use horizontal view pager inside recycler view - android

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

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.

Related

How can I know which button is clicked inside my CardView if I have a dynamic RecyclerView/CardView?

I have two imageviews with in my card view, now how can I setOnClickListeners so that I can know which button in which cardview is selected. im1 and im2 are my clickable ImageViews
This is my code :
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ConnIfInfo dataModel = ifList.get(position);
Log.d("Name", "if list name: "+dataModel.getName());
holder.name.setText(dataModel.getName());
holder.appName.setText(dataModel.getApp().toString());
if(String.valueOf(dataModel.getPreferredModeMode().toString()) .equals( String.valueOf(ProjectionTypes.OperationMode.AOA_AA)))
{
holder.im1.setImageResource(R.drawable.auto1);
holder.im2.setImageResource(R.drawable.carlife);
holder.im1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
iPhoneProjectionManager.startApp(1,"Nexus 5");
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
holder.im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
iPhoneProjectionManager.startApp(2,"Nexus 6");
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
}
#Override
public int getItemCount() {
return ifList.size();
}
You can set the tag to the view. The tag can be any object.
holder.im2.setTag(position);
you can set many tags also, with the key, and the key should be unique resId. you can dump the ids in ids.xml. Refer this
holder.im2.setTag(<res_id>, position);
and you can get the tag as
holder.im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getTag()
//or if you set with key then
v.getTag(<res_id>)
// please check for null value
}
});
instead of setting the onClickListener inside the onBindViewHolder you can do that in ViewHolder class itself
You can also do this by using Interface also
For more details please check out This link contains the brief about the how to handle click event by using interface.

Change color of select items on Click in recyclerView

I want this result from recyclerview in my project, here 3 different text views and when i select any text view then change its background.
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));
}
}
});
To implement such Ui and functionality you can use viewpager instead of recycler view with a view (Item view) which display single question and answer every time and when user pick answer (store full model) store in Arraylist. in View pager you can swap views also from code using setCurrentItem(). After all that question and all, you can display result in new activity or fragment.

How can I optimize the performance of opening an Android fragment?

I have created an 800 xml layout and a Fragment class, but opening a fragment with a button click takes too long.
Can anyone help me optimize the following code sample?
View lesson655 = (View) getActivity().findViewById(R.id.txtlesson655);
lesson655.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int i;
pager.setCurrentItem(0);
for (i = 1; i <= 655; i++) {
pager.setCurrentItem(pager.getCurrentItem() + 1);
}
}
});
If you don't need to go step by step through every Item you can do it this way:
View lesson655 = (View) getActivity().findViewById(R.id.txtlesson655);
lesson655.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
pager.setCurrentItem(655);
}
});

How can I add some view (eg. 10) that created with inflater?

How can I add some view or remove that created with inflater?
when I’m going to adding second of view it crached
View view = G.inflater.inflate(R.layout.activity_sector, linearLayout, false);
int i=0;
btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (myEditText.getText().toString().equals("") || i >= 10) {
return;
} else {
linearLayout.addView(view);
i++;
}
}
});
imgDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
myEditText.setText("");
linearLayout.removeView(view);
i--;
}
});
LOG : java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
The problem is that you are adding the same inflated view every time. Try the following code instead:
#Override
public void onClick(View arg0) {
if (myEditText.getText().toString().equals("") || i >= 10) {
return;
} else {
View view = G.inflater.inflate(R.layout.activity_sector, linearLayout, false);
linearLayout.addView(view);
i++;
}
}
You can add multiple views by inflating them once the click listener registers a click:
btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (myEditText.getText().toString().equals("") || i >= 10) {
return;
} else {
View view = G.inflater.inflate(R.layout.activity_sector, linearLayout, false);
linearLayout.addView(view);
i++;
}
}
});
Then a click on imgDelete object would remove the last view inside the layout.
imgDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
myEditText.setText("");
LinearLayout linearLayout;
// Get total view count
int childCount = linearLayout.getChildCount();
// Remove last view inside the layout
linearLayout.removeViewAt(childCount-1);
i--;
}
});
I believe that's the behaviour you are looking for.

How can i change background color of my layout by clicking a Button

How can i change background color of my layout by clicking on a Button ?
This is my code :
Button color_change;
LinearLayout layout;
int value = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
color_change = (Button)findViewById(R.id.color_btn);
layout = (LinearLayout)findViewById(R.id.LL);
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (value == 1) {
layout.setBackgroundColor(Color.RED);
}
else if (value == 2) {
layout.setBackgroundColor(Color.BLUE);
}
else if (value == 3) {
layout.setBackgroundColor(Color.MAGENTA);
}
else if (value == 4) {
layout.setBackgroundColor(Color.DKGRAY);
value = 0;
}
value++;
}
});
But i want replace if else condition with other code , because this code is complex and i want short code.
So any one suggest me, how can i short my code ?
You can try this
put all color into a array and in onclick method get particular color from the array and set it like
int color[]=new int[]{Color.BLUE,Color.RED,Color.GRAY};
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (value <color.length) {
layout.setBackgroundColor(color[value]);
}
value++;
}
});
There is no magic. Use the switch instead of else ifs. For the added clarity, you might consider having constants or something as values to value, so that you don't have to deal with hard-coded integers.
You could use an array to store the data:
int[] colors = {Color.RED, Color.BLUE, Color.BLACK};
int index = value % colors.length;
layout.setBackgroundColor(colors[index]);
value++;
You will use like this...
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switch(value)
{
case 1:layout.setBackgroundColor(Color.RED);
break;
case 2:layout.setBackgroundColor(Color.BLUE);
break;
case 3: layout.setBackgroundColor(Color.MAGENTA);
break;
case 4:layout.setBackgroundColor(Color.DKGRAY);
value = 0;
break;
}
value++;
}
});
A list of colors you want to use
List<int> colorsList = new List<int>();
colorsList.add(Color.RED);
colorsList.add(Color.WHITE);
colorsList.add(Color.BLUE);
colorsList.add(Color.GREEN);
//here you can add other colors to list
Iterator<int> colorIterator = colorsList.iterator();
function to get next color
int getNextColor()
{
if(colorIterator.hasNext())
{
return colorIterator.next();
}
else
{
colorIterator = colorsList.iterator();
return colorIterator.next();
}
}
and here is your listener
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int color = getNextColor();
layout.setBackgroundColor(color);
}
});

Categories

Resources