Detect Fragment in ViewPager inside a Fragment of another ViewPager visible - android

For example, I have
MainActivity (contain a ViewPager)
Fragment0
Fragment1 (contain a ViewPager)
Fragment1a
Fragment1b
Fragment2
I want to refresh Fragment1a every time it visible so I use setUserVisibleHint inside Fragment1a but not working well.
When I switch between fragment Fragment1a and Fragment1b, setUserVisibleHint of Fragment1a called
However when I go from fragment Fragment0 to Fragment1 or Fragment2 to Fragment1 setUserVisibleHint of Fragment1a (and Fragment1b) not called
How can I detect Fragment1a (and Fragment1b) visible? Any help or suggestion would be great appreciated.

setUserVisibleHint also not working me I used another way to update data in fragment when view pager change fragment, Below is The way,
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
try {
if(viewPagerAdapter.getItem(position) instanceof Fragment1a){
Fragment1a frag1 = (Fragment1a) viewPagerAdapter.getItem(position);
frag1.updateData(data); // here is the public method of fragment which declared for udpate data in fragment
}else if(viewPagerAdapter.getItem(position) instanceof Fragment1b){
Fragment1b frag2 = (Fragment1b) viewPagerAdapter.getItem(position);
frag2.updateData(data); // here is the public method of fragment which declared for udpate data in fragment
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
viewpager.setCurrentItem(0);
This way you can update data directly Just you need to create one public method in your fragment.
Here is the Adapter
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<TabModel> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, TabModel title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position).getTabName();
}
}

Related

asyncTask from 3rd tab called in first tab in View pager fragment

