Android FragmentPagerAdapter Circular listview - android

I want to create a circular listview in android, however I'm using/extending FragmentPagerAdapter and I want to check the item position in order to check the next item in circular listview.
I just want to develop a infinite listview which will repeat itself when the scroll ends.
My Code:
private class ListPageAdapter extends FragmentPagerAdapter {
private String[] items={"Item1","Item2","Item3","Item4", "Item5", "Item6", "Item7", "Item8"};
public ListPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment sampleFragment=new SampleFragment();
Bundle bundle=new Bundle();
bundle.putString("text","FragmentNumber"+i);
sampleFragment.setArguments(bundle);
return sampleFragment;
}
#Override
public int getCount() {
return items.length;
}
#Override
public CharSequence getPageTitle(int position) {
return items[position];
}
}
How I can make the Item1 after Item8 to make it a circular listview?

What about something like:
bundle.putString("text","FragmentNumber"+(i % items.length));
I didn't test it but I would say that it could work :D

Related

ImageButtons Remains unchanged when returned to an old view Pager fragment

I have a HomeActivity where I have used ViewPager. There are 2 fragments home1F and home2F that come when swipe right. The HomeActivity has a top bar (used by Linerlayout) where there are 2 imagebuttons. When home1F is in the screen one button is shown(other is invisible) and when home2F is in the screen the other button is shown while the first one becomes invisible.
But when I run the app I find at first the second button is visible instead of the first one (when there is home1F). When i swipe to home2F nothing happens. When I swipe left to come back to home1F still nothing happens.
Here is the code:
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0:
//imagebutton-1 visible
//imagebutton-2 invisible
return Home1F.newInstance("FirstFragment, Instance 1");
case 1:
//imagebutton-1 invisible
//imagebutton-2 visible
return Home2F.newInstance("SecondFragment, Instance 1");
default:
//imagebutton-1 visible
//imagebutton-2 invisible
return Home1F.newInstance("FirstFragment, Instance 1");
}}
#Override
public int getCount() {
return 2;
}
}
Try this
Show the imageButton1 and hide the imageButton2 in onCreate of your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
imag1.setVisibility(View.VISIBLE);
imag2.setVisibility(View.GONE);
So when your app launch it will show the first imageButton and hide the second imageButton
Now when you switch between tabs
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position==0){
imag1.setVisibility(View.VISIBLE);
imag2.setVisibility(View.GONE);
}
else if(position==1){
imag1.setVisibility(View.GONE);
imag2.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
if you want to switch out the actual fragments that are being displayed, you need to avoid FragmentPagerAdapter and use FragmentStatePagerAdapter.
using getItemPosition() we can update the fragments. In below example have updated fragment titles :
ViewPager pager = /* get my ViewPager */;
// assume this actually has stuff in it
final ArrayList<String> titles = new ArrayList<String>();
FragmentManager fm = getSupportFragmentManager();
pager.setAdapter(new FragmentStatePagerAdapter(fm) {
public int getCount() {
return titles.size();
}
public Fragment getItem(int position) {
MyFragment fragment = new MyFragment();
fragment.setTitle(titles.get(position));
return fragment;
}
public int getItemPosition(Object item) {
MyFragment fragment = (MyFragment)item;
String title = fragment.getTitle();
int position = titles.indexOf(title);
if (position >= 0) {
return position;
} else {
return POSITION_NONE;
}
}
});

Remove fragment in viewPager from fragment

I got a viewPager inside it I have several fragment.
providerViewPager = (ViewPager) findViewById(R.id.viewPager);
userRequest.add(1);
fragmentPagerAdapter = new FragmentStatePagerAdapter(HomeActivity.this.getSupportFragmentManager()) {
#Override
public int getCount() {
return userRequest.size();
}
#Override
public Fragment getItem(int position) {
return MyFragment.newInstance(position, userRequest.get(position));
}
};
providerViewPager.setAdapter(fragmentPagerAdapter);
userRequest is a arrayList of int, for each item in this list I create a MyFragment.
MyFragment contain the display view for each page.
view.findViewById(R.id.mybutton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewPager viewPager = ((HomeActivity)getActivity()).getViewPager();
FragmentStatePagerAdapter adapter = ((HomeActivity)getActivity()).getAdapter();
ArrayList<Integer> ints = ((HomeActivity)getActivity()).getRequest();
viewPager.removeView(view);
ints.remove(getArguments().getInt("position"));
adapter.notifyDataSetChanged();
}
});
This is a onClickListener who is inside my fragment, I would like to remove this current fragment when I'm click on it.
As you can see I get the viewPager, the adapter and the arrayList from my Activity, and I try some method to remove this fragment, this work but I got some bug, for exemple if I remove an element at the middle of the view Pager I can't reach the element at the end. I would like to know if they are something much simple for remove fragment.
I solve it with this :
public void removeUserRequest(int position) {
if (position > 0) {
removeViewOnSwipe(position - 1);
} else {
removeViewOnSwipe(position);
}
}
private void removeViewOnSwipe(int position) {
userRequest.remove(position);
if (userRequest.size() == 0) {
providerViewPager.setVisibility(View.GONE);
fragmentPagerAdapter = null;
} else {
fragmentPagerAdapter = new FragmentStatePagerAdapter(HomeActivity.this.getSupportFragmentManager()) {
#Override
public int getCount() {
return userRequest.size();
}
#Override
public Fragment getItem(int position) {
return ProviderSwipeListFragment.newInstance(position, userRequest.get(position));
}
};
providerViewPager.setAdapter(fragmentPagerAdapter);
providerViewPager.setCurrentItem(position);
}
}
removeUserRequest is the method call in the Fragment.
removeViewOnSwipe : we remove the item in the userRequest list. And after that I create a new Adapter. And finally set the correct new position with
providerViewPager.setCurrentItem(position);
I try do to it without recreat a new adapter with using notifyDataSetChanged instead, but I got some bug, the previous fragment can be visible if the user scroll a lot on the end of the swipe list

