I have search a lot about this solution. I tried remove from mFragmentList and mFragmentTitleList directly. And I also tried call the destroyItem. Many methods have been tried. None of them worked. I have been stuck on this problem for few days. Is there Any answer could help? Thanks.
public final class OrdersViewPagerAdapter extends android.support.v4.app.FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public OrdersViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
if (position <= getCount()) {
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
}
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public void removeView(String title, ViewGroup view, TabLayout tabLayout) {
int index=-1;
int count=mFragmentTitleList.size();
for (int i=0;i<count;i++){
String mtitle=mFragmentTitleList.get(i);
if (mtitle.equals(title)){
index=i;
break;
}
}
if (index!=-1) {
mFragmentTitleList.remove(index);
tabLayout.removeTabAt(index);
destroyItem(view,index,mFragmentList.get(index));
notifyDataSetChanged();
}
}
}
Related
I created a dynamic tab layout using the tutorial from
https://github.com/jintoga/Dynamic-Viewpager-Custom-TabLayout
everything goes well and dynamic tab is created based on button clicks.
But i am able to close the table from the last tab. but when i tries to close a tab from somewhere middle it force closes SOMETIMES showing the error:
Process: com.example.dat.testdynamicviewpager, PID: 8156
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.unFocus(android.view.View)' on a
I uses this ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final ArrayList<Fragment> mFragmentList = new ArrayList<>();
private final ArrayList<String> mFragmentTitleList = new ArrayList<>();
Context context;
ViewPager viewPager;
TabLayout tabLayout;
public ViewPagerAdapter(FragmentManager manager, Context context, ViewPager viewPager,
TabLayout tabLayout) {
super(manager);
this.context = context;
this.viewPager = viewPager;
this.tabLayout = tabLayout;
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
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();
}
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);
CircleImageView tabItemAvatar =
(CircleImageView) view.findViewById(R.id.imageViewTabItemAvatar);
ImageButton remove = (ImageButton) view.findViewById(R.id.imageButtonRemove);
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Remove", "Remove");
removeFrag(position);
}
});
tabItemName.setText(mFragmentTitleList.get(position));
tabItemName.setTextColor(context.getResources().getColor(android.R.color.background_light));
tabItemAvatar.setImageResource(R.drawable.boy);
return view;
}
public void destroyFragmentView(ViewGroup container, int position, Object object) {
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
}
public void removeTab(int position) {
if (tabLayout.getChildCount() > 0) {
tabLayout.removeTabAt(position);
}
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
The error is showing at :
public void removeTab(int position) {
Log.e("POSITION", String.valueOf(position));
if (tabLayout.getChildCount() > 0 && tabLayout!=null) {
tabLayout.removeTabAt(position);
}
notifyDataSetChanged();
}
what is wrong here?
I found where the issue exists. example After creating 7 tabs (0-6 positions), And when I deleted 0th position tab then the total position remains 6 and unchanged) while if I delete the last tab say 6th position tab, then position changes to 5 (here no issue) ) so what can we do to solve this??
I wanted my view pager to call onCreate every time so am using the below code:
#Override
public void setUserVisibleHint(boolean isVisibleToUser)
{
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && isChanged())
{
isChanged = false;
getFragmentManager().beginTransaction().detach(this).attach(this).commit();
viewPagerUpdatedInterface.viewPagerUpdated(); // inteface called in main method this will adapter.notifyDataSetChanged();
}
}
Here I am able to see correct value of string which I needed to update in logs but the same is not showing on screen.
My adapter class is below:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Here's the inner class that contains fragments and changes fragment views on swipe. This class is defined in an Activity.
private class ViewPagerAdapter extends FragmentPagerAdapter {
private int count;
ViewPagerAdapter(FragmentManager fm, int count) {
super(fm);
this.count = count;
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new FriendsListFragment();
case 1:
Bundle bundle = new Bundle();
bundle.putString(Keys.TAG, ChatListFragment.class.getSimpleName());
ChatListFragment chatListFragment = new ChatListFragment();
chatListFragment.setArguments(bundle);
return chatListFragment;
case 2:
return new MoreFragment();
default:
return null;
}
}
#Override
public int getCount() {
return count;
}
}
Here I would like to add a code that sets tags to each fragment, so that I can recognize which fragment I'm at.
I searched quite much, and I haven't found a proper answer for this question, so please help.
You can override the setPrimaryItem method of your FragmentPagerAdapter.
private class ViewPagerAdapter extends FragmentPagerAdapter {
protected int currentPosition = -1;
protected Fragment currentFragment;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// ....
}
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
this.currentPosition = position;
if (object instanceof Fragment) {
this.currentFragment = (Fragment) object;
}
}
public int getCurrentPosition() {
return currentPosition;
}
public Fragment getCurrentFragment() {
return currentFragment;
}
}
Just use getCurrentPosition or getCurrentFragment methods to get information about current Fragment.
I'm working in an android projet. I use tab to display data with fragment. In the third fragment, when i update data, viewPager return to the first. I try everything.
This is my viewPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private int baseId=0;
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public long getItemId(int position) {
// give an ID different from position when position has been changed
return baseId + position;
}
/**
* Notify that the position of a fragment has been changed.
* Create a new ID for each position to force recreation of the fragment
* #param n number of items which have been changed
*/
public void notifyChangeInPosition(int n) {
// shift the ID returned by getItemId outside the range of all previous fragments
baseId += getCount() + n;
}
}
Thanks.
I have problem setting the title view pager by using PagerTitleStrip. I added a child view of PagerTitleStrip in view pager.
Where can I provide the title strings to apply to each page?
How can I set the page title in the Page Adapter class?
Override the getPageTitle method and supply your static array of pages string to it.
String [] titlesArray =
new String []{
page1,
page2,
page3
};
public CharSequence getPageTitle(int position) {
return titlesArray [position];
}
Here is full example.
You have to implement public CharSequence getPageTitle(int position).
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
#Override
public Fragment getItem(int position) {
if(position == 0){
return FragmentInner1.newInstance();
}
else {
return FragmentInner2.newInstance();
}
}
#Override
public CharSequence getPageTitle(int position) {
if(position == 0){
// public static final String PAGE_TITLE = "My Title #1" is within the layout FragmentInner1;
return FragmentInner1.PAGE_TITLE;
}
else {
// public static final String PAGE_TITLE = "My Title #2" is within the layout FragmentInner2;
return FragmentInner2.PAGE_TITLE;
}
}
}