How to update current view in ViewPager

I am using ViewPager with views V1, V2, V3 ..... I am trying to set the visibility of the LinearLayout used in each view by clicking on the button. Using this code, a change is applied on the next view instead of the current view. for example, I'm on V5. When I click, it hides / shows the object on V6. If I switch back from V6 to V5, then it applies the change to 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 inform Thanks

+4
source share
2 answers

To make a free experience, ViewPager not only downloads the view you are viewing, but also adjacent views. This means that if you scroll from position 0 to position 1, then what actually happens is that position 2 loads, so it will be ready when you scroll. This is why the change applies to the "next" view, and not to the current one (if you scroll from view 2 to 1, then view 0 is created).

Since you set the static LinearLayout to OnCreate, then only the last view is created, which will be changed, and it will only ever be that you look at if you scroll to the end of the pager. Instead, you should keep track of which fragment the user is viewing (ViewPager.setOnPageChangeListener ()) and cache the fragment in your adapter. Then you know what position of the fragment you want, and when you ask for it, you simply return the one you created earlier (do not create a new one, then it will not work :)).

Or, tl version; dr: LL_Head is almost always set as the next fragment, not the current one. Do not put it statically. Download it to your PagerAdapter and configure it when you need it.

Edit: Alternatively, you might want the snippets to listen to a random event that tells them whether they should show or hide the layout in question. Otherwise, it will be only the current fragment, which is affected by this, and not all fragments.

+3
source

Numbering in Java starts at 0. Thus, if you want to set the 5th element, you need to call mPager.setCurrentItem (4);

Hope this helps!

0
source

Source: https://habr.com/ru/post/1388002/


All Articles