How To Start Second tab's Content By View pager Adaptor android

i am Suffering In Little problem .My Second Tab Of Fragment Is Not Performing The Task
my first tab is working
i m using View Page Adapter for Tab
Here Is Tutorial same way of this tutorial i used
but my second tab showing nothing
please help me ..
i will appreciate your suggestion
thanks in adavance
here is my view page adapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 2;
private String titles[] = new String[] { "Friends Request", "FriendsList" };
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
MyFriendsList fragmenttab1 = new MyFriendsList();
return fragmenttab1;
// Open Friendlist.java
case 1:
FriendsList fragmenttab2 = new FriendsList();
return fragmenttab2;
}
return null;
}
public CharSequence getPageTitle(int position) {
return titles[position];
}
#Override
public int getCount() {
return PAGE_COUNT;
}
}
in this my first tab of myfriendlist is working but Why Second tab Is not Calling It show blank page in frienlist
please help me

Viewpager with fragments

I have a viewpager in my app with 7 fragments, each represent their own logic
however, there are 2 fragments that are mutually coupled, one cannot exist without the other.
I have 2 buttons, previous and next at the bottom of the screen which switch between the fragments.
I want to make it so the swiping in the viewpager cannot reach one of the coupled fragments
Right now I did this: I put the first fragment (of the coupled ones) to be at location 0, and the 2nd fragment at the last position, so that when you swipe left and right you reach the 2nd coupled fragment last.
I want to make it so you can't reach the last fragment with swiping
how to do that ?
EDIT:
Fragment Adapter
public class PurchaseFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
private List<BaseFragment> fragments;
private boolean shouldShowLastFragment;
#Inject
public PurchaseFragmentStatePagerAdapter(FragmentManager fm) {
super(fm);
}
public void setFragments(List<BaseFragment> fragments) {
this.fragments = fragments;
}
#Override
public BaseFragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments == null ? 0 : shouldShowLastFragment ? fragments.size() : fragments.size()-1;
}
public void enableItem(int i) {
shouldShowLastFragment = true;
notifyDataSetChanged();
}
public void disableItem(int i) {
shouldShowLastFragment = false;
notifyDataSetChanged();
}
}
I assume you have either a FragmentStatePagerAdapter or a FragmentPagerAdapter.
In your adapter, setting the getCount() method to 6, will make sure that you cannot reach the last fragment.
#Override
public int getCount() {
return 6;
}

Dynamically change the title of a android.support.v4.view.PagerTitleStrip

I am currentky using a PagerTitleStrip in my application
(Doc: http://developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html)
Everything works fine, and I have set the title like this:
public class PageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public PageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
String[] titles = { "1", "2", "3", "4","5" };
#Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
My concern now, is that once the first Fragment has finished loading, I have to change the title of the strip.
I have already tried to get the FragmentPagerAdapter and tried to manipulate it, but never succeed to change a title (text)
There is indeed a getPageTitle method: mPagerAdapter.getPageTitle(position) but no setPageTitle ...
Any idea or suggestion?
If I understand problem correctly - you get data later and then want to update titles array and trigger refresh of titles.
If that's the case just trigger PagerAdapter.notifyDataSetChanged(); after that method is invoked Fragments will call getPageTitle method and update themselves.
Kind of ugly solution but working : iterate to the visual tree and set the text yourself.
Also, you have to be aware that there is a first transparent/empty item on the left.
Here is the code (Xamarin here) to update the first title.
var firstHidedOneFound = false;
for (int i = 0; i < _tabstrip.ChildCount; ++i)
{
var child = _tabstrip.GetChildAt(i) as TextView;
if (child == null) { continue; }
if (!firstHidedOneFound)
{
firstHidedOneFound = true;
continue;
}
child.Text = ViewModel.MyShowPanelTitle;
break;
}

Categories

Resources