如何在三个标签页中使用相同的片段但显示不同的内容?

3

我有一个枚举类型,描述了三种不同的运动:

public enum MatchType {
    S1(0, "Sport1", "xml stream address", R.id.match_list, R.layout.fragment_match_list, R.color.separator_sport1),
    S2(0, "Sport2", "xml stream address", R.id.match_list, R.layout.fragment_match_list, R.color.separator_sport2),
    S3(0, "Sport3", "xml stream address", R.id.match_list, R.layout.fragment_match_list, R.color.separator_sport3);

    ...getters/setters

}

我接下来有一个片段,其中包含

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    matchesArrayAdapter = new MatchListAdapter(getActivity(), new ArrayList<Match>());

    return inflater.inflate(matchType.getLayout(), container, false);
}

在我的片段中,我有一个AsyncTask,在这里我有这个:
@Override
protected void onPostExecute(final List<Match> matches) {
    if (matches != null) {
        matchListView = (ListView) getActivity().findViewById(matchType.getRId());

        [setup listeners]

        matchesArrayAdapter.matchArrayList = matches;
        matchListView.setAdapter(matchesArrayAdapter);
    }
}

编辑: 我的Activity中有一个AppSectionsPagerAdapter,其中包含

public Fragment getItem(int i) {
    MatchListSectionFragment fragment = new MatchListSectionFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(Constants.MATCH_TYPE, i);
    fragment.setArguments(bundle);
    return fragment;
}

编辑2: 这是我的片段的onCreate和onCreateView:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    matchType = MatchType.getMatchType(bundle.getInt(Constants.MATCH_TYPE));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    matchesArrayAdapter = new MatchListAdapter(getActivity(), new ArrayList<Match>());
    return inflater.inflate(matchType.getLayout(), container, false);
}

AsyncTask 为我的枚举中的每个运动读取一个 xml 流,但我的问题是选项卡 #1 会被来自选项卡 #2 和随后的选项卡 #3 的数据覆盖。

以前我为每种运动定义了一个片段,但这肯定是不必要的吧?

我该如何使用相同的布局和片段来处理每种运动?

3个回答

2
在 Activity 中实例化 Fragment 时,使用 Bundle 设置 Fragment 的参数。
Bundle myBundle = new Bundle();
myBundle.putInt(MY_EXTRA, 1);
myFragment.setArguments(myBundle);

在你的Bundle中加入一些额外的内容,这些内容将在Fragment的onCreate()回调函数中被读取。
int i = getArguments().getInt(MY_EXTRA);

你能看一下我在原帖中的编辑吗?这是我目前所拥有的。这不够吗? - CJe
是的,在您的片段中从getArguments()读取MATCH_TYPE int,并相应地进行操作。 - GaBo
但是我已经使用matchType = MatchType.getMatchType(bundle.getInt(Constants.MATCH_TYPE))来做了,但还是不起作用... - CJe
我假设bundle是片段的参数?我不明白为什么它不能工作。这是我为无限数量的类似片段所做的方式。使用调试模式检查您的值是否被正确发送,也可以检查savedInstanceState信息吗? - GaBo
抱歉,savedInstanceInfo是什么?我该如何检查它? - CJe

0
你应该在你的Fragment中创建newInstance方法,同时你还应该将MatchType实例存储在你的Fragment中。
MatchType matchType;

public static MyFragment newInstance(MatchType matchType) {
          MyFragment fragment = new MyFragment();
          fragment.matchType = matchType;
          return fragment;
}

在您的Activity中,您应该使用此方法创建3个MyFragment实例(每个片段都拥有相关的MatchType)。然后,在onCreateView方法中,您应该从matchType向您的视图插入数据。
抱歉,我在手机上。对我的英语也很抱歉。
更新
检查您的变量matchType。也许它被声明为静态?

我已经在原帖中添加了EDIT 2,因为它现在不起作用 :-( - CJe
也许你的变量 matchType 被声明为 静态 了? - voltazor
不行。声明为私有的MatchType matchType = null; - CJe
非常感谢你,Voltazor。我回家后会看一下的 :-) - CJe

0

我正在使用手机。 在LinearLayout中放置三个FrameLayout,分别命名为frame1、frame2和frame3。这将是您的主Activity的布局。 然后在Activity的onCreate()方法中,调用getFragmentManager().getFragmentTransaction()。 实例化三个片段并通过Bundle发送数据,最好是通过Bundle。 在Fragment Transaction中为每个片段调用add()或replace()方法,第一个参数是相应FrameLayout的id,第二个参数是片段本身。 调用commit()。


我真的希望这可以适用于任意数量的片段,而不必为枚举中的每个条目指定一个片段。我希望这尽可能地动态化。 - CJe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接