Creating a new ArrayPagerAdapter with variety of Fragments

偶尔善良 提交于 2019-12-25 05:47:09

问题


I am trying to create an ArrayAdapter similar to the sample shown in the readme at this github page https://github.com/commonsguy/cwac-pager. I have a variety of Fragments that I want to pass into an ArrayPagerAdapter instead of just one like in the example. I'm confused on how desc.getTitle is supposed to be passed in into the newly created instance of the Fragment in the example. Also how would I be able to do this same thing using android.app.Fragment to create a new instances of Fragments? I have posted my code below I have an error on newInstance();

static class SimplePagerAdapter extends ArrayPagerAdapter<android.app.Fragment>{



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

    @Override
    protected android.app.Fragment createFragment(PageDescriptor desc) {

        Fragment newFragment = new JudgeMainFragment();

        return(android.app.Fragment.newInstance(desc.getTitle()));
    }
}

回答1:


I'm confused on how desc.getTitle is supposed to be passed in into the newly created instance of the Fragment in the example

Your code is different from what I have in the project README and will not work, as Fragment has no newInstance() method. The README is showing an EditorFragment, specifically the EditorFragment from the sample app. EditorFragment implements a newInstance() method, using the arguments Bundle to get the title into the fragment:

package com.commonsware.cwac.pager.demo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class EditorFragment extends Fragment {
  private static final String KEY_TITLE="title";

  static EditorFragment newInstance(String title) {
    EditorFragment frag=new EditorFragment();
    Bundle args=new Bundle();

    args.putString(KEY_TITLE, title);
    frag.setArguments(args);

    return(frag);
  }

  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.editor, container, false);
    EditText editor=(EditText)result.findViewById(R.id.editor);

    editor.setHint(getTitle());

    return(result);
  }

  public String getTitle() {
    return(getArguments().getString(KEY_TITLE));
  }
}

Note that there is no requirement for the fragment to know its title, but if you want the fragment to know its title, use the arguments Bundle, such as via a newInstance()-style factory method.

Also how would I be able to do this same thing using android.app.Fragment to create a new instances of Fragments?

You wouldn't, because Fragment itself is fairly useless on its own. You create subclasses of Fragment to implement your pages, the way you do any fragment in Android.

I have a variety of Fragments that I want to pass into an ArrayPagerAdapter instead of just one like in the example

You would need to know, based on the PageDescriptor, how to create the right fragment instance. That might be via an int or something in your custom PageDescriptor implementation, that you then use with a switch in createFragment().



来源:https://stackoverflow.com/questions/33003500/creating-a-new-arraypageradapter-with-variety-of-fragments

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