choman choman - 5 months ago 17
Android Question

Can't remove fragment from its arraylist

I have a program that has fragment as arraylist, I am using it with TabLayout , As I remove a tab I want to remove the fragment also. But with my implementation it is not sequential. Tab 1 will remove Fragment2 etc. Please check where I am going wrong ?

Under onCreate of MainActivity this is how I add fragments

private TabPagerAdapter pagerAdapter;


public List<Fragment> fragmentList = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {

fragmentList.add(NewsFragment.newInstance(Tab1));
fragmentList.add(NewsFragment.newInstance(Tab2));
fragmentList.add(NewsFragment.newInstance(Tab3));

mViewPager = (ViewPager) findViewById(R.id.pager);

pagerAdapter = new TabPagerAdapter(getSupportFragmentManager(), fragmentList);
mViewPager.setAdapter(pagerAdapter);


This is my FragmentStatePagerAdapter, removeFragment is where I am trying to remove the fragment

public class TabPagerAdapter extends FragmentStatePagerAdapter {


private List<Fragment> mFragmentList;
private List<String> tabTitles = new ArrayList<>();


@Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}

@Override
public Fragment getItem(int position) {

return mFragmentList.get(position);
}

public TabPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
mFragmentList = fragmentList;
}

@Override
public int getCount() {

return mFragmentList.size();
}

public void removeFragment(int tabPosition) {

if (!mFragmentList.isEmpty()) {
mFragmentList.remove(tabPosition);

}
}

}


OnActivity result under mainactivity is where I am trying to remove both the tab & fragment

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK || data == null)
return;
settingList = (ArrayList<SettingCheckBox>) data.getSerializableExtra(SETTING_CHECK_BOX);
for (int i = 0; i < settingList.size(); i++) {
if (settingList.get(i).getChecked()) {
Log.d("**Checked Item**", String.valueOf(settingList.get(i).getDescription()));

String removeTab = String.valueOf(settingList.get(i).getDescription());
Boolean checkedValue = settingList.get(i).getChecked();


if (removeTab.equals("tab1")) {
//remove tab
tabLayout.removeTab(tab1);
//remove fragment
pagerAdapter.removeFragment(0);
// fragmentList.remove(i);
pagerAdapter.notifyDataSetChanged();


} else if (removeTab.equals("tab2")) {

tabLayout.removeTab(tab2);
pagerAdapter.removeFragment(1);
// fragmentList.remove(i);

pagerAdapter.notifyDataSetChanged();


} else if (removeTab.equals("tab3")) {

tabLayout.removeTab(tab3);
pagerAdapter.removeFragment(2);
// fragmentList.remove(i);

pagerAdapter.notifyDataSetChanged();
}

Answer

Just to answer my own question. I added the fragment this way

public List<Fragment> fragmentList = new ArrayList<>(); 
private NewsFragment frag1 
frag1 = NewsFragment.newInstance("https://www.yahoo.com");                  
fragmentList.add(frag1); 

In case I need to find the position of the fragment

for (int i = 0; i < fragmentList.size(); i++) { 
if (fragmentList.get(i).equals(frag1)) {
//found the position so removing it 
pagerAdapter.removeFragment(i); } 
}
Comments