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

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

Related

Toggle Button Recyclerview Issue

i've this issue made me crazy. When retrieved JSON data i will use this my adapter:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof OriginalViewHolder) {
final DomandaQuizTipo1 domandaQuiz1 = items.get(position);
OriginalViewHolder vItem = (OriginalViewHolder) holder;
/* Getting each answer from array of answer */
Risposta1 risposta1 = domandaQuiz1.getRisposte().get(0);
Risposta1 risposta2 = domandaQuiz1.getRisposte().get(1);
Risposta1 risposta3 = domandaQuiz1.getRisposte().get(2);
/* Get and set text of question */
vItem.domanda.setTypeface(typefaceRegular);
vItem.domanda.setText(domandaQuiz1.getDomanda());
/* Get and set text of answer 1 */
vItem.risposta1.setText(risposta1.getRisposta());
vItem.risposta1.setTextOn(risposta1.getRisposta());
vItem.risposta1.setTextOff(risposta1.getRisposta());
/* Get and set text of answer 2 */
vItem.risposta2.setText(risposta2.getRisposta());
vItem.risposta2.setTextOn(risposta2.getRisposta());
vItem.risposta2.setTextOff(risposta2.getRisposta());
/* Get and set text of answer 3 */
vItem.risposta3.setText(risposta3.getRisposta());
vItem.risposta3.setTextOn(risposta3.getRisposta());
vItem.risposta3.setTextOff(risposta3.getRisposta());
/* When i click the first answer uncheck 2nd and 3rd */
vItem.risposta1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(0).getPunteggio();
MainActivity.UpdatePunteggioTotale();
vItem.risposta2.setChecked(false);
vItem.risposta3.setChecked(false);
}
});
/* When i click the second answer uncheck 1st and 3rd */
vItem.risposta2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(1).getPunteggio();
MainActivity.UpdatePunteggioTotale();
Log.d("adapterposition", "POS" + position);
vItem.risposta1.setChecked(false);
vItem.risposta3.setChecked(false);
}
});
/* When i click the third answer uncheck 1st and 2nd */
vItem.risposta3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(2).getPunteggio();
MainActivity.UpdatePunteggioTotale();
vItem.risposta1.setChecked(false);
vItem.risposta2.setChecked(false);
}
});
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
My issue is this: in a recyclerview of each question that contains 3 answers to choose, when i click on one of answer (ex. first toggle button) is checked correctly but when i scroll recycler view i see other question with answer (ex. first toggle button) checked.
How can i avoid this issue? Take a look from this gif i putted below.
Check gif this please to understand
You should add a field like a selected (local not server) and check that's is selected and change item ui
if(vItem.selected==1)
vItem.risposta1.setChecked(true);
else
vItem.risposta1.setChecked(false);
if(vItem.selected==2)
vItem.risposta2.setChecked(true);
else
vItem.risposta2.setChecked(false);
if(vItem.selected==3)
vItem.risposta3.setChecked(true);
else
vItem.risposta3.setChecked(false);
vItem.risposta1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=1;
notifyItemChange(adapterPosition);
}
});
vItem.risposta2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=2;
notifyItemChange(adapterPosition);
}
});
vItem.risposta3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=3;
notifyItemChange(adapterPosition);
}
});
You have to set the status of each toggle button in onBindViewHolder.
Something like:
vItem.risposta1.setText(risposta1.getRisposta());
//...
vItem.risposta1.setChecked(risposta1.isChecked());
Also when you update the data in the OnClickListener you have to notify the adapter:
adapter.notifyDataSetChanged();

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

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.

Android button 2 clicks

How to change the android button after 2 clicks ?
the first time to change button i will use this code
{
public void onClick(View v) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
}
}
I want to change the button view again after pressing it one more time
how can i do that ?
Perhaps do it like this:
int count = 0;
public void onClick(View v) {
count++;
if(count == 2){
count = 0;
b.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.menubuttonpressed));
}
}
This will set the background after every 2nd click on your button (view).
private int clickCount =0;
public void onClick(View v) {
if (clickCount==0) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
} else {
// do something else
}
clickCount++;
}
Well, one way is to keep a counter.
numberOfClicks = 0;
...
public void onClick(View v) {
...
if(numberOfClicks==0)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed0));
else if(numberofClicks==1)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed1));
...
numberofClicks++;
}

how to set onclicklistener to items in current page?

I have an application where i want to add onclicklistener to my individual items in pager.
Below is my screen shot . please some one tell me how to add onclicklistener to individual image in this current page .
Thanks
below is my code for pager view.
here when 1st time run the app initialize the every view page, when i try to click on image it return only last image info. But i want the current image info.
scroller = ((Pager)findViewById(R.id.scrollView));
indicator = ((PageIndicator)findViewById(R.id.indicator));
indicator.setPager(scroller);
// Pager pager = new Pager(this, attrs)
//indicator.getActivePage();
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pageView = null;
for (int i = 0; i < NUM_PAGES; i++) {
pageView = layoutInflater.inflate(R.layout.page, null);
// ((TextView) pageView.findViewById(R.id.pageText)).setText("Page " + (i+1));
c=4*i;
imageView1 = (ImageView) pageView.findViewById(R.id.imageView1);
imageView1.setImageResource(icons[c]);
imageView2 = (ImageView) pageView.findViewById(R.id.imageView2);
imageView2.setImageResource(icons[c+1]);
imageView3 = (ImageView) pageView.findViewById(R.id.imageView3);
imageView3.setImageResource(icons[c+2]);
imageView4 = (ImageView) pageView.findViewById(R.id.imageView4);
imageView4.setImageResource(icons[c+3]);
// pageView.setBackgroundColor(COLORS[i % COLORS.length]);
imageView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
scroller.addPage(pageView);
}
It seem to be you didn't read information available at android developers.
Use below code to add onClickListener for images. I don't know whether you are using ImageView or ImageButton. I took ImageView.
ImageView iv = new ImageView(/**Context you have to pass**/);
iv.setOnClickListener(new View.OnClickListener) {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do whatever you want
}
});
I hope it may help you.
set this on your images..or button
in the xml such that
<Button [...]
android:onClick: "onClick" [...]
/>
"onClick": name this whatever you want your method name that handles clicks to be
This can be done by assigning onClickListeners to Image Buttons. You can use Image buttons to display these images and set onClickListeners on them. Perhaps something like this can help?
ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Do your stuff here
}
});
If you use ImageView then set the property OnClick to true.

Categories

Resources