Remove Fragment From TabLayout and ViewPager - android

I want to delete a fragment and it's view from a tablayout when a checkbox is checked from a recyclerview. I have skipped the initializing codes and will share only the adapter codes (as that's where the work is) like below:
ViewpagerAdapter code:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private static final String TAG = ViewPagerAdapter.class.getSimpleName();
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> titlesList = new ArrayList<>();
private Context context;
private TabLayout tabLayout;
private ViewPager viewPager;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public ViewPagerAdapter(FragmentManager fm, Context ctx, ViewPager viewPager, TabLayout tabLayout) {
super(fm);
this.context = ctx;
this.viewPager = viewPager;
this.tabLayout = tabLayout;
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
public void addFragTab(Fragment fragment, String name){
fragmentList.add(fragment);
titlesList.add(name);
}
public void addFragLastPosition(Fragment fragment, String name){
fragmentList.add(fragmentList.size(), fragment);
titlesList.add(name);
}
public void removeFrag(int position){
removeTab(position);
Fragment fragment = fragmentList.get(position);
fragmentList.remove(fragment);
titlesList.remove(position);
destroyFragmentView(viewPager, position, fragment);
notifyDataSetChanged();
}
private void destroyFragmentView(ViewGroup container, int position, Object obj) {
FragmentManager manager = ((Fragment) obj).getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.remove((Fragment) obj).commit();
}
private void removeTab(int position) {
if (tabLayout.getChildCount() > 2){
tabLayout.removeTabAt(position);
}
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return titlesList.get(position);
}
}
and recyclerview adapter with checkbox to delete and add fragment:
public class PrefsAdapter extends RecyclerView.Adapter<PrefsCustomizeViewHolder> {
private static final String TAG = PrefsAdapter.class.getSimpleName();
private final Context context;
private List<Customize> itemsList;
private long id_nature, id_space;
private Customize cust;
private String nature, space;
private ViewPagerAdapter pagerAdapter;
public PrefsAdapter(Context context, List<Customize> itemsList) {
this.context = context;
this.itemsList = itemsList;
pagerAdapter = new ViewPagerAdapter(((AppCompatActivity)context).getSupportFragmentManager());
}
#Override
public PrefsCustomizeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.prefs_customize_items_layout, parent, false);
return new PrefsCustomizeViewHolder(view);
}
#Override
public void onBindViewHolder(final PrefsCustomizeViewHolder viewholder, final int position) {
final Customize customize = itemsList.get(position);
Picasso.with(context)
.load(customize.getIcon())
.fit()
.into(viewholder.prefsSelImg);
switch (position) {
case 0:
cust = new Select()
.from(Customize.class)
.where("tabName = ? ", Constants.NATURE)
.executeSingle();
if (cust != null) {
nature = cust.tabName;
Log.d(TAG, "Nature Tab From DB:\t" + nature);
id_nature = cust.getId();
Log.d(TAG, "Nature Tab Id from DB:\t" + id_nature);
viewholder.prefsCheckBox.setChecked(true);
} else {
viewholder.prefsCheckBox.setVisibility(View.INVISIBLE);
viewholder.prefsCheckBox.setChecked(false);
}
break;
case 1:
cust = new Select()
.from(Customize.class)
.where("tabName = ? ", Constants.SPACE)
.executeSingle();
if (cust != null) {
space = cust.tabName;
Log.d(TAG, "Space Tab From DB:\t" + space);
id_space = cust.getId();
Log.d(TAG, "Space Tab Id from DB:\t" + id_space);
viewholder.prefsCheckBox.setChecked(true);
} else {
viewholder.prefsCheckBox.setVisibility(View.INVISIBLE);
viewholder.prefsCheckBox.setChecked(false);
}
break;
}
viewholder.prefsSelImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (position) {
case 0:
if (viewholder.prefsCheckBox.isChecked()){
viewholder.prefsCheckBox.setChecked(false);
//Customize.delete(Customize.class, id_nature);
pagerAdapter.removeFrag(1); // not working
} else {
viewholder.prefsCheckBox.setChecked(true);
customize.setTabId(UUID.randomUUID().toString());
customize.setTabName(Constants.NATURE);
customize.save();
pagerAdapter.addFragTab(new NatureFrag(), Constants.NATURE); // not working too
viewholder.prefsCheckBox.setVisibility(View.VISIBLE);
}
break;
case 1:
if (viewholder.prefsCheckBox.isChecked()){
viewholder.prefsCheckBox.setChecked(false);
Customize.delete(Customize.class, id_space);
Log.d(TAG, "Deleted Space id:\t" + id_space); // prints right log detail
pagerAdapter.removeTab(new SpaceFragment(), Constants.SPACE);
} else {
viewholder.prefsCheckBox.setChecked(true);
customize.setTabId(UUID.randomUUID().toString());
customize.setTabName(Constants.NATURE);
customize.save();
pagerAdapter.addTab(new SpaceFragment(), Constants.SPACE);
viewholder.prefsCheckBox.setVisibility(View.VISIBLE);
}
break;
}
}
});
}
#Override
public int getItemCount() {
if (itemsList == null) {
return 0;
}
return itemsList.size();
}
}
The method to delete the tab doesn't work. Can anyone help out with this implementation or another way? Thanks.

