Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove a Fragment or layout #39

Closed
RaghuManickam opened this issue Dec 23, 2016 · 7 comments
Closed

Remove a Fragment or layout #39

RaghuManickam opened this issue Dec 23, 2016 · 7 comments

Comments

@RaghuManickam
Copy link

Hi,

I have removed a fragment dynamically from the Stepper adapter and i used notifyDataSetChanged(); to update Stepper adapter.

But it is not reflected back to StepperLayout.

Please let me know if there is any other method to remove view from stepper layout.

Thank you

@zawadz88
Copy link
Contributor

Hi @RaghuManickam,
How did you remove the Fragment exactly? Did you set a new adapter without this Fragment by using StepperLayout#setAdapter(stepAdapter)?

@RaghuManickam
Copy link
Author

Hi @zawadz88

I used this code in the adapter to remove the fragment

public class StepperAdapter extends AbstractStepAdapter {
private List mFragmentList = new ArrayList<>();

public StepperAdapter(FragmentManager fm) {
    super(fm);
}

@Override
protected Fragment createStep(int i) {
    return mFragmentList.get(i);
}

public void addFragment(Fragment fragment) {
    mFragmentList.add(fragment);
}

public void addFragment(Fragment fragment,int position) {
    mFragmentList.add(position,fragment);
    notifyDataSetChanged();
}


public void RemoveFragmnet(Fragment fragment) {
    mFragmentList.remove(fragment);
    notifyDataSetChanged();
}

public void RemoveFragmnet(int ID) {
    mFragmentList.remove(ID);
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return mFragmentList.size();
}

}

please review my code and kindly provide me feedback if any

@zawadz88
Copy link
Contributor

zawadz88 commented Jan 4, 2017

Hi,
You shouldn't really keep a list of Fragments in the FragmentAdaper yourself - FragmentPagerAdapter does that for you + destroys the unneeded fragments and recreates them when needed. Please see the sample app for an example.
Could you set a new adapter instead? Also, why do you need to remove the fragments from the adapter? What's your scenario?

@RaghuManickam
Copy link
Author

Hi @zawadz88 ,

The exact scenario, is we have a check box in first fragment according to the selection in the first fragment I have to hide or show the second fragment dynamically. the problem is i cannot recreate the whole fragment as i have 10 edittext above the check box and it is need to filled before the check box.

using this code i can remove the fragment but i am unable to notify the changes in stepper.

example 👍

1->2->3->4

no 2 is removed

1->3->4 this is not notified in stepper

@zawadz88
Copy link
Contributor

Hi @RaghuManickam,
Adding removing fragments is not really something this library supports currently and I have some doubts about doing so in the future. The reason for this is that this might misleading for the user if he already sees a Tab which is later removed or he saw 4 dots and now he has 3...
Having said that, you should probably do the same thing you would do with a regular ViewPager: http://stackoverflow.com/questions/9758799/how-to-remove-fragment-from-fragmentpageradapter

You might do the following:

  1. Add a flag to your adapter indicating whether the 2nd fragment should shown or not.
  2. Modify getCount() & createStep(..) methods accordingly.
  3. When you want to remove the fragment change the flag in the adapter.
  4. Remove the 2nd fragment:
    FragmentManager fm = stepperActivity.getSupportFragmentManager(); fm.beginTransaction() .remove(fm.findFragmentByTag("android:switcher:" + R.id.ms_stepPager + ":1")) .commitNow();
  5. Reset the adapter & views by calling:
    stepperActivity.mStepperLayout.setAdapter(stepAdapter, stepperActivity.mStepperLayout.getCurrentStepPosition());

The solution is not complete though & hacky. E.g. the user might reach the 4th step, then go back to the first one and unselect the checkbox. This would remove the rest of the fragments probably and all the data that was stored in them.

@zawadz88
Copy link
Contributor

Hi @RaghuManickam,
Does the point I raised in my previous comment make sense to you?

@RaghuManickam
Copy link
Author

Hi @zawadz88 ,

Sorry I was busy in other work . sorry for the delay.

I will check it and will update it . you can close this issue for now .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants