I got a ViewPager which holds Fragments via FragmentStatePagerAdapter. Let's say the pager initially holds the following pages (Fragments):
A - B - C - D
When the user swipes, he can move from A to B, B to C etc.
But there are cases when the user changes some options on the A page, he can move not to B, but C:
A - C - D
Then the user goes back to A, modifies something and that re-enables B:
A - B - C - D
How can i achieve this very dynamic behavior? I cannot add Fragments any time when the user changes something and then re-populate the ViewPager, because it's slow and breaks the flow.
We too had the same situation, and we solved this issue by maintaining a boolean ArrayList.The pages here are fragments. On the Fragment, we had written an interface to update the ArrayList.This interface is implemented on the parent activity.On every swipe of the ViewPager we are retrieving the boolean ArrayList. On ViewPager, in setOnPageChangeListener we overrided the onPageSelected.
And here is the piece of code:
pageIndicator.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
pagesPageBreakInfoList = activityContext.getBooleanList();
currentPageNumber = position;
if (!pagesPageBreakInfoList.get(position)) {
if (currentPageNumber > previouspagenumber) {
previouspagenumber = currentPageNumber;
if (numberofSubmodule == (position + 1)) {
viewPager.setCurrentItem(position - 1);
} else {
viewPager.setCurrentItem(position + 1);
}
} else {
previouspagenumber = currentPageNumber;
viewPager.setCurrentItem(position - 1);
}
} else {
previouspagenumber = currentPageNumber;
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
Related
I want to know the current page position of the ViewPager. I create an adapter and set this adapter to the ViewPager. Now I want to know the current page number or position of the current view, because I want to start some extra events on that particular page.
I used viewPager.setCurrentItem(1); for setting the first item.
Is there a similar method for getting the current page?
in the latest packages you can also use
vp.getCurrentItem()
or
vp is the viewPager ,
pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);
you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
There is a method object_of_ViewPager.getCurrentItem() which returns the position of currently Viewed page of view pager
If you only want the position, vp.getCurrentItem() will give it to you, no need to apply the onPageChangeListener() for that purpose alone.
You will figure out that setOnPageChangeListener is deprecated, use addOnPageChangeListener, as below:
ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position == 1){ // if you want the second page, for example
//Your code here
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
For this problem Onpagechange listener is the best one But it will also have one small mistake that is it will not detect the starting time time of 0th position Once you will change the page it will starts to detect the Page selected position...For this problem I fount the easiest solution
1.You have to maintain the selected position value then use it....
2. Case 1: At the starting of the position is always Zero....
Case 2: Suppose if you set the current item means you will set that value into maintain position
3.Then do your action with the use of that maintain in your activity...
Public int maintain=0;
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
//Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
}
#Override
public void onPageSelected( int i) {
// here you will get the position of selected page
maintain = i;
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
updateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
data.set(maintain, "Replaced "+maintain);
myViewPager.getAdapter().notifyDataSetChanged();
}
});
getCurrentItem(), doesn't actually give the right position for the first and the last page I fixed it adding this code:
public void CalcPostion() {
current = viewPager.getCurrentItem();
if ((last == current) && (current != 1) && (current != 0)) {
current = current + 1;
viewPager.setCurrentItem(current);
}
if ((last == 1) && (current == 1)) {
last = 0;
current = 0;
}
display();
last = current;
}
The setOnPageChangeListener() method is deprecated. Use
addOnPageChangeListener(OnPageChangeListener) instead.
You can use OnPageChangeListener and getting the position inside onPageSelected() method, this is an example:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.d(TAG, "my position is : " + position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Or just use getCurrentItem() to get the real position:
viewPager.getCurrentItem();
There is no any method getCurrentItem() in viewpager.i already checked the API
In Oncreate of my MainActivity I add this code:
mDateAreaSlider.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// Hide right arrow if reach last position
if (arg0 == adapter.DateList.length - 1) {
mButtonNext.setVisibility(View.INVISIBLE);
mButtonBack.setVisibility(View.VISIBLE);
}
// Hide left arrow if reach first position
else if (arg0 == 0) {
mButtonBack.setVisibility(View.INVISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
// Else show both arrows
else {
mButtonBack.setVisibility(View.VISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
ifSingding = true;
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
and on the button click I add this code:
mDateAreaSlider.setCurrentItem(0);
but the problem is it can slide to the page I selected but does not make my arrows invisible.
NOTE: The 2 arrows are ImageViews in main layout,not inflated layout.So it is nothing to do with adapter.
Replace your original code with this:
final ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == adapter.DateList.length - 1) {
mButtonNext.setVisibility(View.INVISIBLE);
mButtonBack.setVisibility(View.VISIBLE);
}
// Hide left arrow if reach first position
else if (position == 0) {
mButtonBack.setVisibility(View.INVISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
// Else show both arrows
else {
mButtonBack.setVisibility(View.VISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
mDateAreaSlider.addOnPageChangeListener(onPageChangeListener);
Execute this code on your button click
mDateAreaSlider.setCurrentItem(0);
mDateAreaSlider.post(new Runnable() {
#Override
public void run() {
onPageChangeListener.onPageSelected(0);
}
});
I have four pages/screens in my view pager and I am using Circle page indicator for displaying the position of the screen. It works perfectly.
Now I need to add one more indicator circle even though the screen count is 4. Up on crossing the 4 the screen, I want to display another activity.
I have searched and found a solution for moving to another activity before the fake page (5th page is shown). Here is the code:
#Override
public Object instantiateItem(final ViewGroup container, int position) {
// TODO Auto-generated method stub
((ViewPager) container).setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// skip fake page (first), go to last page
if (position == 4) {
//instead of activity call, I am checking whether it's working or not
((ViewPager) container).setCurrentItem(0, false);
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
return super.instantiateItem(container, position);
}
But the problem is that, When I am doing this, the circle page indicator stops working.
Here is code for setting the adapter to circle page indicator:
mVpAdvertisement.setAdapter(mAdapter);
mCpIndicator.setViewPager(mVpAdvertisement);
mCpIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
#Override
public void onPageSelected(int position)
{
// on changing the pages during swipe
if(position==0)
{
}
}
#Override
public void onPageScrollStateChanged(int arg0)
{
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
});
I want to display 5 circles and upon swiping from 4th screen in view pager, I want application to display another activity, without showing the 5th screen (dummy screen) in view pager.
This is the library I am using: https://github.com/JakeWharton/Android-ViewPagerIndicator
I know this is too late, even the person posted the question got the answer also, but as it may help others to find the quick answer i am posting the answer.
Use two image(circle) for indicator one is for off and another for on. use frame layout to show the indicator. when user slide the viewPager just change the image. You can download the complete solutions which i have developed and working fine for me.
http://javaant.com/viewpager-with-circle-indicator-in-android
I used CommonsWare's ViewPager (With the same code) to make a ViewPager with 6 pages that shows the previous and the next page along the current one.
So here is how it looks: (first image) as you can see when I swipe the pager the first and the 4th page slide out/in from screen. (2nd image)
And here is what I want : ( I just want to fix the 1st and the 4th page's positions so the 2nd page goes on the first page)
(The position of first page is -1 and the 4th is 1 )
I just have problem with using SetTranslationX(float value) for "position >= 1" and "position <= -1"
What should I put in SetTranslationX(float value) to make fixed views ?
So I managed to do it this way :
By looking at JazzyViewPager's code, I used PositionOffsetpixels for setTranslationX() so here is how the code is look like :
pager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
OFFSET = positionOffsetPixels;
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
and in pagetransformer I did like this :
if (position <= -1) {
view.setTranslationX(OFFSET);
}else if (position >=1) {
float mTrans = -view.getWidth()-pager.getPageMargin()+OFFSET;
view.setTranslationX(mTrans);
It worked like a charm ...
I have a list that implementing both of PullToRefresh (the old one) and SwipeListView library.
Im following this to combining those library and this to use it in the Activity.
My list can do some basic functionalities like pullUp/pullDown and swipe left/right but i encountered some bug :
The item position in my listview is always started from 1, i believe it should be started from 0, so i need to decrease it in my methods.
When i swipe an item (say, the first item), the item 5th item will be swiped to. So the index+4 item will also be swiped.
The code i used to initialize the objects :
private PullToRefreshSwipeListView ptrListView;
private SwipeListView resultListView;
resultListView = ptrListView.getRefreshableView();
ptrListView.setOnRefreshListener(new OnRefreshListener2<SwipeListView>() {
#Override
public void onPullDownToRefresh(PullToRefreshBase<SwipeListView> refreshView) {
// TODO Auto-generated method stub
}
#Override
public void onPullUpToRefresh(PullToRefreshBase<SwipeListView> refreshView) {
// TODO Auto-generated method stub
}
});
This is the method i used to initialize the listview :
private void setListview() {
adapter = new LibraryAdapter(this, R.layout.item_library_list, new ArrayList<PurchasedItem>(), resultListView);
adapter.setListener(new LibraryListListener() {
//set the adapter
});
resultListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
//position di -1 karena sejak gabung library swipelistview + pulltorefresh, position slalu kelebihan 1 & menyebabkan OutOfBound error.
#Override
public void onClickFrontView(final int position) {
//do something here
}
#Override
public void onOpened(int position, boolean toRight) {
// TODO Auto-generated method stub
super.onOpened(position-1, toRight);
lastPos = position-1;
}
#Override
public void onMove(int position, float x) {
// TODO Auto-generated method stub
super.onMove(position-1, x);
}
#Override
public int onChangeSwipeMode(int position) {
// TODO Auto-generated method stub
return SwipeListView.SWIPE_MODE_DEFAULT;
}
#Override
public void onStartOpen(int position, int action, boolean right) {
// TODO Auto-generated method stub
super.onStartOpen(position-1, action, right);
}
});
resultListView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapter, View arg1,
final int pos, long arg3) {
//do something here
}
});
ptrListView.setAdapter(adapter);
ptrListView.setLongClickable(true);
resultListView.setSwipeOpenOnLongPress(false);
}
And this is my xml :
<com.handmark.pulltorefresh.library.PullToRefreshSwipeListView
xmlns:swipe="http://schemas.android.com/apk/res-auto"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#drawable/line_separator_repeat"
android:listSelector="#00000000"
ptr:ptrMode="pullFromEnd"
swipe:swipeActionLeft="reveal"
swipe:swipeBackView="#+id/back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeFrontView="#+id/front"
swipe:swipeMode="both" />
Please kindly help me, any help is appreciated. Thanks
I had the same first issue like you. i think the first item (index 0) is the header.
int _index = index - listView.getHeaderViewsCount();
I had never met your second problem. You can try this to solve touching problems :
listView.setOnScrollListener(listView.getRefreshableView().getTouchListener().makeScrollListener());
Hope this help
In order to fix problem no. 2, just add those both Override functions to your adapter:
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 500;
}