Related

Interface in fragment in ViewPager.OnPageChangeListener

In my viewpager i set listener from which i call methods from my interface in tab fragments.
But variable in this tab fragments in my interfaces methods always return null.
Tell me why ?
p.s. sorry for my english)
Parent fragment:
public class MyFragment
extends Fragment {
protected Context mContext;
#Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}
#Override
public void onDetach() {
super.onDetach();
mContext = null;
} }
Container fragment with PagerAdapter:
public class FragmentContainer
extends MyFragment {
private ViewPager viewPager;
private ViewPager.OnPageChangeListener onPageChangeListener;
public interface IPageSelected {
void onPageSelected();
void onPageUnselected();
}
public static FragmentContainer newInstance() {
return new FragmentContainer();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.fragment_container, container, false);
viewPager = view.findViewById(R.id.pager);
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
onPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
for(int x = 0; x < pagerAdapter.getCount(); x++) {
((IPageSelected) pagerAdapter.getItem(x)).onPageUnselected();
}
((IPageSelected) pagerAdapter.getItem(position)).onPageSelected();
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
viewPager.addOnPageChangeListener(onPageChangeListener);
return view;
}
#Override
public void onResume() {
super.onResume();
setCurrentItem();
}
private void setCurrentItem() {
viewPager.post(() -> {
viewPager.setCurrentItem(0);
onPageChangeListener.onPageSelected(0);
});
}
class PagerAdapter
extends FragmentStatePagerAdapter {
PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TestFragment1.newInstance();
case 1:
return TestFragment2.newInstance();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab1";
case 1:
return "Tab2";
default:
return null;
}
}
} }
Tab fragments.
Question: why variable mContext in interface methods always return null ?
public class TestFragment1
extends MyFragment
implements FragmentContainer.IPageSelected {
public static TestFragment1 newInstance() {
return new TestFragment1();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
#Override
public void onPageSelected() {
Log.d("myLog", "TestFragment1 onPageSelected: " + mContext); // WHY NULL ???
}
#Override
public void onPageUnselected() {
Log.d("myLog", "TestFragment1 onPageUnselected: " + mContext); // WHY NULL ???
} }
public class TestFragment2
extends MyFragment
implements FragmentContainer.IPageSelected {
public static TestFragment2 newInstance() {
return new TestFragment2();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
#Override
public void onPageSelected() {
Log.d("myLog", "TestFragment2 onPageSelected: " + mContext); // WHY NULL ???
}
#Override
public void onPageUnselected() {
Log.d("myLog", "TestFragment2 onPageUnselected: " + mContext); // WHY NULL ???
} }
In your fragment container :
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager()); here is the fault.
In order to attach your child fragments to your container fragment, consider passing child fragment manager getChildFragmentManager() to your viewpager adapter. (Right now your fragments are attached to same level as your container fragment).
I found solution.
Adapter class.
Old:
class PagerAdapter
extends FragmentStatePagerAdapter {
PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TestFragment1.newInstance();
case 1:
return TestFragment2.newInstance();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab1";
case 1:
return "Tab2";
default:
return null;
}
}
}
New:
class PagerAdapter
extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Adapter initialization.
Old:
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
viewPager.setAdapter(pagerAdapter);
New:
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
pagerAdapter.addFragment(TestFragment1.newInstance(), "tab1");
pagerAdapter.addFragment(TestFragment2.newInstance(), "tab2");
viewPager.setAdapter(pagerAdapter);
And now new question: what's the differense with the first option ?
Now i initializing adapter in container fragment, it's a cause ?
Why didn't work when initializing was in the adapter ?

Remove Tab Fragment from ViewPager and TabLayout Android

