我有一個描述三個不同的運動枚舉:如何爲三個標籤使用不同內容的相同片段?
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的數據覆蓋。
之前我有一個爲每項運動定義的片段,但肯定不是必需的?
我該如何處理每個運動使用相同佈局的相同片段?
你可以在我原來的職位看看我的編輯?這是我目前所擁有的。這還不夠嗎? – CJe
是的,然後在你的片段中讀取來自getArguments()的MATCH_TYPE int並相應地進行工作。 – GaBo
但是,我已經做了matchType = MatchType.getMatchType(bundle.getInt(Constants.MATCH_TYPE));仍然不起作用... – CJe