gpt4 book ai didi

android - 将演示者传递给 Android 中的 fragment (没有构造函数)

转载 作者:行者123 更新时间:2023-11-29 01:43:15 26 4
gpt4 key购买 nike

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

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

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

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

我在下面包含了一些我想做的伪代码。请注意,我只是直接将其输入浏览器,因此可能会出现一些愚蠢的错误,但希望您能大致了解我的意思。

我的 2 个接口(interface):

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
}

}

我的 fragment :

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;
}

}

最后,实例化 fragment 的类:

public class TabsPagerAdapter extends FragmentPagerAdapter{

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

//...
}

最佳答案

你可以这样做

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
.
.
.
}

关于android - 将演示者传递给 Android 中的 fragment (没有构造函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23119009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com