ViewPage Fragment disappear when reload again

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

The following is layout

test.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:layout_width="wrap_content" android:layout_height="wrap_content"         android:text="Click"         android:id="@+id/button"         />  <!-- Framelayout to display Fragments --> <FrameLayout         android:id="@+id/frame_container"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </LinearLayout> 

test_detail.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">  <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textAppearance="?android:attr/textAppearanceLarge"         android:text="Hello"         android:id="@+id/textView" android:layout_gravity="center_horizontal"/> </LinearLayout> 

common_view_pager_layout

<?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">      <android.support.v4.view.ViewPager         android:id="@+id/MainViewerPage"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         >     <android.support.v4.view.PagerTabStrip             android:id="@+id/TitlePageTabStrip"             android:layout_width="match_parent"             android:layout_height="wrap_content" />     </android.support.v4.view.ViewPager>  </LinearLayout> 

The following is my Activity code

    public class TestFragmentActivity extends FragmentActivity {     @Override     public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     this.setContentView(R.layout.test);      Button button = (Button) this.findViewById(R.id.button);     button.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View view) {             FragmentManager fragmentManager = getSupportFragmentManager();             fragmentManager.beginTransaction()                     .replace(R.id.frame_container, new HomeFragment()).commit();          }     });      }      public class HomeFragment extends Fragment {     private PagerAdapter _adapter;      public HomeFragment() {     }      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,                              Bundle savedInstanceState) {          View view = inflater.inflate(R.layout.common_view_pager_layout, container, false);          this._adapter = new PagerAdapter(TestFragmentActivity.this);         this._adapter.add(new DetailFragment());          ViewPager pager = (ViewPager) view.findViewById(R.id.MainViewerPage);         pager.setAdapter(this._adapter);          return view;     }     }       public class DetailFragment extends Fragment {      public DetailFragment() {     }      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,                              Bundle savedInstanceState) {          View rootView = inflater.inflate(R.layout.test_detail, container, false);          return rootView;     }     }       public class PagerAdapter extends FragmentPagerAdapter {     private ArrayList<Fragment> _fragments;      public PagerAdapter(FragmentActivity activity) {         super(activity.getSupportFragmentManager());          this._fragments = new ArrayList<Fragment>();     }      public void add(Fragment fragment) {         this._fragments.add(fragment);     }      @Override     public Fragment getItem(int position) {         return this._fragments.get(position);     }      @Override     public CharSequence getPageTitle(int position) {         return "Test";     }      @Override     public int getCount() {         return 1;     }       } } 

When I click a button the fragment in ViewPager was display

But when I click the button again the fragment disappear

Please help me.

回答1:

try using this:

in the constructor of your adapter change this :

public class MyPagerAdapter extends FragmentPagerAdapter { private ArrayList<Fragment> _fragments;  public MyPagerAdapter(FragmentManager activity) {     super(activity);      this._fragments = new ArrayList<Fragment>(); }  public void add(Fragment fragment) {     this._fragments.add(fragment); }  @Override public Fragment getItem(int position) {     return this._fragments.get(position); }  @Override public CharSequence getPageTitle(int position) {     return "Test"; }  @Override public int getCount() {     return 1; }   } 

and when you want to create the adapter send getChildFragmentManager() to its constructor

public class HomeFragment extends Fragment { private MyPagerAdapter _adapter;  public HomeFragment() { }   @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,                          Bundle savedInstanceState) {      View view = inflater.inflate(R.layout.common_view_pager_layout, container, false);     ViewPager pager = (ViewPager) view.findViewById(R.id.MainViewerPage);      this._adapter = new MyPagerAdapter(getChildFragmentManager());     this._adapter.add(new DetailFragment());       pager.setAdapter(this._adapter);       return view; } } 

I test it and it works, any problem comment it.

Good Luck!



回答2:

Try to add this lines in your code:

public int getItemPosition(Object object) {    return POSITION_NONE; } 

and Add the below lines

pager.setOnPageChangeListener(new OnPageChangeListener() {      @Override      public void onPageSelected(int position) {           ... anything you may need to do to handle pager state ...           adapter.notifyDataSetChanged(); //this line will force all pages to be loaded fresh when changing between fragments      } 

Under

pager.setAdapter(adapter); 

Hope this may help you!



回答3:

In your Fragment class, in the onCreate() method, you have to call setRetainInstance(true) like this:

@Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);          setRetainInstance(true); } 

This tells the FragmentManager to keep your fragments on reloads. In addition, check your manifest file doesn't have any

android:screenOrientation 

config changes where you are basically saying you will handle reloads due to config changes yourself.



回答4:

Ensure you pass the adapter instance the child fragment manager, not the fragment manager.

Like this:

this._adapter = new PagerAdapter(getChildFragmentManager());



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!