public class MainActivity extends AppCompatActivity {
MovieDetailFragment movieDetailFragment;
MovieInfoFragment movieInfoFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movieDetailFragment = new MovieDetailFragment();
movieInfoFragment = new MovieInfoFragment();
getSupportFragmentManager().beginTransaction().add(R.id.container, movieInfoFragment).commit();
}
public void replaceMovieInfoFragment(){
getSupportFragmentManager().beginTransaction().replace(R.id.container, movieDetailFragment).addToBackStack(null).commit();
}
}
==============================================================================
public class MovieInfoFragment extends Fragment{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.movie_info_fragment, container, false);
ViewPager viewPager = rootView.findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(5);
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
ViewPager1 viewPager1 = new ViewPager1();
adapter.addItems(viewPager1);
ViewPager2 viewPager2 = new ViewPager2();
adapter.addItems(viewPager2);
ViewPager3 viewPager3 = new ViewPager3();
adapter.addItems(viewPager3);
ViewPager4 viewPager4 = new ViewPager4();
adapter.addItems(viewPager4);
ViewPager5 viewPager5 = new ViewPager5();
adapter.addItems(viewPager5);
viewPager.setAdapter(adapter);
return rootView;
}
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<Fragment> items = new ArrayList<Fragment>();
public void addItems(Fragment fragment) {
items.add(fragment);
}
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return items.get(position);
}
@Override
public int getCount() {
return items.size();
}
}
==============================================================================
public class ViewPager1 extends Fragment{
@Override
public void onDetach() {
super.onDetach();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.view_pager1, container, false);
Button button = rootView.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).replaceMovieInfoFragment();
}
});
return rootView;
}
}
============================================================================== If you press the button in ViewPager1 to move to movieDetailFragment and press Back, the new ViewPager will not be created in the movieInfo activity and a blank screen will appear. Is there a way to update or refresh Viewpager?
android-viewpager android
Similarly, I wrote a test code and tested it, but it works well without any problems.
Looking at your code, it seems that you are using android.app.Fragment
. Please use android.support.v4.app
package. Also, one suspicious part is
ViewPagerAdapter = newViewPagerAdapter(getFragmentManager()));
I think we should use getChildFragmentManager()
instead of getFragmentManager()
.
© 2024 OneMinuteCode. All rights reserved.