2013-08-17 51 views
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); 
    } 
} 

編輯: 在我的活動我有一個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的數據覆蓋。

之前我有一個爲每項運動定義的片段,但肯定不是必需的?

我該如何處理每個運動使用相同佈局的相同片段?

回答

1

當在您的Activity中實例化您的片段時,使用一個Bundle來設置片段的參數。

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

在你的捆綁放置一些額外的,將被讀取片段的onCreate()回調。

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

你可以在我原來的職位看看我的編輯?這是我目前所擁有的。這還不夠嗎? – CJe

+0

是的,然後在你的片段中讀取來自getArguments()的MATCH_TYPE int並相應地進行工作。 – GaBo

+0

但是,我已經做了matchType = MatchType.getMatchType(bundle.getInt(Constants.MATCH_TYPE));仍然不起作用... – CJe

0

我在移動。 將三個FrameLayouts放入一個名爲frame1,frame2和frame 3的LinearLayout中。這將是您的主Activity的佈局。 然後在Activity的oncreate()方法中,調用getFragmentManager()。getFragmentTransaction()。 實例化這三個片段並將其發送給他們,最好通過一個Bundle。 在Fragment Transaction上爲每個片段調用add()或replace()方法,第一個參數是相應FrameLayout的ID,第二個參數是片段本身。 調用commit()。

+0

我真的希望這可以處理任何數量的片段,而不必爲我的枚舉中的每個條目指定一個片段。我希望這個儘可能動態。 – CJe

0

您應該在您的片段中創建newInstance方法,同時您應該將MatchType instansce存儲在您的片段中。

MatchType matchType; 

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

在你的活動,你應該創建MyFragment的3個實例用此方法(以及相關的每個片段它擁有使用MatchType)。然後在onCreateView方法中,你應該從matchType向你的視圖中插入數據。

對不起,我是手機。抱歉我的英語。

更新

檢查變量使用MatchType。也許它宣佈爲靜態

+0

我已經加入編輯2我原來的職位,因爲它是現在 - 不工作:-( – CJe

+0

也許你的變量** **使用MatchType聲明爲靜態** ** – voltazor

+0

Nopes聲明爲private使用MatchType使用MatchType = NULL;? – CJe

相關問題