I'm using scrollable tabs with FragmentPagerAdapter and I need to switch between the tabs programmatically
After clicking the create group button, I need to switch to the next tab i.e. manage group. How to do that?
For just moving 1 step ahead you can do:
int current = mPager.getCurrentItem();
mPager.setCurrentItem(current + 1, true);
If you want to scroll to any specific index you can just pass that too:
mPager.setCurrentItem(index, true);
You can also set smoothScroll flag as true/false which will give you smooth transition effect.
EDIT:
In your activity make a method :
public void scrollPager(int index) {
if (mPager != null) {
mPager.setCurrentItem(index, true);
}
}
From your fragment when you hit the button you can either create a call back or simply call activity's method:
((ActivityName) getActivity()).scrollPager(1);
Related
I'm setting a new android app, and I want to exploit the bottom navigation bar in my phone (previous,home) so that i don't have to create a customized one, I want to know if it is possible to set an onClickEvent on these buttons
You have to handle both button separately.
For hardware back button (Previous) you have to override onBackPress method in your Activity class. So, you will get the back press event you can change the behavior of back button if you don't want back event just remove super.onBackPress(). If you want to do this from fragment you just need to add your logic like you can get current fragment instance from FragmentManager like below and call fragment method what ever you want to do on that fragment.
\\ To get current fragment
\\ NOTE: I add back stack name as class name.
public static Fragment getCurrentFragment(FragmentManager fragmentManager) {
int count = fragmentManager.getBackStackEntryCount();
if (count > 0) {
FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1);
String tag = backStackEntry.getName();
return fragmentManager.findFragmentByTag(tag);
}
return null;
}
#Override
public void onBackPressed() {
// enter code here
super.onBackPressed(); // Remove this line if you don't want to go back.
}
For home button handling you can use this answer or this post.
Please comment me if you have more questions.
I have 3 fragments which is settled in TabLayout with ViewPager.
The Scenario is:
First fragment has one ImageView with some EditText and RadioButton with NEXT Button
Second fragment has 5 EditText with NEXT Button
Third Fragment has 4 ImageView and 4 EditText with SignUp Button
all fields are required.
Now what I have done:
Checked all validation on Button click of Fragment 1 and moved to NEXT fragment.
if (getActivity() != null) {
((RegisterActivity) getActivity()).mViewPager.setCurrentItem(1, true);
}
Same process
Same process and proceed for SignUp.
Problem:
How can I check values When I move to second fragment without clicking button (Sliding Viewpager or Clicking on TAB)
How can I update all final values (when I change it but do not click on button and move forward)
I have tried to use onAttach and onDetach for saving values but didn't worked.
Any solution for manage all data and check validation in each situation?
You can track of current selected tab by TabLayout.ViewPagerOnTabSelectedListener,
take a look here for more information docs
In general just implement this interface and then you will be notified when user swipe tabs or if you programmatically swipe you will still get notified.
Not the best solution but the workaround I have recently implemented.
Take boolean[] of total fragments.
like:
private boolean[] arrLastPageDone = new boolean[]{false, false, false};
If all values of fragment1 validated, set true on first position. Same for other fragments.
Simply check values in onPageSelected.
like:
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position > 0 && !arrLastPageDone[position - 1]) {
pager.setCurrentItem(pager.getCurrentItem() - 1);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
And it works. It dosen't let swipe to next fragment until all values validated.
And you can save those data to SingleTon Class until all process finished.
so i have a standard tabLayout that is in a viewpager.
But the very last tab needs to open a new activity. its only purpose is to open a new activity. and when the user returns from the activity by pressing the back button the last tab the user was at should be selected.
my tabs are custom views and there are 4 of them but the last one should be a button to trigger an event. so it will look just like a tab but really will be a button with a onclick listener. how can i acheive this ? i am wondering if a framelayout could be used and activated for the very last tab icon . thats the only way i can see to do it is to get the custom view and set its parent to be clickable that way it absorbs the click event. then i can set a onclick listener on the parent to open the activity. but then how to handle swipping to that tab ? swipping should open that activity as well.
Assuming you're using TabLayout.setupWithViewPager(), you can accomplish what you want by simply adding an extra tab and changing the OnTabSelectedListener.
// normal setup
tabs.setupWithViewPager(pager);
// our extra tab
tabs.addTab(tabs.newTab().setText("extra"));
// remove the `OnTabSelectedListener` created by `setupWithViewPager()`
tabs.clearOnTabSelectedListeners();
// add our own `OnTabSelectedListener`
tabs.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(pager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == pager.getAdapter().getCount()) {
// special case for the last tab in the list
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
else {
// otherwise, handle as normal
super.onTabSelected(tab);
}
}
});
Okay, so I have a FragmentPagerAdapter with 3 pages on it...
Is there a way I can HIDE a page from the SlidingTabLayout? I don't want to destroy the page, as I want to be able to unhide it later. Is there a way to do this? Or do I have to destroy the page, and add it back in later?
Hold all your pages in an Array (or a List).
boolean isHide;
public int getCount (){
if(isHide){
return container.size() - 1;
}
return container.size();
}
public Fragment getItem(int position) {
if(isHide && position == positionToHide){
return container.get(position + 1);
}
return container.get(position);
}
One solution would be to prevent transition to that fragment by hiding the tab button and/or disabling swipe.
fragment will still be loaded but you want be able to move to it.
I have a view pager with 2 scrolling pages in my app. at first I populate it with two fragments.
In first fragment I have a button. clicking the button new adapter is created and view pager is populated with two different fragments. at the moment when I press back I exit from the app instead I want to restore previous state of the view pager. please help
For the first time:
ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),nBank);
mViewpager.invalidate();
mViewpager.setAdapter(pagerAdapter);
Second time:
public void onListItemPressed(Currency objectCurrency) {
// TODO Auto-generated method stub
DetailPagerAdapter detaluriadapteri = new DetailPagerAdapter(getSupportFragmentManager());
mViewpager.setAdapter(detaluriadapteri);
}
One solution could be to implement onBackPressed:
#Override public void onBackPressed() {
if (mViewpager != null && mViewpager.getAdapter() instanceof DetailPagerAdapter) {
ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),nBank);
mViewpager.invalidate();
mViewpager.setAdapter(pagerAdapter);
} else {
super.onBackPressed();
}
}
Though, I think it would be better to start a new activity using DetailPagerAdapter when onListItemPressed is called. This way the default behavior of android would be to navigate back to your main activity on backpress, currently your main activity could be getting too much responsibility. Thus, having a self contained activity handling the details part would also be easier to maintain as it might need different tabs, actionbar menu items, etc..
Could also be a Fragment containing a the details viewpager but I have had trouble implementing this myself. Should be possible though and my trouble might be caused by some of the libraries I use.