【问题标题】:How do I use the same fragment for three tabs with different content?如何将相同的片段用于具有不同内容的三个选项卡?
【发布时间】:2013-08-17 14:37:10
【问题描述】:

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

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

编辑: 在我的活动中,我有一个 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 中的数据覆盖。

之前我为每项运动定义了一个片段,但肯定没有必要吗?

如何为每项运动使用具有相同布局的相同片段?

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    在 Activity 中实例化片段时,请使用 Bundle 设置片段的参数。

    Bundle myBundle = new Bundle();
    myBundle.putInt(MY_EXTRA, 1);
    myFragment.setArguments(myBundle);
    

    在你的包里放一些额外的东西,将在片段的 onCreate() 回调中读取。

    int i = getArguments().getInt(MY_EXTRA);
    

    【讨论】:

    • 您能看看我在原始帖子中的编辑吗?这就是我目前所拥有的。这还不够吗?
    • 是的,然后在您的片段中从 getArguments() 读取 MATCH_TYPE int 并进行相应的工作。
    • 但是我已经用 matchType = MatchType.getMatchType(bundle.getInt(Constants.MATCH_TYPE));还是不行……
    • 我假设 bundle 是片段参数?我不明白为什么它不会工作。这就是我对无限数量的相似片段的做法。使用调试模式检查您的值是否正确发送,也许还要检查您的 savedInstanceState 信息?
    • 抱歉,saveInstanceInfo 是什么?以及如何检查?
    【解决方案2】:

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

    【讨论】:

    • 我真的希望它可以处理任意数量的片段,而不必为我的枚举中的每个条目指定一个片段。我希望这尽可能动态。
    【解决方案3】:

    你应该在你的片段中创建 newInstance 方法,你也应该在你的片段中存储 MatchType 实例。

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

    在您的 Activity 中,您应该使用此方法创建 3 个 MyFragment 实例(与它拥有的每个片段 MatchType 相关)。然后在 onCreateView 方法中,您应该将数据从 matchType 插入到您的视图中。

    抱歉,我正在使用移动设备。对不起我的英语。

    更新

    检查您的变量ma​​tchType。也许它声明为静态

    【讨论】:

    • 我已将 EDIT 2 添加到我原来的帖子中,因为它现在是 - 不工作:-(
    • 也许您的变量 matchType 声明为 static
    • 不。声明为私有 MatchType matchType = null;
    • 感谢一百万伏特。回家后看看:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多