2012-09-09 35 views
0

我正在開發一個android項目,我正在努力研究如何使用片段爲我的應用製作平板電腦友好的用戶界面。但我不確定如何根據片段A中發生的情況更新片段B.我知道我需要某種接口,但我無法解決如何實現它。更新片段中的內容

基本上,我所擁有的是一項名爲MainActivity的活動,它設置了片段的佈局。

在橫向模式下,XML文件是。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <fragment android:name="com.BoardiesITSolutions.FragmentTest.FragmentA" 
     android:id="@+id/list" 
     android:layout_weight="1" 
     android:layout_width="0px" 
     android:layout_height="match_parent"> 
    </fragment> 
    <FrameLayout android:id="@+id/viewer" 
     android:layout_weight="1" 
     android:layout_width="0px" 
     android:layout_height="match_parent" 
     android:background="?android:attr/detailsElementBackground"> 
    </FrameLayout> 
</LinearLayout> 

在縱向模式下其

目前在MainActivity只是設置的內容視圖的XML文件在上面的方法onCreate內使用SetContentView。下面是它的外觀。

Fragment layout

在它延伸ListFragment,包含項目的ListView和我希望能夠做的是基於什麼是碎片A.

選擇更新片段B內的TextView的FragmentA類文件

下面是片段A.代碼

@Override 
    public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) 
    { 
     return inflator.inflate(R.layout.fragment_a, container, false); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) 
    { 
     super.onActivityCreated(savedInstanceState); 

     myListView = getListView(); 

     ArrayList<String> arrayList = new ArrayList<String>(); 
     arrayList.add("Item1"); 
     arrayList.add("Item2"); 
     arrayList.add("Item3"); 

     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), 
       android.R.layout.simple_list_item_activated_1, arrayList); 
     setListAdapter(arrayAdapter); 

     View fragmentB = getActivity().findViewById(R.id.viewer); 
     mDualPane = fragmentB != null && fragmentB.getVisibility() == View.VISIBLE; 

     if (savedInstanceState != null) 
     { 
      mCurCheckPosition = savedInstanceState.getInt("curChoice", 0); 
     } 

     if (mDualPane) 
     { 
      myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
      showDetails(mCurCheckPosition); 
     } 
    } 

    @Override 
    public void onListItemClick(ListView l, View view, int position, long id) 
    { 
     showDetails(position); 
    } 

    private void showDetails(int index) 
    { 
     mCurCheckPosition = index; 
     if (mDualPane) 
     { 
      myListView.setItemChecked(index, true); 
      FragmentB details = (FragmentB)getFragmentManager().findFragmentById(R.id.viewer); 
      if (details == null || details.getShownIndex() != index) 
      { 
       details = FragmentB.newInstance(index); 

       FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.viewer, details); 
       fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
       fragmentTransaction.commit(); 
      } 
     } 
     else 
     { 
      Intent intent = new Intent(getActivity(), FragmentBActivity.class); 
      intent.putExtra("index", index); 
      startActivity(intent); 
     } 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) 
    { 
     super.onSaveInstanceState(outState); 
     outState.putInt("curChoice", mCurCheckPosition); 
    } 

FragmentB包含下面的代碼,這類擴展片段

public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) 
    { 
     if (container == null) 
     { 
      return null; 
     } 
     View view = inflator.inflate(R.layout.fragment_b, container, false); 
     return view; 
    } 

    public static FragmentB newInstance(int index) 
    { 
     FragmentB fragmentB = new FragmentB(); 
     Bundle args = new Bundle(); 
     args.putInt("index", index); 
     //rgs.putString("content", content); 
     fragmentB.setArguments(args); 

     return fragmentB; 
    } 

    public int getShownIndex() 
    { 
     return getArguments().getInt("index", 0); 
    } 

而且在FragmentB活動文件時,它包含以下

@Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) 
     { 
      finish(); 
      return; 
     } 

     if (savedInstanceState == null) 
     { 
      FragmentB fragmentB = new FragmentB(); 
      fragmentB.setArguments(getIntent().getExtras()); 
      getFragmentManager().beginTransaction().add(android.R.id.content, fragmentB).commit(); 
     } 
    } 

正如你可以從上面的截圖中看到,我已在片段的基礎工作,當我點擊每個項目,它顯示目前選擇的項目是什麼,但我不知道如何告訴它根據用戶從片段a中單擊的內容更新片段b中的textview,以及如何在縱向和橫向模式下處理這些內容。

感謝您提供的任何幫助。

回答

2

您可以覆蓋onActivityCreated()方法FragmentB,通過該TextView的id查找視圖並更新它。

這裏有一個模擬:

public class FragmentB extends Fragment{ 

    //...... 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     TextView textView = getActivity().findViewById(R.id.my_textview); 
     textView.setText("Hello World!"); 
    } 
} 
+0

我將如何從第一個片段獲取值,即如果用戶在fragmentA中單擊Item1,則它將該值傳遞給FragmentB,以便它可以顯示在第二個片段上 – Boardy

+0

獲取列表項的值,並將其傳遞給'FragmentB.newInstance()'。正如你所看到的,'showDetails()'已經做了類似的事情,它接受item的索引,並通過'newInstance(index)'方法將它傳遞給FragmentB。 –

0

,但我不知道如何告訴它來更新基於什麼用戶從片段的點擊,這是如何在縱向處理在片段B中的TextView和風景模式。

Have Fragment調用託管活動的方法,讓它知道用戶點擊了某個東西。然後託管活動可以調用片段B上的方法(如果該片段存在),或者啓動片段B(如果片段不存在但有空間),或者啓動將負責顯示片段的活動B(例如,在電話上)。

我不會做的是你在做什麼:有片段A創建片段B.片段A不應該在意片段B是否存在;那是活動的工作。片段A只應該擔心片段A,並將必要事件傳遞給活動。

+0

我不完全確定你的意思。我使用的基本實現取自Google的android開發示例 – Boardy

+0

@Boardy:Android設備具有不同的屏幕尺寸(例如,手機與平板電腦)。如果你首先在打擾片段,那是因爲你正在計劃一個UI,在手機上,你顯示的是一個活動片段比平板電腦更少。你的片段A假設將會有*總是*片段B(屏幕尺寸是2「還是200」),並且片段A是負責顯示它的人。我不會這麼做,所以關於哪些片段適合顯示的決定是由活動做出的。片段只關注其自己的內容。 – CommonsWare

+0

但這是來自谷歌的例子。我沒有假設片段a的活動將始終包含片段b。如果我運行porteaid模式,則只顯示片段a,直到我單擊一個項目,然後顯示片段b。如果我以橫向模式運行應用程序,則兩個片段按預期顯示。 – Boardy

相關問題