I'm trying to use code from This Link creating multiple fragment wizard page. On the first fragment there is EditText and Next Button. On clicking Next Button, I'm disabling these controls and then navigating to 2nd fragment(page).
Now when I press back button using the below code
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
Fragment get changed back to first activity. But the EditText and Next Button are still disable.
I want to enable these button on back button clicking. But not getting any call in the overridden onResume(), onStart(), onViewStateRestored() function of FirstFragment class.
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 0) { // first fragment
// enable the views
}
if (position == 1) { // Second fragment
// disable the views
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Use the onPageSelected to disable and enable the buttons depending on the position you are at
Fragment is not actually paused or disabled when you go thru pager (depends on your nr. of active pages setting), it is still there. You should send event from your Adapter to Fragment when changing pages.
Related
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.
I have a viewpager and used fragmentpageradapter, now I have 3 fragments and in every fragment I need to make a http request (i.e, hit a api),
I don't want to hit all the api for 3 fragments at a time instead I require to hit the api only when the respective page is the current item visible.
I have already tried setting offscreenlimit, but it did not help. What else can I try?
viewPager.setOffscreenLimit(0);
This can be easily done by using OnPageChangeListener provided by ViewPager class
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// communicate with Fragment here
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
First remove the API calls from onCreateView of fragments, then use this event listener to check which page is selected, according to the selected position you can communicate to the fragment to call the required API.
Use this in your Fragment
private boolean hasBeenVisibleOnce = false;
#Override
public void setUserVisibleHint(boolean visible) {
super.setUserVisibleHint(true);
if (this.isVisible()) {
if (visible && !hasBeenVisibleOnce) {
new NetCheck().execute();
hasBeenVisibleOnce= true;
}
}
}
you can use setUserVisibleHint to check if a fragment is visible to the user. You want to add another boolean (called hasBeenVisibleOnce) to determine if the fragment has been visible at least once. This is to prevent the HTTP Request to be fired multiple times
Consider there is an activity with a title. Viewpager fragment is inside the activity. Fragment will be loaded with data from network API.
Now I need to update the activity title after getting the data from API.
Problem:
Since Viewpager is used, it loads the (prev), current, (next) fragments too. So activity is not aware of which title it is for.
Each title is different for each fragment, so I need to update the title only when the user views that fragment.
You need to create object of your Activity, By using this Object you are able to use activity method.
For more detail; visit this link.How to use method from another Class?
Use this with addOnPageChangeListener method of pager, in it you will get your current postion of pager.
In you Activity add the listener on ViewPager:
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
#Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
#Override public void onPageSelected(int position)
{
//You can use switch case too
if (position== 0)
{
//set title
}
else if (position== 1)
{
//set title
}...
}
#Override public void onPageScrollStateChanged(int state)
{
}
});
You can call the Activity method from Fragment by
Make method in Activity public
In fragment use getActivty() -> cast it to corresponding Activity then call this method
If you want to call Activity method only when fragment visible you can try https://stackoverflow.com/a/42019934/5381331
In Activity, you want to get the current visible fragment, you can try https://stackoverflow.com/a/47789367/5381331
I have Nested Fragments [1 main Fragment called "MainFrag" and many child Fragments]
the App can show MainFrag or can show other fragments.
what I want to do is to setCurrentItem of MainFrag's ViewPager every time the user selects to show that MainFrag so I tried to put it at it's onCreateView but this only works for the first time the User Shows MainFrag as if user Selects another fragment and then re-selects MainFrag the seCurrentItem is not working
I also tried to put it inside MainFrag's onResume, It worked as required but it also causes a problem that if the user moved the App to background and then reOpen it viewPager will Scroll and i don't want this
So where Exactly Can I put setCurrentItem ?
mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);
Edit: I am using Navigation Menu to Move between 3 main fragments where MainFrag is one of them.
After your mViewPager.setAdapter(adapter); method you can put
mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);
After setting adapter to viewpager add setCurrentItem method
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);
Try this:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
viewPager.setCurrentItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
I am using a ViewPager, which has two pages and it is being reused. The total count what i have is 156 pages. I want to disable left swipe for page 2.
Please help me out, thanks,
Here is my code..
public class SlidePagerActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setOnPageChangeListener(new ViewPageChangeListener(){
// set current slide index
AbcController.getInstance().getSlideController().setCurrentSlide(_nNextSlideIndex);
// set Current Cycle Index
AbcController.getInstance().getSlideController().setCurrentCycle(_nNextSlideCycle);
// set pager index
_mPager_in.setCurrentItem(_nNextSlideIndex);
});
}
}
I wanted to to prevent the user to go swipe back to previous page, but didn't want the page neither to disappear (to allow him to swipe back if some condition are fulfilled).
I finally use OnPageChangeListener:
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position < pageActuelle) {
viewPager.setCurrentItem( pageActuelle );
return;
}
pageActuelle = position;
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
Now I am forcing the user to go back to the his current page when he is swiping left. This solution was really adapted to my problem since I could display a Toast when the user tries to swipe back.
Hope it could help someone.
Can you specify your problem more detailed?
You can always remove all pages before the specific postion, in that case there is nothing to swipe too. (sorry cant comment)