I have an activity that hosts tablayout with viewpager and fragments. I have successfully setup the tablayout and viewpager with user-populated values from a local database like below code(trying to be brief, showing only one tab here) -
public void setUpTabs() {
setUpViewPager(viewPager);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setupWithViewPager(viewPager);
}
private void setUpViewPager(ViewPager viewPager) {
Customize customize = new Select()
.from(Customize.class)
.where("tabName = ? ", Constants.NATURE)
.executeSingle();
if (customize != null) {
String nature = customize.tabName;
Log.d(TAG, "Nature Tab Name:\t" + nature);
}
pagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
pagerAdapter.addTab(new Top30Fragment(), Constants.Top30); // default tab
if (nature != null) {
pagerAdapter.addTab(new NatureFragment(), Constants.NATURE); // dynamic tab from db
}
pagerAdapter.addTabAtLastPosition(new PrefsFragment(), Constants.PREFS);
viewPager.setAdapter(pagerAdapter);
}
Customize is model class, I have used ActiveAndroid library for persistence. Here's my tabsadapter class code:
public class TabsPagerAdapter extends FragmentPagerAdapter {
private static final String TAG = TabsPagerAdapter.class.getSimpleName();
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> titlesList = new ArrayList<>();
private ViewPager viewPager;
private TabLayout tabLayout;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
public void addTab(Fragment fragment, String name){
fragmentList.add(fragment);
titlesList.add(name);
}
public void addTabAtLastPosition(Fragment fragment, String name){
fragmentList.add(fragmentList.size(), fragment);
titlesList.add(name);
}
public void removeTab(Fragment fragment, String name){ //Not working
for (int i = 0; i < fragmentList.size(); i++){
if (fragmentList.get(i).equals(fragment)){ // never enters/returns from this loop
Log.d(TAG, "Fragment at position:\t" + i);
fragmentList.remove(fragment);
titlesList.remove(name);
}
}
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return titlesList.get(position);
}
}
In my preferences fragment(also part of the tablayout), I have a recyclerview with checkboxes, case1:if checked, the tab is checked in db and only added to tablayout if not already there. case2:Otherwise if unchecked, means the tab is in layout and db and has to be removed from both.
I have started working on case 2 but I'm only able to delete values from db, the tab to be deleted is still present in tablayout. Here's my recyclerview adapter code to delete the tab:
public class PrefsAdapter extends RecyclerView.Adapter<PrefsCustomizeViewHolder> {
private static final String TAG = PrefsAdapter.class.getSimpleName();
private final Context context;
private List<Customize> itemsList;
private long id_nature, id_space, id_seasons, id_art, id_scifi, id_misc;
private Customize cust; // Model class for saving and retrieving tabs
private TabsPagerAdapter pagerAdapter;
private String nature, space, seasons, art, scifi, misc;
private String tabNature, tabSpace, tabSeasons, tabArt, tabScifi, tabMisc;
public PrefsAdapter(Context context, List<Customize> itemsList) {
this.context = context;
this.itemsList = itemsList;
pagerAdapter = new TabsPagerAdapter(((AppCompatActivity)context).getSupportFragmentManager());
}
#Override
public PrefsCustomizeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.prefs_customize_items_layout, parent, false);
return new PrefsCustomizeViewHolder(view);
}
#Override
public void onBindViewHolder(final PrefsCustomizeViewHolder viewholder, final int position) {
final Customize customize = itemsList.get(position);
Picasso.with(context)
.load(customize.getIcon())
.fit()
.into(viewholder.prefsSelImg);
// search db to find if checkbox is checked, i.e tab is present or not and update recyclerview
switch (position) {
case 0:
cust = new Select()
.from(Customize.class)
.where("tabName = ? ", Constants.SPACE)
.executeSingle();
if (cust != null) {
space = cust.tabName;
Log.d(TAG, "Space Tab From DB:\t" + space);
id_space = cust.getId();
Log.d(TAG, "Space Tab Id from DB:\t" + id_space);
viewholder.prefsCheckBox.setChecked(true);
} else {
viewholder.prefsCheckBox.setVisibility(View.INVISIBLE);
viewholder.prefsCheckBox.setChecked(false);
}
break;
}
viewholder.prefsSelImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (position) {
case 0:
if (viewholder.prefsCheckBox.isChecked()){
viewholder.prefsCheckBox.setChecked(false);
Customize.delete(Customize.class, id_space);
Log.d(TAG, "Deleted Space id:\t" + id_space); // prints right log detail
pagerAdapter.removeTab(new SpaceFragment(), Constants.SPACE);
} else {
viewholder.prefsCheckBox.setChecked(true);
customize.setTabId(UUID.randomUUID().toString());
customize.setTabName(Constants.NATURE);
customize.save();
pagerAdapter.addTab(new SpaceFragment(), Constants.SPACE);
viewholder.prefsCheckBox.setVisibility(View.VISIBLE);
}
break;
}
}
});
}
#Override
public int getItemCount() {
if (itemsList == null) {
return 0;
}
return itemsList.size();
}
}
The tabspagerAdapter.removeTab() never gets called even though I have initialized it in the constructor like below:
public PrefsAdapter(Context context, List<Customize> itemsList) {
this.context = context;
this.itemsList = itemsList;
pagerAdapter = new TabsPagerAdapter(((AppCompatActivity)context).getSupportFragmentManager());
}
Can someone help me out with removing the fragment and also why the pagerAdapter.removeTab(fragment, title) method never gets called and how to update the tab layout. Thanks.
I have used the following code to successfully change the number of tabs (both add and remove). I found I had to remove them from both the pager adapter and the tab layout.
int cnp = mPagerAdapter.getCount();
// remove all tabs (or just the ones you need to remove)
// from both the pager adapter and the tab layout
for(int i = cnp-1; i >= 0; --i) {
mPagerAdapter.destroyItem(mViewPager, i, mPagerAdapter.getItem(i));
mTabLayout.removeTabAt(i);
}
// add back in whatever tabs you need and update the pager adapter to be consistent
mTabLayout.addTab(mTabLayout.newTab().setText("Tab 0"),0);
mTabLayout.addTab(mTabLayout.newTab().setText("Tab 1"),1);
mPagerAdapter.setConfiguration(2); // my custom method to tell it which pages to show
mTabLayout.invalidate();
mPagerAdapter.notifyDataSetChanged();
Relevant class members:
ViewPager mViewPager;
TabLayout mTabLayout;
MyPagerAdapter mPagerAdapter; // extends FragmentPagerAdapter

