Dynamically Adding and Removing tabs using ArrayPagerAdapter

时光毁灭记忆、已成空白 提交于 2019-12-13 05:06:54

问题


This is a continuation from the question I 1st asked here, Creating a new ArrayPagerAdapter with variety of Fragments. You were dead on about me using the wrong ArrayAdapter I just needed to use the one that has v4 support. I have posted the code for it below. One of the next blocks i'm getting stuck on right now is creating the PageDescriptor objects in the ArrayList passed into SimplePageAdapter. I've tried copying and pasting the SimplePageDescriptor class used in the Demo into my code but I am getting an error when trying to return from the Parceable.Creator method. It says SimplePageDescriptor has private access in com.commonsware.cwac.pager.SimplePageDescriptor. I guess the main thing i'm trying to grasp is how to use the SimplePageDescriptor from the demo in my own code. Do I just use the entire pager folder? I have posted my code for the SimplePagerAdapter and the SimplePageDescriptor below.

 class SimplePagerAdapter extends ArrayPagerAdapter<android.support.v4.app.Fragment> {



    public SimplePagerAdapter(FragmentManager fragmentManager,
                              ArrayList<PageDescriptor> descriptors) {
        super(fragmentManager, descriptors);
    }

    @Override
    protected Fragment createFragment(PageDescriptor desc) {

        mMainFragment = JudgeMainFragment.newInstance();

        mClassifyFragment = JudgeClassifyFragment.newInstance();

        mSidebarFragment = JudgeSidebarFragment.newInstance((SidebarCall) mActivity);


        mVerdictFragment = JudgeVerdictFragment.newInstance();



        return (mMainFragment.newInstance());


    }
}





public static final Parcelable.Creator<com.commonsware.cwac.pager.SimplePageDescriptor> CREATOR=
  new Parcelable.Creator<com.commonsware.cwac.pager.SimplePageDescriptor>() {
    public com.commonsware.cwac.pager.SimplePageDescriptor createFromParcel(Parcel in) {
      //This is the line I get the error at
      return new com.commonsware.cwac.pager.SimplePageDescriptor(in);
    }

    public com.commonsware.cwac.pager.SimplePageDescriptor[] newArray(int size) {
      return new com.commonsware.cwac.pager.SimplePageDescriptor[size];
    }
  };

回答1:


One of the next blocks i'm getting stuck on right now is creating the PageDescriptor objects in the ArrayList passed into SimplePageAdapter

PageDescriptor is an interface. Create your own class (e.g., BlainePageDescriptor) that implements the interface. This is covered in the documentation.

I've tried copying and pasting the SimplePageDescriptor class used in the Demo into my code

That will not solve your problem.

Your problem, as I understand it, is that you want your ArrayPagerAdapter to be able to handle N different types of pages (JudgeMainFragment, JudgeClassifyFragment, etc.). That requires you to return the proper fragment from createFragment(), given the supplied PageDescriptor. Hence, you need to create your own PageDescriptor implementation (e.g., BlainePageDescriptor). That class needs to hold onto sufficient information to both satisfy the PageDescriptor interface and be able to tell createFragment() what sort of fragment to create.



来源:https://stackoverflow.com/questions/33093955/dynamically-adding-and-removing-tabs-using-arraypageradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!