How to change ViewPager's page?
I'm not sure that I fully understand the question, but from the title of your question, I'm guessing that what you're looking for is pager.setCurrentItem( num )
. That allows you to programatically switch to another page within the ViewPager
.
I'd need to see a stack trace from logcat to be more specific if this is not the problem.
ViewPager set current page programmatically
In your PagerActivity use
viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
int pagei = position + 1;
pages=pagei + "";
Toast.makeText(PagerActivity.this, getString(R.string.changeinfopage) + " " + pages, Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
and
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_previous:
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
return true;
case R.id.action_next:
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
return true;
}
return super.onOptionsItemSelected(item);
}
How to change page in ViewPager Android?
How to change page in ViewPager Android?
Use ViewPager.setCurrentItem(int item) for changing ViewPager pages programmatically.
Where item
is index of Page which want to set as current item in ViewPager.
How to change page with button in viewPager?
The DialogFragmentContainer
is the parent fragment of the ViewPager page fragments; i.e. it's the parent of Step1 through Step4 fragments.
And therefore you can access it from Step1
fragment, the fragment that you want to change the current ViewPager page using the parentFragment
attribute:
binding.btnNext.setOnClickListener{
//I want to change fragment here
(parentFragment as DialogFragmentContainer).binding.pager.currentItem = myItemPosition
}
Change page on button click ViewPager Android
you get NPE
on line 71 @ MyFragment
, as you said in comment this line is:
mViewPager.setCurrentItem(1, true);
so mViewPager
is null, you need initialize that before using
use following code instead your code:
mViewPager.setCurrentItem(getItem(+1), true);
and getItem()
is:
private int getItem(int i) {
return mViewPager.getCurrentItem() + i;
}
Change the page number for viewPager
Here is an example of using dynamic radio button as an indicator in viewpager:
(this example from fragment, so just adjustment for your usage)
final RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.radiogroup);
final RadioButton[] rb = new RadioButton[items.length];
for (int i = 0; i < items.length; i++) {
final int j = i;
rb[i] = new RadioButton(rootView.getContext());
radioGroup.addView(rb[i]);
rb[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(items.length - j - 1);
}
});
}
How to control ViewPager change page in Fragment object
you can use eventbus notice viewpager to next page.
implement toNextPage func
void toNextPage() {
if (viewPager.getAdapter().getCount() >= viewPager.getCurrentItem() + 1) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
} else {
Toast.makeText(this, "is end", Toast.LENGTH_SHORT).show();
}
}
Change viewpager fragment by a buttonClick
I'm not sure what exactly you said about "Change".
If you're asking about change page display index, you can try
ViewPager.setCurrentItem(int pageIndex, boolean isSmoothScroll);
Or, if you're asking about change content, here's what I did
public class MyPagerAdapter extends FragmentStatePagerAdapter
{
private FragmentManager fragMan;
private ArrayList<Fragment> fragments=new ArrayList<Fragment>();
public void clearAll() //You can clear any specified page if you want...
{
for(int i=0; i<fragments.size(); i++)
fragMan.beginTransaction().remove(fragments.get(i)).commit();
fragments.clear();
fragments=new ArrayList<Fragment>();
notifyDataSetChanged();
}
public void addList() //Add some new fragment...
{
listFrag=new ListFragment();
fragments.add(list);
}
}
Hope it helps~
Related Topics
Retrofit Uploading Multiple Images to a Single Key
How to Fix "Failed to Sync Vcpu Reg" Error
Adding Tab Inside Fragment in Android
Change Checkbox Value Without Triggering Oncheckchanged
Glide-4.0.0 Missing Placeholder, Error, Glideapp and Does Not Resolve Its Method Placeholder,Error
The Application Icon Does Not Show on Action Bar
Calling Startintentsenderforresult from Fragment (Android Billing V3)
Android Slidingtablayout with Icons
How to Make Proguard Ignore External Libraries
Highlight Custom Listview Item When Long Click
Libgdx Spritebatch Render to Texture
How to Use Android Emulator for Testing Bluetooth Application
How to Get Add to Home Screen Pop Up on Site Open in Mobile Browser
Android - Print Full Exception Backtrace to Log
Best Way to Update Data with a Recyclerview Adapter
How to Use Scale Gesture Detector for Pinch Zoom in Android
Fragment Already Added Illegalstateexception
Android: 2 or More Expandablelistview Inside Navigation Drawer