Not getting current selected Tab position in android tablayout

I am creating a Tab based application where the user can create tabs by clicking a button and can delete the tabs by clicking another button. I implemented this using Tab layout, Fragments and Viewpager.
I am able to get the current tab position when new tab is created by the function setuptablyout function. But when i clicked another tab ie moving to other tabs I am not able to toast the position.
I found that using tabLayout.addOnTabSelectedListener I can get the position, while that doesn't work here.
my Activity Code is:
public class MainActivity extends AppCompatActivity{
TextView textView;
private TabLayout tabLayout;
public static ViewPager viewPager;
public static ViewPagerAdapter adapter;
private ImageView add;
int selectedTabPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.my_viewpager);
tabLayout = (TabLayout) findViewById(R.id.my_tab_layout);
add= (ImageView) findViewById(R.id.imageButtonAdd1);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), MainActivity.this, viewPager, tabLayout);
viewPager.setAdapter(adapter);
createFirstFragment("POS "+adapter.getCount());
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Toast.makeText(getApplicationContext(),"Pos onTabSelected: "+ tabLayout.getSelectedTabPosition(),Toast.LENGTH_SHORT).show();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(adapter.getCount() > 4){
Toast.makeText(MainActivity.this, "Only 5 Tabs allowed", Toast.LENGTH_SHORT).show();
}
else {
Bundle bundle = new Bundle();
bundle.putString("data", "POS "+adapter.getCount());
FragmentChild fragmentChild = new FragmentChild();
fragmentChild.setArguments(bundle);
adapter.addFrag(fragmentChild, "POS "+adapter.getCount());
adapter.notifyDataSetChanged();
if (adapter.getCount() > 0) tabLayout.setupWithViewPager(viewPager);
Log.e("adapter.getCount()",String.valueOf(adapter.getCount()));
viewPager.setCurrentItem(adapter.getCount() - 1);
setupTabLayout();
}
}
});
}
public void createFirstFragment(String pagename) {
Bundle bundle = new Bundle();
bundle.putString("data", pagename);
FragmentChild fragmentChild = new FragmentChild();
fragmentChild.setArguments(bundle);
adapter.addFrag(fragmentChild, pagename);
adapter.notifyDataSetChanged();
if (adapter.getCount() > 0) tabLayout.setupWithViewPager(viewPager);
Log.e("adapter.getCount()",String.valueOf(adapter.getCount()));
viewPager.setCurrentItem(adapter.getCount() - 1);
setupTabLayout();
}
public void setupTabLayout() {
selectedTabPosition = viewPager.getCurrentItem();
Toast.makeText(MainActivity.this, "selectedTabPosition --> "+selectedTabPosition, Toast.LENGTH_SHORT).show();
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setCustomView(adapter.getTabView(i));
}
}
}
ViewPager class:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> mFragmentList = new ArrayList<>();
private final ArrayList<String> mFragmentTitleList = new ArrayList<>();
Context context;
ViewPager viewPager;
TabLayout tabLayout;
int selectedTabPosition;
PreferenceHelper prefs;
private Map<Integer, String> mFragmentTags;
private FragmentManager mFragmentManager;
private boolean doNotifyDataSetChangedOnce = false;
public ViewPagerAdapter(FragmentManager manager, Context context, ViewPager viewPager,
TabLayout tabLayout) {
super(manager);
this.context = context;
this.viewPager = viewPager;
this.tabLayout = tabLayout;
mFragmentTags = new HashMap<Integer, String>();
prefs = new PreferenceHelper(context);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
if (doNotifyDataSetChangedOnce) {
doNotifyDataSetChangedOnce = false;
notifyDataSetChanged();
}
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void removeFrag(int position) {
removeTab(position);
Fragment fragment = mFragmentList.get(position);
mFragmentList.remove(fragment);
mFragmentTitleList.remove(position);
//destroyFragmentView(viewPager, position, fragment);
notifyDataSetChanged();
Log.e("getCount()", String.valueOf(getCount()));
if (getCount() > 0)
tabLayout.setupWithViewPager(viewPager);
setupTabLayout();
}
public View getTabView(final int position) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_tab_item, null);
TextView tabItemName = (TextView) view.findViewById(R.id.textViewTabItemName);
ImageView tabItemAvatar =
(ImageView) view.findViewById(R.id.imageViewTabItemAvatar);
ImageButton remove = (ImageButton) view.findViewById(R.id.imageButtonRemove);
if(getCount() == 1){
remove.setVisibility(View.INVISIBLE);
}
else{
remove.setVisibility(View.VISIBLE);
}
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Remove", "Remove");
if(getCount() != 1){
removeFrag(position);
}
else{
// Toast.makeText("Atleast One Tab is requied")
}
}
});
tabItemName.setText(mFragmentTitleList.get(position));
tabItemName.setTextColor(context.getResources().getColor(android.R.color.background_light));
tabItemAvatar.setImageResource(R.drawable.boy);
return view;
}
public void setupTabLayout() {
selectedTabPosition = viewPager.getCurrentItem();
Toast.makeText(context, "selectedTabPosition --> "+selectedTabPosition, Toast.LENGTH_SHORT).show();
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setCustomView(getTabView(i));
}
}
public void removeTab(int position) {
if (tabLayout.getChildCount() > 0 && tabLayout!=null) {
tabLayout.removeTabAt(position);
prefs.remove("POS"+position);
}
}
/*#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
if (mCurrentFragment != object) {
mCurrentFragment = (Fragment) object;
}
super.setPrimaryItem(container, position, object);
}*/
#Override
public Object instantiateItem(ViewGroup container, int position) {
Object object = super.instantiateItem(container, position);
if (object instanceof Fragment) {
Fragment fragment = (Fragment) object;
String tag = fragment.getTag();
mFragmentTags.put(position, tag);
}
return object;
}
public Fragment getFragment(int position) {
Fragment fragment = null;
String tag = mFragmentTags.get(position);
if (tag != null) {
fragment = mFragmentManager.findFragmentByTag(tag);
}
return fragment;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I am getting this weird behaviour that whenever I create new tabs the onTabSelected functions are calling multiple time that even when I create 3rd tab onTabslected toast shows Pos onTabSelected: 0,1 and not the just 2nd position which is supposed to be the 2nd position. What can I do to solve this?
My intention is to get some unique tab id so that I can identify each tab separately
you just create the newInstance constructor for creating fragment with arguments
in your FragmentChild
public static FragmentChild newInstance(int page, String title) {
FragmentChild fragmentChild = new FragmentChild ();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentChild.setArguments(args);
return fragmentChild;
}
// Store instance variables based on arguments passed
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
tvLabel.setText(page + " -- " + title);
return view;
}
And then after in adapter class change this.
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
to
#Override
public Fragment getItem(int position) {
return FragmentChild.newInstance(position, "Page # 1");
}
I have not tested this code but, I think this code will help you to get your unique tab id..
test this code and let me know if it works

