Java – refresh the Android viewpager fragment on refresh
I'm new to Android. I really don't understand why the dynamically added fragment content (such as some images added after the button is clicked) disappears after scrolling some fragments and then returning
There are very simple code activities and fragments:
public class MyActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); final CustomAdapter adapter = new CustomAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); } class CustomFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment,container,false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getView().findViewById(R.id.clickMeButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getView().findViewById(R.id.image).setVisibility(View.VISIBLE); } }); } } class CustomAdapter extends FragmentStatePagerAdapter { private List<CustomFragment> fragments = Arrays.asList( new CustomFragment(),new CustomFragment(),new CustomFragment() ); public CustomAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { return fragments.get(i); } @Override public int getCount() { return fragments.size(); } } }
And appropriate xmls:
main. In XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <android.support.v4.view.ViewPager android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/viewPager" /> </LinearLayout>
fragment. XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/clickMeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="click me"/> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:visibility="gone"/> </LinearLayout>
The logic is simple On each clip, I can click a button and an image will appear
It works But
When I display the image on the first clip, scroll to the third clip and return to the first clip again, and the image disappears
What can I do to prevent this from happening? Should I preserve visibility in some way?
Solution
This happens because the fragmentstatepageradapter frees the memory associated with the fragment when the fragment scrolls out of the view From docs:
You can try one of four options:
1. Because the number of pages is small, you can use fragmentpageradapter instead of fragmentstatepageradapter The fragmentpageradapter retains the created fragments and does not destroy them when sliding
2. In fragment, store a Boolean value in sharedpreference, and set (or clear) its value when making the image visible or invisible Then, in the onresume () method of fragment, make the image visible or invisible according to the value of sharedpreference
3. in Fragment's onCreate (), call setRetainInstance (true);.
4. If the number of fragments is fixed and relatively small, then add the following code to your oncreate():
mViewPager = (ViewPager)findViewById(R.id.pager); mViewPager.setOffscreenPageLimit(limit); /* limit is a fixed integer*/