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
Related
Trying to implement circular view pager(Pagination). In the below code snippet, the setCurrentItem method of the Viewpageris set to first position(0th index), but it always moves to the second position(1st index).
Below is the code,
ViewPagerListener.java
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int positionOfView, float positionOffset, int positionOffsetPixels) {
LogUtil.d(TAG, "START of onPageScrolled");
mPosition = positionOfView;
LogUtil.d(TAG, "COMPLETION of onPageScrolled");
}
#Override
public void onPageSelected(int position) {
LogUtil.d(TAG, "START of onPageSelected");
//LogUtil.d(TAG, "Position :" + position + ", " + "Count of pager adapter :" + (mViewPager.getAdapter().getCount() - 1));
LogUtil.d(TAG, "COMPLETION of onPageSelected");
}
#Override
public void onPageScrollStateChanged(int state) {
LogUtil.d(TAG, "START of onPageScrollStateChanged");
if (state == ViewPager.SCROLL_STATE_DRAGGING && mPosition == 5) {
LogUtil.d(TAG, "trying to move to first");
currentItemInViewPager(0); //---> This is the position where I am setting the view to move back to start position(0th index)
}
LogUtil.d(TAG, "COMPLETION of onPageScrollStateChanged");
}
});
#Override
public void currentItemInViewPager(int index) {
LogUtil.d(TAG, "START of currentItemInViewPager");
if (index == 0) {
if (getChildFragmentManager().findFragmentByTag("TAG_INTRO_FRAGMENT") != null) {
LogUtil.d(TAG, "Removed");
getChildFragmentManager().beginTransaction().remove(getChildFragmentManager().findFragmentByTag("TAG_INTRO_FRAGMENT")).commit();
}
mViewPager.setVisibility(View.VISIBLE);
mTabLayout.setVisibility(View.VISIBLE);
mPagerAdapter = new TutorialPagerAdapter(getChildFragmentManager(), this);
mViewPager.setAdapter(mPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager, true);
}
mViewPager.setCurrentItem(index);
LogUtil.d(TAG, "COMPLETION of currentItemInViewPager");
}
Please let me know if I am missing out anything
Thanks
Before calling this method just call mViewPager.setCurrentItem(0);
If you want to keep the animation of paging u will need to override the pager instead or using a library as:
https://github.com/antonyt/InfiniteViewPager
Link to Reference:
ViewPager as a circular queue / wrapping
i am using
com.sothree.slidinguppanel
its working fine but on click listener is not working below is my code
slidingLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
slidingLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//my code
}
}
});
so its never going to the my code part i mean its not hitting the on click listener. please help
Because it's already handled in the Library:
/**
* Set the draggable view portion. Use to null, to allow the whole panel to
* be draggable
*
* #param dragView
* A view that will be used to drag the panel.
*/
public void setDragView(View dragView) {
if (mDragView != null) {
mDragView.setOnClickListener(null);
}
mDragView = dragView;
if (mDragView != null) {
mDragView.setClickable(true);
mDragView.setFocusable(false);
mDragView.setFocusableInTouchMode(false);
mDragView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!isEnabled() || !isTouchEnabled())
return;
if (mSlideState != PanelState.EXPANDED && mSlideState != PanelState.ANCHORED) {
if (mAnchorPoint < 1.0f) {
setPanelState(PanelState.ANCHORED);
} else {
setPanelState(PanelState.EXPANDED);
}
} else {
setPanelState(PanelState.COLLAPSED);
}
}
});
;
}
}
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
I have an activity that has 2 buttons (Previous, Next) and a text view in between the two. Below that is a viewpager.
I would like to change the textview to the title of whatever page they are on.
Here is the class where it all happens:
public class MyWorkout extends BaseActivity {
NonSwipeableViewPager vp;
TextView txtViewTitle;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_workout);
mToolBar = activateToolbar();
setUpNavigationDrawer();
getSupportActionBar().setTitle("My Workout");
Intent intent = getIntent();
String workouttitle = intent.getStringExtra("workout");
switch(workouttitle) {
case "w29" : {
getSupportActionBar().setTitle("My Workout - Workout 29");
break;
}
case "w30" : {
getSupportActionBar().setTitle("My Workout - Workout 30");
break;
}
}
Button btnPrev = (Button) findViewById(R.id.btnPrevious);
Button btnNext = (Button) findViewById(R.id.btnNext);
vp = (NonSwipeableViewPager) findViewById(R.id.view_pager);
txtViewTitle = (TextView) findViewById(R.id.txtViewTitle);
this.addPages(vp);
//THIS IS WHAT I HAVE TRIED
if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MoveNext();
}
});
btnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MovePrevious();
}
});
}
public void MoveNext() {
//it doesn't matter if you're already in the last item
vp.setCurrentItem(vp.getCurrentItem() + 1);
}
public void MovePrevious() {
//it doesn't matter if you're already in the first item
vp.setCurrentItem(vp.getCurrentItem() - 1);
}
private void addPages(NonSwipeableViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
Intent intent = getIntent();
String workout = intent.getStringExtra("workout");
Bundle args = new Bundle();
args.putString("workout", workout);
Week1Mon week1MonFragment = new Week1Mon();
week1MonFragment.setArguments(args);
adapter.addPage(week1MonFragment);
Week1Wed week1WedFragment = new Week1Wed();
week1WedFragment.setArguments(args);
adapter.addPage(week1WedFragment);
Week1Fri week1Fri = new Week1Fri();
week1Fri.setArguments(args);
adapter.addPage(week1Fri);
Week2Mon week2MonFragment = new Week2Mon();
week2MonFragment.setArguments(args);
adapter.addPage(week2MonFragment);
Week2Wed week2WedFragment = new Week2Wed();
week2WedFragment.setArguments(args);
adapter.addPage(week2WedFragment);
Week2Fri week2Fri = new Week2Fri();
week2Fri.setArguments(args);
adapter.addPage(week2Fri);
pager.setAdapter(adapter);
}
}
The part I have tried:
if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}
This sets the textview to Week One, Monday. Unfortunately it doesn't change when the user hits the Next button. I'm also hoping theres a better way, as I don't want to "assume" that the current item is that day of the week, because every week is different. I also need to figure out how to set the current page when the app is opened, because it saves the position is shared pref and I have to load it in the class above, and somehow set the page to a current page (preferably by the title)
Here is MyFragPagerAdapter:
public class MyFragPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> pages = new ArrayList<>();
public MyFragPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return pages.get(position);
}
#Override
public int getCount() {
return pages.size();
}
public void addPage(Fragment f) {
pages.add(f);
}
#Override
public CharSequence getPageTitle(int position) {
return pages.get(position).toString();
}
}
Thanks for any help, let me know if you need to see more code!
You can use,
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
// Check if this is the page you want.
}});
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);
}
});