Refresh a fragment inside a viewpager which is inside a fragment

I have an activity which consists of three fragments. In one of these fragments I have a viewpager that consists of three other fragments. I call getChildFragmentManager() inside fragment that has viewpager in it. I have searched SO and read almost every answer that are related to this topic for example: this and this and one that seemed most useful was this but could not solve my problem. I think if i can just return POSITION_NONE for getItemPosition(Object object) in FragmentStatePagerAdapter and call notifyDataSetChanged() will solve my problem but every time I do this my app crashes with this error:
java.lang.IllegalStateException: FragmentManager is already executing
transactions!!!
and I mentioned that I am using getChildFragmentManager() inside my fragment.
Anyone thinks of any solution to this problem?
this is the parent fragment:
public class WordDetailFragment extends Fragment {
#BindView(R.id.word_parts_tab_layout)
TabLayout mTabLayout;
#BindView(R.id.word_part_view_pager)
ViewPager mViewPager;
private WordPagerAdapter mPagerAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_word_detail, container, false);
ButterKnife.bind(this, view);
mPagerAdapter = new WordPagerAdapter(getChildFragmentManager(), getActivity());
mViewPager.setAdapter(mPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);
return view;
}
public void notifyTranslateChanged() {
mViewPager.getAdapter().notifyDataSetChanged();
}
}
this is the code for adapter
public class WordPagerAdapter extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 3;
private String[] tabTitles = new String[]{"Definition", "Example", "Col & fam"};
private Context context;
public WordPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case 0:
fragment = new WordDefinitionFragment();
break;
case 1:
fragment = new WordExampleFragment();
break;
case 2:
fragment = new WordColFamFragment();
break;
default: fragment = new WordDefinitionFragment();
}
return fragment;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
One of the fragments in viewpager
public class WordDefinitionFragment extends Fragment {
#BindView(R.id.show_translate_switch)
Switch mShowTranslateSwitch;
private List<WordInfo> mWordInfoList;
private List<Definition> mDefinitionList;
private boolean mShowTranslation;
private View mView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_word_definition, container, false);
ButterKnife.bind(this, mView);
mShowTranslation = SharedPrefHelper.getBooleanInfo(getActivity(), getString(R.string.show_translate_key));
mShowTranslateSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (getActivity() != null)
((WordActivity) getActivity()).changeTranslateMode(b);
showTranslation(b);
}
});
return mView;
}
}
and my activity. U have just included the parts that i felt are needed here.
public class WordActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceSate) {
super.onCreate(savedInstanceSate);
setContentView(R.layout.activity_word);
}
public void changeTranslateMode(boolean show) {
SharedPrefHelper.setBooleanInfo(this, getString(R.string.show_translate_ke)y, show);
((WordDetailFragment)mWordDetailFragment).notifyTranslateChanged();
}
}
and my logcat error is
After many searches I could figure out a way which is not very smart but for the time being has solved my problem.
First of all I used the class FixedFragmentStatePagerAdapter from this post. I also had to remove the part in which the fragment bundle is restored because it caches fragment variables and views.
public abstract class FixedFragmentStatePagerAdapter extends PagerAdapter {
private static final String TAG = "PagerAdapter";
private static final boolean DEBUG = false;
private final FragmentManager mFragmentManager;
private FragmentTransaction mCurTransaction = null;
private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<>();
private ArrayList<String> mSavedFragmentTags = new ArrayList<>();
private ArrayList<Fragment> mFragments = new ArrayList<>();
private Fragment mCurrentPrimaryItem = null;
public FixedFragmentStatePagerAdapter(FragmentManager fm) {
mFragmentManager = fm;
}
/**
* Return the Fragment associated with a specified position.
*/
public abstract Fragment getItem(int position);
public String getTag(int position) {
return null;
}
#Override
public void startUpdate(ViewGroup container) {
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// If we already have this item instantiated, there is nothing
// to do. This can happen when we are restoring the entire pager
// from its saved state, where the fragment manager has already
// taken care of restoring the fragments we previously had instantiated.
if (mFragments.size() > position) {
Fragment f = mFragments.get(position);
if (f != null) {
return f;
}
}
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
Fragment fragment = getItem(position);
String fragmentTag = getTag(position);
if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment + " t=" + fragmentTag);
/* if (mSavedState.size() > position) {
String savedTag = mSavedFragmentTags.get(position);
if (TextUtils.equals(fragmentTag, savedTag)) {
Fragment.SavedState fss = mSavedState.get(position);
if (fss != null) {
fragment.setInitialSavedState(fss);
}
}
}*/
while (mFragments.size() <= position) {
mFragments.add(null);
}
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
mFragments.set(position, fragment);
mCurTransaction.add(container.getId(), fragment, fragmentTag);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
Fragment fragment = (Fragment)object;
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object
+ " v=" + ((Fragment)object).getView() + " t=" + fragment.getTag());
while (mSavedState.size() <= position) {
mSavedState.add(null);
mSavedFragmentTags.add(null);
}
mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
mSavedFragmentTags.set(position, fragment.getTag());
mFragments.set(position, null);
mCurTransaction.remove(fragment);
}
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
Fragment fragment = (Fragment)object;
if (fragment != mCurrentPrimaryItem) {
if (mCurrentPrimaryItem != null) {
mCurrentPrimaryItem.setMenuVisibility(false);
mCurrentPrimaryItem.setUserVisibleHint(false);
}
if (fragment != null) {
fragment.setMenuVisibility(true);
fragment.setUserVisibleHint(true);
}
mCurrentPrimaryItem = fragment;
}
}
#Override
public void finishUpdate(ViewGroup container) {
if (mCurTransaction != null) {
mCurTransaction.commitAllowingStateLoss();
mCurTransaction = null;
mFragmentManager.executePendingTransactions();
}
}
#Override
public boolean isViewFromObject(View view, Object object) {
return ((Fragment)object).getView() == view;
}
#Override
public Parcelable saveState() {
Bundle state = null;
if (mSavedState.size() > 0) {
state = new Bundle();
Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
mSavedState.toArray(fss);
state.putParcelableArray("states", fss);
state.putStringArrayList("tags", mSavedFragmentTags);
}
for (int i=0; i<mFragments.size(); i++) {
Fragment f = mFragments.get(i);
if (f != null) {
if (state == null) {
state = new Bundle();
}
String key = "f" + i;
mFragmentManager.putFragment(state, key, f);
}
}
return state;
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
if (state != null) {
Bundle bundle = (Bundle)state;
bundle.setClassLoader(loader);
Parcelable[] fss = bundle.getParcelableArray("states");
mSavedState.clear();
mFragments.clear();
ArrayList<String> tags = bundle.getStringArrayList("tags");
if (tags != null) {
mSavedFragmentTags = tags;
} else {
mSavedFragmentTags.clear();
}
if (fss != null) {
for (int i=0; i<fss.length; i++) {
mSavedState.add((Fragment.SavedState)fss[i]);
}
}
Iterable<String> keys = bundle.keySet();
for (String key: keys) {
if (key.startsWith("f")) {
int index = Integer.parseInt(key.substring(1));
Fragment f = mFragmentManager.getFragment(bundle, key);
if (f != null) {
while (mFragments.size() <= index) {
mFragments.add(null);
}
f.setMenuVisibility(false);
mFragments.set(index, f);
} else {
Log.w(TAG, "Bad fragment at key " + key);
}
}
}
}
}
}
Then I implemented ViewPager.OnPageChangeListener in the fragment that contains the viewpager. And in the void onPageScrollStateChanged(int state) method I updated the view of my fragments in viewpager.
public class WordDetailFragment extends Fragment implements ViewPager.OnPageChangeListener {
#BindView(R.id.word_parts_tab_layout)
TabLayout mTabLayout;
#BindView(R.id.word_part_view_pager)
ViewPager mViewPager;
private WordPagerAdapter mPagerAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_word_detail, container, false);
ButterKnife.bind(this, view);
mPagerAdapter = new WordPagerAdapter(getChildFragmentManager(), getActivity());
mViewPager.setAdapter(mPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(this);
return view;
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
Fragment mFragment;
#Override
public void onPageSelected(int position) {
mFragment = mPagerAdapter.getItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
if (mFragment instanceof WordDefinitionFragment) {
((WordDefinitionFragment) mFragment).showTranslation();
//Log.e("StateChanged", mFragment.getClass().getSimpleName());
}
if (mFragment instanceof WordExampleFragment) {
((WordExampleFragment) mFragment).showTranslation();
//Log.e("StateChanged", mFragment.getClass().getSimpleName());
}
if (mFragment instanceof WordColFamFragment) {
((WordColFamFragment) mFragment).showTranslation();
//Log.e("StateChanged", mFragment.getClass().getSimpleName());
}
}
}
}
And in my fragments, I had a callback interface which was implemented in the activity. And in void setUserVisibleHint(boolean isVisibleToUser) method I updated the UI again because neighbor fragments did not get updated.
public class WordExampleFragment extends Fragment {
#BindView(R.id.example_field_name_id)
TextView mExampleFieldNameTV;
#BindView(R.id.word_tv)
TextView mWordTv;
#BindView(R.id.play_sound_iv)
ImageView mPlaySoundIv;
#BindView(R.id.show_translate_switch)
Switch mShowTranslateSwitch;
private TextView mExample1ContentFaTV;
private List<WordInfo> mWordInfoList;
private List<Example> mExampleList;
private int mWordNumber;
//callback interface
private TranslateModeChangeListener mWordPartFragments;
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mWordPartFragments = (TranslateModeChangeListener) context;
} catch (ClassCastException ex) {
throw new ClassCastException("Must implement WordPartFragments interface");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_word_example, container, false);
ButterKnife.bind(this, view);
mWordInfoList = WordActivity.sWordInfoList;
mWordNumber = WordActivity.sWordNumber;
mExampleList = mWordInfoList.get(mWordNumber).getExamples();
showTranslation();
mShowTranslateSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//this method is called from activity=> callback interface
mWordPartFragments.changeTranslateMode(b);
showTranslation();
}
});
return view;
}
//this method is called when viewpager shows this fragment and it is visible to user
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
showTranslation();
}
}
//this methoud updates my UI: shows or hides the translation
public void showTranslation() {
if (isAdded()) {
boolean showTranslation = WordActivity.sTranslationShown;
//Log.e("Ex", "added & shown = " + showTranslation);
mShowTranslateSwitch.setChecked(showTranslation);
mExample1ContentFaTV.setVisibility((showTranslation) ? View.VISIBLE : View.INVISIBLE);
}
}
}

