Answer the question
In order to leave comments, you need to log in
ViewPager how to update?
There is a fragment, on it there is a viewpager. The viewpager has two fragments. Where on the second fragment is another viewpager (it also has 2 fragments). How to make it so that in the first viewpager on the first fragment, update the data for the second fragment (viewpager).
I use notifyDataSetChanged();
it, but it updates the entire adapter, but only a fragment is needed.
I did it this way so far. But this approach is not suitable, because it updates the entire adapter
public class Fragment_adapter_history extends Fragment {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
static final String TAG = "myLogs";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history_adapter, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
mViewPager = (ViewPager) view.findViewById(R.id.vpPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
Log.d(TAG, "onPageSelected, position = " + position);
mSectionsPagerAdapter.notifyDataSetChanged();
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
return view;
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public int getItemPosition (Object object) { return POSITION_NONE; }
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
FragmentTransaction fragmentTransaction;
fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
Fragment_history fragment_history = new Fragment_history();
fragmentTransaction.commit();
return fragment_history;
case 1:
FragmentTransaction fragmentTransaction2;
fragmentTransaction2 = getActivity().getSupportFragmentManager().beginTransaction();
Fragment_izbranoe fragment_izbranoe = new Fragment_izbranoe();
fragmentTransaction2.commit();
return fragment_izbranoe;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "История";
case 1:
return "Избранное";
}
return null;
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question