i used view pager inside tab view i had 3 tabs in my activity. each tab having a fragment. i called asyncTask in OnCreateView of all tabs. everything is working fine. but the problem is comes when i come on first tab asynctask from all others 2nd tab or 3rd tab also gets called in first tab.here is my code snippet.
TabActivity :
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
FirstTab:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_current, container, false);
RequestNetworkcall();
return view;}
i am stuck on this problem from last 2 days lease help me guys.
Viewpager always initializes it's three pages, like it's visible.
So that means all your pages have already called their onResume() method.
You need to call your asynctask when that page is selected.
UPDATE:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 3) {
//Call your asynctask here
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Child's viewpager destroyed/getting invisible after parent viewpager's page slide

I've a ViewPager (Parent ViewPager) in my activity with 4 fragments. One of the fragments implements a sliding ViewPager (Child ViewPager) with automatic sliding.
When I swipe the parent ViewPager to, say, tab 3 and then I go back to the first fragment which contains the sliding ViewPager, some of the slide disappears and some of it stays. When I slide the ViewPager (the child ViewPager) back and forth couple of times, it gets restored to its original state.
I assume the fragment state of the sliding view pager is getting lost when the parent ViewPager is being slid.
Here's the parent ViewPager's adapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Here's the child ViewPager's adapter (slider):
class sliding_PageAdapters extends FragmentPagerAdapter {
int mNumOfTabs;
public sliding_PageAdapters(FragmentManager fm, int numTabs) {
super(fm);
this.mNumOfTabs = numTabs;
}
#Override
public void destroyItem(ViewGroup viewPager, int position, Object object) {
viewPager.removeView((View) object);
}
#Override
public Fragment getItem(int position) {
Bundle ar = new Bundle();
FragmentHomeImages fragment = new FragmentHomeImages();
switch (position) {
case 0:
ar.putString("number", "0");
fragment.setArguments(ar);
return fragment;
case 1:
ar.putString("number", "1");
fragment.setArguments(ar);
return fragment;
case 2:
ar.putString("number", "2");
fragment.setArguments(ar);
return fragment;
case 3:
ar.putString("number", "3");
fragment.setArguments(ar);
return fragment;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
The child ViewPager is being called with getChildFragmentManager() here:
sliding_pagerAdapter = new sliding_PageAdapters(getChildFragmentManager(), 4);
And the parent ViewPager is being called with getSupportFragmentManager() here:
adapter = new ViewPagerAdapter(getSupportFragmentManager());
I can think of one problem. On the parent viewpager call setOffscreenPageLimit(limit).
here limit is the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed. So set the appropriate limit based on your use-case.
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(fragmentManager));
viewPager.setOffscreenPageLimit(2);

Add new fragments to left and right side of viewpager

I'm trying to create a calendar application. Each month is one Fragment. I'm using ViewPager and FragmentPageAdapter. I want to add one Fragment when swiping to the left and position is 2 and I want to add one Fragment when swiping to the right and position is fragments.size() - 2.
I have no problem to add Fragment to the right but the problem occurs when adding to 0 position because of Can't change tag of fragment exception. I was looking to the solution to the problem with no success. I'm wondering if there is some alternative to ViewPager or another solution to this problem.
Here is my adapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
public void addFragment(Fragment fragment) {
fragments.add(fragment);
notifyDataSetChanged();
}
public void addFragment(Fragment fragment, int position) {
fragments.add(position, fragment);
notifyDataSetChanged();
}
public void removeFragment(int position) {
fragments.remove(position);
notifyDataSetChanged();
}
public void removeFragment(Fragment fragment) {
fragments.remove(fragment);
notifyDataSetChanged();
}
}

Save fragments state in ViewPager on screen rotation

I have TabLayout and ViewPager with 2 fragments, each of them is a recyclerView that load data from firebase.
All works fine. But when I change screen orientation ViewPager recreate each fragment and fragments load data again.
Is it possible and how to save fragment state in ViewPager?
But I do not want to override onSaveInstanceState method for creating data set and then restore it. I want something like find fragment by tag when check existing fragment.
Activity
public class MainActivity extends BaseBroadcastActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mViewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(MainListFragment.newInstance(connected, "Notices"), "Notices");
adapter.addFragment(MainListFragment.newInstance(connected, "Posts"), "Posts");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
In MainListFragment I only create recyclerView and load data from firebase.
Thank You!
EDIT
Thank You for responding!
I find my error. What's happen when we create FragmentPageAdapter at first time and when we recreate activity:
FragmentPageAdapter call instantiateItem method where it find fragment by tag and if fragment exists adapter use it, but if fragment does not exist adapter call getItem method and create fragment.
In my situation fragments are not recreated, but data is refreshing because I refresh them in onCreateView method in fragment, but now I am edit onCreateView:
if (savedInstanceState == null) {
retrieveData();
}
It should be done in the way that you return new instance always in getItem(). I would also rather use FragmentStatePagerAdapter especially when you have more pages.
You also need to save every internal state that can be lost in onSaveInstanceState(). If you don't want to reload, you'll need to save the data. If you have some data in POJOs, you can add Serializable interface to them to serialize them easily into bundle.
There is also setRetainInstance() on fragment, but it always caused some weird behavior for me, so I'm not using it.
static class Adapter extends FragmentStatePagerAdapter {
public Adapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position) {
case 0: fragment=MainListFragment.newInstance(mConnected, "Notices"); //don't forget to save mConnected in savedInstanceState
case 1: fragment=MainListFragment.newInstance(mConnected, "Posts");
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
String title="";
switch (position) {
case 0: title="Notices";
case 1: title="Posts";
}
return title;
}

Change dynamically Fragments in ViewPager

I have a problem while changing fragments in my viewpager. In specific, my fragment is replaced with an empty one.
Here's some code:
public void replaceFragment() {
supportManager.getFragments().set(1,getNewFragment()));
pageAdapter.notifyDataSetChanged();
menuControl.setView(1);//this just changes the fragment to be shown
}//here is where i change the fragment
//and here is the adapter
public class SimplePageAdapter extends FragmentPagerAdapter{
final List<Fragment> fragment;
final FragmentManager fragmentManager;
public SimplePageAdapter(FragmentManager fm, List<Fragment> fragment) {
super(fm);
fragmentManager = fm;
this.fragment=fragment;
}
#Override
public Fragment getItem(int arg0) {
return fragment.get(arg0);
}
#Override
public int getCount() {
return fragment.size();
}
#Override
public int getItemPosition(Object o) {
return POSITION_NONE;//I read that this is for making notifyDataSet work
}
}

Categories

Resources