how to disable preloading nextpage in Viewpager? [duplicate] - android

This question already has answers here:
ViewPager.setOffscreenPageLimit(0) doesn't work as expected
(13 answers)
Closed 7 years ago.
How do I disable page pre loading in Viewpager??
I tried
viewPager.setOffscreenPageLimit(0)
but it's not working.

use this viewPager.setOffscreenPageLimit(1) instead of viewPager.setOffscreenPageLimit(0) . because the minimum value of setOffscreenPageLimit() is 1.

Have a look at this.
According to Ali Imran you can try this:
First create a count variable in the adapter class
second in the ViewPager Adapter make a public function say setCount()
and in the override getCount() function of Adapter return the count variable
now override the on pageScroll() function of the ViewPager when the
user flips the page you can call the setCount() to increase the
count of the count variable
also check that if that the current
index is last and page is scrolled to left

Related

How do I display items in a RecyclerView one by one onButtonClick instead of loading all questions at once

I have a list of questions with answer choices(let us assume 5 questions). Currently, all 5 questions are loaded at once in the RecyclerView, so a student can answer all the questions in the view. I would like to change this such that the first question loads and only after the student has answered and clicks the submit button, will the second load and so on. In other words, submit the answer to question 1, the view clears and shows question 2 and so on. An example of an app that I have seen achieve that is Duolingo. You only see the next question after you have submitted the current one displayed. How do I achieve this?
If you need to show only one question at a time, you don't need to use a RecyclerView at all. Just use text views and reuse them.
If you want to display old questions when a new question is unlocked, you may handle it in the getItemCount() method of your RecyclerView's adapter.
First sort the questions in order and use:
#Override
public int getItemCount() {
return currentQuestionIndex + 1;
}
After that, when the next button is clicked, increment the current question's index and call notifyDataSetChanged() in your RecyclerView.
If you want to display only one question and use RecyclerView anyway, return 1 from the getItemCount method. Then in the onBindViewHolder method instead of using the position parameter to get item from the list, use list.get(currentQuestionIndex);
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Question question = questionList.get(currentQuestionIndex);
//other code
}

Android ArrayList Adapter does not change pictures [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have an ArrayList adapter wich used to load my listView.
My problem is that I want to change a pictures on the items in the list;
Until now I used a simple String array and didnt have any problem, but I had to change it to ArrayList wich loaded by an ORMlite selection and now my adapter doesent change the pictures...
I toast the text of the item wich is like ..."Asd"...
and in the adapter I use this:
if(textView.getText()=="Asd"){imageView.setImageResource(R.drawable.ic_asd);}
I also tried this:
if(values.get(position).getName()=="Asd")
{
imageView.setImageResource(R.drawable.ic_asd);
}
What is the problem? :S
Well, it's hard to say exactly what the issue is, but the main thing that's sticking out is that you're checking String equality wrong.
To check string equality, you should use Object.equals(Object obj); For example, textView.getText()=="Asd" should be textView.getText().toString().equals("Asd");
yes your problem is that you should use textview.getText().equals("Asd")
Use
if(textView.getText().toString().equals("Asd"))
I'm sure this is the problem ;)

Android: Keep screen on [duplicate]

This question already has answers here:
Correct method for setKeepScreenOn / FLAG_KEEP_SCREEN_ON
(5 answers)
Closed 9 years ago.
I try to set the screen to always on however I cant figure out how to do it within a fragment. I have tried to get access to the ViewPager but it returns with a null value. I want the screen to be on for the whole application but it should be able to be changed by the user within the settings of the app.
Here is my code from within my fragment:
private void setIsAlwaysOn(boolean b)
{
ViewPager pager = (ViewPager)getView().findViewById(R.id.pager);
pager.setKeepScreenOn(b);
sharePropertiesEditor.putBoolean(sp_alwaysOn, b);
sharePropertiesEditor.commit();
}
I get a nullpointerexception at line 4 of the visible code above.
Yeah. Can't you use this?
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
For Kotlin, use:
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

Viewpager OffscreenPageLimit issue

I've multiple choice (radio buttons) questions on pages. I'm getting user answers with hashmap. So I set previous question answer on start of next question. As a result I start using set hashmap on start 2.page.
BUT problem is: I think viewpager loads first 2 pages at the same time ; so "set hashmap" on start 2.page goes null ; because 1. page isnt loaded / or just loaded at the same time
How can I get rid of this problem ?
page: list answers (1)
page: get user choice(1), clear list(1), set list answers (2)
page: get user choice(2), clear list(2), set list answers (3) ...
maybe setoffscreenpagelimit 0 could be solution but lowest value is 1
This older answer can probably help you.
Basically you just want to know when another fragment is visible, and update it from there.

Android : clear listview in Listadapter [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
how to clear my listview in android
In android, i have used a listadapter, simple adapter and passed an array and i want to clear the listview contents? Instead of clearing , it is appending the records to my listview.
Grab my Custom adapter from here. TestAdapter
Now add a method like this..
public void clearAdapter()
{
deviceNames.clear();
selected.clear();
notifyDataSetChanged();
}
Now call youListAdapter.clearAdapter() from you Activity.
Simply write listView.setAdapter(null)
Use notifydataSetInValidated() on your adapter to notify your listview that the data set is not longer valid. or set your addapter to null : l.setAdapter(null);

Categories

Resources