Let's say I have two pages in my viewpager, is it any way to move from page 2 to page 1, but doing this like user is accesing page 3 (with all the animation)
You can achieve this by using a custom FragmentPagerAdapter that provides the same fragments over and over again:
private class EndlessPagerAdapter extends FragmentPagerAdapter {
private EndlessPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position % 2 == 0) {
return fragmentOne;
} else {
return fragmentTwo;
}
}
#Override
public int getCount() {
return Integer.MAX_VALUE;
}
}
Set the adapter for your ViewPager and start somewhere in the middle to allow almost endless swiping in either direction:
mViewPager.setAdapter(new EndlessPagerAdapter(getChildFragmentManager()));
mViewPager.setCurrentItem(Integer.MAX_VALUE/2);
Related
I'm developing an app that displays information about wifistate, battery and other stuffs. To do this, I'm using fragments and each one has It's own ListView showing the info. When I slide from a fragment to another one, Listviews make the UI lag causing a bad user experience. How could I avoid this?
Thank You in advance.
EDIT:
This is my Adapter code:
public class GenericFragmentPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> _fragments;
public GenericFragmentPagerAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
_fragments = fragments;
}
#Override
public int getCount() {
return _fragments.size();
}
#Override
public Fragment getItem(int position) {
return _fragments.get(position);
}
#Override
public String getPageTitle(int position) {
return ((ITitledFragment) _fragments.get(position)).getTitle();
}
}
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();
}
}
I have some sliders inside recyclerview among some other items. this is my pager adapter:
class PagerFragmentAdapter extends FragmentStatePagerAdapter {
public PagerFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
and i call setup on binding viewholder:
setup(RecyclerView.ViewHolder holder) {
pos = holder.getAdapterPosition();
if (pagerHolder == null) {
pagerHolder = (PagerHolder) holder;
viewPager = pagerHolder.getViewPager();
adapter = new PagerFragmentAdapter(fragmentManager);
viewPager.setAdapter(adapter);
pagerHolder.bindAdapter();
} else if(adapter!=null)
adapter.notifyDataSetChanged();
}
the first slider in recyclerview is OK but others are not OK!!
non of the fragments are loaded! and if i scroll it just shows first page of pager not all of them.
I have also tried creating fragments inside getItem() method but nothing changed..
I created a viewpager and an adapter with fragments that contains 2 views. I want to make the second view in the fragment invisible when I click on the frist one. I implemented this method on my view to handle the click events
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
ImageFragment fragment = (ImageFragment) ImageGallery.fragmentmanager.findFragmentByTag("android:switcher:"+R.id.imagerpager+":"+position);
if (fragment != null) {
fragment.setTransparency();
}
return super.onSingleTapConfirmed(e);
}
All fragments I get are null. To make sure that my objects are not null, following works but for the wrong(preloaded) fragments
ImageFragment fragment = (ImageFragment) ImageGallery.fragmentmanager.findFragmentById(R.id.imagepager);
I`m quite sure I have to implement a FragmentTransition to my adapter to make fragmentmanager.findFragmentByTag work. But I could not figure out how to do that. My adapter looks like
public class GalleryAdapter extends FragmentStatePagerAdapter {
private int mCount = pictureUrlList.size();
public GalleryAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ImageFragment fragment = ImageFragment.newInstance(position);
return fragment;
}
#Override
public int getCount() {
return mCount;
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Can anybody show me how to do that?
I have been struggling a few days with building functionality in my application that allows a user to remove a page/Fragment from a Viewpager, I have tried changing my FragmentPageAdapter to a FragmentStatePageAdapter and override my getItemPosition() to return POSITION_NONE; however this still does not allow me to remove the Fragment dynamically when I call fragments.remove(page);
pageAdapter.notifyDataSetChanged();
has anyone done this successfully before? seems like it should be really simple but it hasnt been, below is my fragment adapter, any help would go a long way thanks!
class MyPageAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public float getPageWidth(int position) {
if (position == 0) {
return (0.75f);
} else {
return (1f);
}
}
#Override
public CharSequence getPageTitle(int position) {
MyFragment theFragment = (MyFragment) this.fragments.get(position);
return theFragment.returnTitle();
}
#Override
public int getCount() {
return this.fragments.size();
}
}
Use Jake Wharton's ViewPagerIndicator. It also has an open source sample application with this funcionality (add and remove fragments from options menu).
Put this into you adapter:
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
And this is adding a page:
if (mAdapter.getCount() < 10) {
mAdapter.setCount(mAdapter.getCount() + 1);
mIndicator.notifyDataSetChanged();
}
This is how to remove a page:
if (mAdapter.getCount() > 1) {
mAdapter.setCount(mAdapter.getCount() - 1);
mIndicator.notifyDataSetChanged();
}