Cardview with viewpager inside recyclerview item

I have tried a lot to implement a CardView which inside has a ViewPager (with fragments) and a TabLayout but RecyclerView shows only the first item and not the others.
Here is my ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int tabCount;
public ViewPagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
this.tabCount= tabCount;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
default:
return null;
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return tabCount;
}
}
Here is my TimeSheetAdapter:
public class TimeSheetAdapter extends RecyclerView.Adapter<TimeSheetAdapter.ViewHolder> {
Context context;
ArrayList<TimeSheet> timeSheetArrayList;
public TimeSheetAdapter(Context context, ArrayList<TimeSheet> timeSheetArrayList) {
this.context = context;
this.timeSheetArrayList = timeSheetArrayList;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
public ViewHolder(View itemView) {
super(itemView);
tabLayout = (TabLayout) itemView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) itemView.findViewById(R.id.pager);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager);
}
}
public TimeSheetAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View viewHolder = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_sheet_row, parent, false);
return new TimeSheetAdapter.ViewHolder(viewHolder);
}
#Override
public void onBindViewHolder(TimeSheetAdapter.ViewHolder holder, int position) {
ViewPagerAdapter adapter = new ViewPagerAdapter(((AppCompatActivity) context).getSupportFragmentManager(), holder.tabLayout.getTabCount());
holder. viewPager.setAdapter(adapter);
if (holder.tabLayout != null) {
for (int i = 0; i < holder.tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = holder.tabLayout.getTabAt(i);
if (tab != null)
tab.setText("Tab " + Integer.toString(i + 1));
}
}
holder.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(holder.tabLayout));
}
#Override
public int getItemCount() {
return timeSheetArrayList.size();
}
}
The key is to assign unique ids to each ViewPager using setId() method, even they are inside rows of the RecyclerView.
Update ViewPagerAdapter as follows:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private String[] tabTitles = new String[] {"Tab 1", "Tab 2"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
Update TimeSheetAdapter as follows:
public class TimeSheetAdapter extends RecyclerView.Adapter<TimeSheetAdapter.ViewHolder> {
private Context context;
private ArrayList<TimeSheet> timeSheetArrayList;
public TimeSheetAdapter(Context context, ArrayList<TimeSheet> timeSheetArrayList) {
this.context = context;
this.timeSheetArrayList = timeSheetArrayList;
}
public TimeSheetAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View viewHolder = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_sheet_row, parent, false);
return new TimeSheetAdapter.ViewHolder(viewHolder);
}
#Override
public void onBindViewHolder(TimeSheetAdapter.ViewHolder holder, int position) {
ViewPagerAdapter adapter = new ViewPagerAdapter(((AppCompatActivity) context).getSupportFragmentManager());
holder.viewPager.setAdapter(adapter);
holder.tabLayout.setupWithViewPager(holder.viewPager);
holder.viewPager.setOffscreenPageLimit(2);
}
#Override
public int getItemCount() {
return timeSheetArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TabLayout tabLayout;
ViewPager viewPager;
public ViewHolder(View itemView) {
super(itemView);
tabLayout = itemView.findViewById(R.id.tabLayout);
viewPager = itemView.findViewById(R.id.pager);
// Assign a unique id so that ViewPager is shown.
// Following method is an example and can be unreliable.
viewPager.setId((int) (Math.random() * 10000));
}
}
}
And MainActivity should be as follows:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<TimeSheet> timeSheetArrayList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
TimeSheetAdapter adapter = new TimeSheetAdapter(this, timeSheetArrayList);
recyclerView.setAdapter(adapter);
fetchData(); // Populate timeSheetArrayList
}
...
}

Categories

Resources