How can I make my arrows invisible by setCurrentItem method of Viewpager? - android

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);
}
});

Related

How to get position of first and last position of viewpager?

I want to hide left arrow when the first position of a ViewPager and hide right arrow when the last position of a ViewPager. And then , I also want to show current page number with TextView like (5/30). 5 is current page number and 30 is total page number. Thank You Sir.
Here is my code;
mPager.beginFakeDrag();
leftNav = (ImageButton) findViewById(R.id.left_nav);
rightNav = (ImageButton) findViewById(R.id.right_nav);
txt_count = (ImageButton) findViewById(R.id.txt);
int tab = mPager.getCurrentItem();
if (tab == 0){
leftNav.setVisibility(View.GONE); // hide all position :(
}
leftNav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int tab = mPager.getCurrentItem();
if (tab > 0) {
tab--;
mPager.setCurrentItem(tab);
} else if (tab == 0) {
mPager.setCurrentItem(tab);
}
}
});
rightNav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int tab = mPager.getCurrentItem();
tab++;
mPager.setCurrentItem(tab);
}
});
You can use :
viewPager.getCurrentItem()
ViewPager.getAdapter().getCount() The getAdapter() method returns the object that supplies the pages for the ViewPager.
Add onPageChangeListener as shown below
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == (mPager.getAdapter().getCount() - 1)) {
rightNav.setVisibility(View.GONE);
} else {
rightNav.setVisibility(View.VISIBLE);
}
if (position == 0) {
leftNav.setVisibility(View.GONE);
} else {
leftNav.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Your Click listener for left & right arrow buttons
leftNav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mPager.getAdapter() != null && mPager.getAdapter().getCount() != null && mPager.getCurrentItem() != 0) {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
});
rightNav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mPager.getAdapter() != null && mPager.getAdapter().getCount() != null && mPager.getCurrentItem() != (mPager.getAdapter().getCount() - 1)) {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
}
});
To get all page count in ViewPager, use getAdapter method of the viewPager.getAdapter()
viewPager.getAdapter().getCount()
To find out current page of ViewPager use ViewPager.getCuurentItem()
viewPager.getCurrentItem()
Use OnPageChangeListener
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public void onPageSelected(int position) {
if(position == 0)
{
// hide left nav
}
else if(position == mPager.getAdapter().getCount() - 1) // is last position
{
// hide right nav
}
else
{
// show all nav
}
}
}
Reference
https://developer.android.com/reference/android/support/v4/view/ViewPager.html

MainActivity Get Position Of Item From PagerAdapter [duplicate]

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

stoping the SwipeMenuLIstView to Swipe

i am using Swipe menu list View from the Githu
https://github.com/baoyongzhang/SwipeMenuListView
According to my project requirement i need to stop some list View to Swipe
for example i want to stop the list item on the postion 5
how i would do that
mListView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
#Override
public void onSwipeStart(int position) {
if (positon == 5) {
Toast.makeText(getApplicationContext(), "you are not Allow to Swipe", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onSwipeEnd(int position) {
}
});
You can create a private instance of the listener and than set it to the mListView if a condition is met and if you want to not allow the swiping to be active you can simply set the onSwipeListener to null like this:
private SwipeMenuListView.OnSwipeListener swipeListener = new SwipeMenuListView.OnSwipeListener() {
#Override
public void onSwipeStart(int i) {
}
#Override
public void onSwipeEnd(int i) {
}
});
if (condtion) {
Toast.makeText(getApplicationContext(), "you are not Allow to Swipe", Toast.LENGTH_SHORT).show();
//disable swiping
mListView.setOnSwipeListener(null);
}else{
//allow swiping
mListView.setOnSwipeListener(swipeListener);
}
you can use BaseSwipListAdapter and change condition according your requirments.
#Override
public boolean getSwipEnableByPosition(int position) {
if(position==5){
return false;
}
return true;
}

some bugs appeared when combining PullToRefresh and SwipeListView library

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;
}

android 2.3.3 and 4.x seems have different effect when run animation

My problem is:
I customized a view as below JPTabBarMainMenu, set it as part of a listview header. in JPTabBarMainMenu there are two buttons, animation will runs when I click one of the two buttons.
the problem is the animation runs very well on android 2.3.3, but won't run on 4.0.3 and 4.0.4, I guess it won't work on 4.x. I don't know the reason.
JPTabBarMainMenu extends FrameLayout{
code as below:
onAnimationStart/onAnimationRepeat/onAnimationEnd won't getting called.
FrameLayout.LayoutParams params = (LayoutParams) mSlider.getLayoutParams();
JPTranslateAnimation slideRightAnimation = new MaringTranslateTranslateAnimation(params.leftMargin, tabItem.getLeft() + offset / 2, 0, 0, mSlider);
slideRightAnimation.setDuration(slideRightAnimation.computeDuration(
JPTranslateAnimation.ANIMATION_STD_DURATION, mTabLayout.getWidth()));
slideRightAnimation.setRepeatCount(0);
slideRightAnimation.setFillAfter(true);
slideRightAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
isRunningAnimation = true;
if(mSelectionChangedListener != null){
mSelectionChangedListener.onSelectionChangedStart(oldIndex, mCurrentTab);
}
if(oldIndex != -1){
TabItem item = mTabItems.get(oldIndex);
item.onTabItemSelected(false);
}
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
isRunningAnimation = false;
if(mSelectionChangedListener != null){
mSelectionChangedListener.onSelectioinChangedEnd(oldIndex, mCurrentTab);
}
TabItem item = mTabItems.get(mCurrentTab);
item.onTabItemSelected(true);
if(mDrawSelectorByDefault == false){
mSlider.setVisibility(View.INVISIBLE);
}
}
});
if(mSlider.getVisibility() != View.VISIBLE){
mSlider.setVisibility(View.VISIBLE);
}
mSlider.startAnimation(slideRightAnimation);

Categories

Resources