How do I refresh ViewPager?

How to refresh current view in ViewPager

  • I am using ViewPager with views V1, V2, V3 ..... I am trying to set visibility of a LinearLayout used in each view, by clicking on a button. Through this code it apply the change on the next view instead of the current view. e.g. I am on V5. When I click it hides/show the object on V6. If I am going backwards from V6 to V5, then it applies the change on V4. Here is the code: public class FragmentStatePagerSupport extends FragmentActivity { static final int NUM_ITEMS = 10; MyAdapter mAdapter; ViewPager mPager; static int mNum; private Button btn_zoom; static LinearLayout LL_Head; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_pager); mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager)findViewById(R.id.pager); mPager.setAdapter(mAdapter); mPager.setCurrentItem(5); btn_zoom = (Button)findViewById(R.id.btn_zoom); btn_zoom.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { if (LL_Head.getVisibility() == View.VISIBLE) { LL_Head.setVisibility(View.GONE); }else{ LL_Head.setVisibility(View.VISIBLE); } } }); . . . } public static class MyAdapter extends FragmentStatePagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { return ArrayListFragment.newInstance(position); } } public static class ArrayListFragment extends ListFragment { static ArrayListFragment newInstance(int num) { ArrayListFragment f = new ArrayListFragment(); Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNum = getArguments() != null ? getArguments().getInt("num") : 1; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.sura_vpager, container, false); TextView tv1=(TextView) v.findViewById(R.id.txtHead); tv1.setText("Fragment #" + mNum); LL_Head = (LinearLayout)v.findViewById(R.id.LL_Head); return v; } Please advise Thanks

  • Answer:

    In order to make a fluent experience the ViewPager not only loads the view you are currently looking at, but also the adjacent views. That means, that if you are scrolling from position 0 to position 1, what actually happens is that position 2 is loaded, so it will be ready when you scroll on. This is why the change is applied to the "next" view, rather than the current one (if you scroll from view 2 to 1, then view 0 is created). Since you are setting the static LinearLayout in OnCreate, then it's only the last view to be created that is changed - and this will only ever be the one you are looking at, if you have scrolled to the end of the pager. Instead you should keep track of which fragment the user is looking at (ViewPager.setOnPageChangeListener()) and cache the fragment in your adapter. You then know which fragment position you want, and when you ask for it, you will just return the one you previously created (don't create a new one, then it won't work :)). Or, the tl;dr version: LL_Head is almost always set to be the next fragment, not the current one. Don't set it statically. Cache it in your PagerAdapter and reget it when you need it. Edit: Alternatively you may want to have the fragments listen to an event of sorts, which tells them whether they should show or hide the layout in question. Otherwise it will only be the current fragment that is affected by this, rather than all fragments.

Azam at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

The numbering in Java starts from 0. Thus when you want to set the 5th item, you have to call mPager.setCurrentItem(4); Hope this helps!

Dimitris Makris

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.