(If someone needs more information, or a better description let me know)
Hello i included the viewPagerLibrary from here: http://viewpagerindicator.com/#introduction today in my project.
No i get a really strange problem:
If i add a site or page (let’s call it site in the next few lines) and remove it again everything is ok. But if i try to add a different page (Those pages are different Fragements which implements a BaseFragment class) the content of the first page is shown.
The same thing happens if i add a few pages and delete one inbetween those pages. The page which was after the deleted page shows now the deleted pages content.
An example of this bug:
The problem now is. If i add FragmentA after that FragmentB, then i delete FragmentA, FragmentB gets the view/content of FragmentA. The strange thing is, the object is the correct one (So the adapter return the correct object) and the Title is also the correct one.
In my main i create my Pager, Indicator and Adapter this way:
Cfg.mAdapter = new FragmentAdapter(getSupportFragmentManager());
Cfg.mPager = (ViewPager)findViewById(R.id.pager);
Cfg.mPager.setAdapter(Cfg.mAdapter);
Cfg.mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
Cfg.mIndicator.setViewPager(Cfg.mPager);
//We set this on the indicator, NOT the pager
Cfg.mIndicator.setOnPageChangeListener(TabHelper.onPageChangeListener);
(The Cfg is a static file to store those things for the usage)
My BaseFragment looks like the following:
public class BaseFragment extends Fragment{
public static int FILE_FRAGMENT = 0;
public static int FTP_FRAGMENT = 1;
public static int ADDFTP_FRAGMENT = 2;
public static int PREVIEW_FRAGMENT = 3;
public static int CSS_FRAGMENT = 4;
public static int BOOKS_FRAGMENT = 5;
public static int SNIPPETS_FRAGMENT = 6;
//private int id;
private int typ;
private String title;
public int getTyp() {
return typ;
}
public void setTyp(int typ) {
this.typ = typ;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
One of the Fragments looks like this (I think the other fragments make no difference):
public class FtpFragment extends BaseFragment {
private static RowLayout rowLayout_view;
public FtpFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
init_data();
}
public static void init_data()
{
//Remove child for update
rowLayout_view.removeAllViews();
List<FtpData> ftps = FtpStorage.getInstance().getFtps();
if (ftps != null) {
for (FtpData f : ftps) {
View inflatedView;
inflatedView = View.inflate(Cfg.ctx, R.layout.ftp, null);
inflatedView.setOnClickListener(button_ftp_listener);
inflatedView.setOnLongClickListener(button_ftp_longClickListener);
inflatedView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, Converter.convertFromDPtoPixel(160.0f)));
inflatedView.setTag(f);
inflatedView.findViewById(R.id.book_imageview).setBackgroundDrawable(
Cfg.ctx.getResources().getDrawable(R.drawable.nopreview));
((TextView) inflatedView.findViewById(R.id.book_textview)).setText(f.nickname);
rowLayout_view.addView(inflatedView);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_ftp, container, false);
rowLayout_view = (RowLayout) v.findViewById(R.id.rowLayout_ftps);
return v;
}
@Override
public String getTitle() {
return "FTPs";
}
@Override
public int getTyp() {
return BaseFragment.FTP_FRAGMENT;
}
@Override
public void setTyp(int typ) {
super.setTyp(typ);
}
}
To remove or add a page i call this:
public static void addNewTab(BaseFragment fragment)
{
Cfg.mAdapter.addItem(fragment);
Cfg.mPager.setCurrentItem(Cfg.mAdapter.getCount());
Cfg.mIndicator.notifyDataSetChanged();
}
public static void deleteActTab()
{
Cfg.mAdapter.removeItem(Cfg.mAdapter.getActPage());
Cfg.mIndicator.notifyDataSetChanged();
}
And that’s the adapter:
public class FragmentAdapter extends FragmentPagerAdapter implements TitleProvider{
public List<BaseFragment> fragments = new LinkedList<BaseFragment>();
private int actPage;
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
public void setActPage(int actPage) {
Lg.d("setActPage: " + actPage + " : " + fragments.get(actPage).toString());
this.actPage = actPage;
}
public void addItem(BaseFragment fragment)
{
Lg.d("addItem: " + fragment.toString());
fragments.add(fragment);
}
public void removeItem(int index)
{
if(index < getCount()){
Lg.d("RemoveItem: " + index + " : " + fragments.get(index).toString());
fragments.remove(index);
}
}
public BaseFragment getActFragment()
{
return getItem(getActPage());
}
public int getActPage() {
return actPage;
}
@Override
public BaseFragment getItem(int position) {
if(position < getCount())
{
Lg.v("getItem: " + fragments.get(position));
return fragments.get(position);
}
else
return null;
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public String getTitle(int position) {
Lg.v("Get Title: " + fragments.get(position).getTitle());
return fragments.get(position).getTitle();
}
}
Yeah i hope someone can help me.
If i forgot something let me konw.
Thanks in advance,
Mike
Ok i’ve now solved my problem in a hackish way, but yeah it’s working ;). If someone can improve my solution please let me know. For my new solution i now use a CustomFragmentStatePagerAdapter but it doesn’t save the state like it should and stores all the Fragments in a list. This can cause a memory problem if the user has more than 50 fragments, like the normal FragmentPagerAdapter does. It would be great if someone can add the State-thing back to my solution without removing my fixes. Thanks.
So here’s my CustomFragmentStatePagerAdapter.java
Here’s my normal FragmentAdapter.java
And this is the way i delete a Fragment. (I know it’s a bit more than only .remove() ). Be free to improve my solution, you can also add this code somewhere in the adapter so yeah. It’s up to the user who tries to implement this. I use this in my TabHelper.java (A class which handles all tab operations like delete, add, …)
Description of the Cfg. thing. I save the reference to those objects in a cfg, class so i can always use them without the need of a special Factory.java …
Yeah. i hope i was able to help. Feel free to improve this, but let me know so i can improve my code too.
Thanks.
If i missed any code let me know.
My old answer also works but only if you have different Fragments. FileFragment, WebFragment, … Not if you use one of those fragmenttypes twice.
I got it pseudo working for now. It’s a really dirty solution and i’m still searching for a better one. Please help.
I changed the code, where i delete a tab to this one:
If someone can improve this code let me know. If someone can tell us the real answer for that problem. please add it here. There are many many people who experience this issue. I added a reputation of 50 for the one who solve it. I can also give a donation for the one who solve it.
Thanks