将演示者传递给Android中的片段(不带构造函数)

时间:2014-04-16 19:59:49

标签: android fragment mvp

我想尝试将MVP模式用于我正在编写的简单Android应用程序。这是我第一次使用MVP模式,因为我还在学习,所以请保持温和;)

我有一个片段,我想与4位不同的演示者一起使用。我遇到的问题是,如何将不同的演示者传递给每个实例?

我会喜欢将演示者传递给构造函数,但是当Android重新创建片段时,它会调用默认构造函数。这是否意味着它将不再持有对演示者的引用?

如果是这样,我还能怎样传递演示者?

我已经包含了一些我想在下面做的伪代码。请注意我直接在浏览器中输入了这个,所以可能会有一些愚蠢的错误,但希望你能大致了解我的意思。

我的2个接口:

public interface IClickableListPresenter {

    ListAdapter createListAdapter();
    void onListItemClick(int position);

}

public interface ITabbable {

    String getTitle();
    Fragment getFragment();

}

2个示例演示者:

public class ArtistPresenter implements IClickableListPresenter {

    public ListAdapter createListAdapter(){
        // Create a ListAdapter containing a list of artists
    }

    public void onListItemClick(int position){
        // Handle the click event
    }
}

public class TitlePresenter implements IClickableListPresenter {

    public ListAdapter createListAdapter(){
        // Create a ListAdapter containing a list of song titles
    }

    public void onListItemClick(int position){
        // Handle the click event in a completely different way 
        // to the ArtistPresenter
    }

}

我的片段:

public class ClickableListFragment extends ListFragment 
                                    implements ITabbable {

    private IClickableListPresenter presenter;
    private String title;

    // What can I do instead of this constructor?
    public ClickableListFragment(
            String title, IClickableListPresenter presenter){

        this.title = title;
        this.presenter = presenter;
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setListAdapter(presenter.createListAdapter());
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id){
        presenter.onListItemClick(position);
    }

    public Fragment getFragment(){
        return this;
    }

    public String getTitle(){
        return title;
    }

}

最后,实例化片段的类:

public class TabsPagerAdapter extends FragmentPagerAdapter{

    private ITabbable tabs[] = {
            new ClickableListFragment("Artist", new ArtistPresenter()),
            new ClickableListFragment("Title", new TitlePresenter()),
            //...
    };

    //...
 }

1 个答案:

答案 0 :(得分:1)

你可以做这样的事情

public class TabsPagerAdapter extends FragmentPagerAdapter{

    private ITabbable tabs[] = {
        ClickableListFragment.newInstance(ClickableListFragment.Type.ARTIST),
        ClickableListFragment.newInstance(ClickableListFragment.Type.TITLE),
        //...
    };
}

在ClickableListFragment

public enum Type {
    ARTIST,

    TITLE
}

public static MyFragment newInstance(final Type fragmentType) {
    final MyFragment fragment = new MyFragment();
    Bundle args = new Bundle();
    args.putSerializable(TYPE, fragmentType);
    fragment.setArguments(args);
    return fragment;
}

和onCreate

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Type tempType = (Type) getArguments().getSerializable(TYPE);
    if(tempType == Type.ARTIST){
        variable = new ArtistPresenter(); 
        .... what you want with that variable
    .
    .
    .
}