簡體   English   中英

將我的應用程序轉換為片段:如何處理活動?

[英]Convert my application to Fragments: How to handle activities?

我打算將現有的android應用程序轉換為片段布局。

這個想法是采用經典的兩個面板布局(項目列表在左側,細節在右側)。

實際上,該應用程序由4個活動組成:

  1. 具有所有可用選項的ChoiceListActivity
  2. 3種不同的活動,工具上的每個操作一項。

現在,我開始進行轉換,並創建了FragmentActivity類,這是主要的類:

    public class MainFragment extends FragmentActivity {

    private static final String TAG = "MainFragment";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        if(findViewById(R.id.fragment_container)!=null){
            Log.i(TAG, "No Tablet");
            Intent i = new Intent(MainFragment.this, main.ChoiceActivity.class);
            startActivity(i);
        } else {
            Log.i(TAG, "Tablet");
        }
    }
}

我創建了ChoiceListFragment:

public class ChoiceListFragment extends ListFragment {

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
        super.onListItemClick(l, v, position, id);
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        String[] options = getResources().getStringArray(R.array.listitems);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), R.layout.list_item, options);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

}

該片段將在面板的左側。

我的問題是在右邊。 想法是,將為列表中的每個元素顯示相應的活動(或片段?)。

那么正確的方法是什么?

  1. 當用戶選擇項目時,以正確的片段開始活動是個好主意嗎?

  2. 還是我必須以編程方式在片段之間切換? 以及如何做到這一點(我找到了很多教程,但是他們在右側面板中始終使用相同的活動來更改其中的一些數據)?

我已經為正確的片段創建了以下類(但是我​​不確定自己是否正確執行了該類):

public class RightFragment extends Fragment {

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

    }

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


}

我注意到我最終可以在onCreate方法期間使用LayoutInflater對象更改布局,但這只是在屏幕上顯示布局,而在布局中聲明的對象未初始化(也未添加eventListener,等等)。 那么該怎么做呢?

也許我應該創建一個Intent並使用startActivity來啟動現有活動,或者這是一個壞主意嗎?

實際上,xml布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/choicelist_fragment"
        android:name="main.fragments.ChoiceListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/right_fragment"
        android:name="main.fragments.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout> 

好的,我找到了解決方案,一開始不清楚,但是閱讀一些文檔,看了許多教程,也許我知道它是如何工作的。

首先,我從布局中刪除了第二個片段* right_fragment *(檢查問題),我用一個名為* activity_container *的空FrameLayout替換了它,它將成為我的片段的容器。

背后的想法只是使用FragmentManager替換容器內的片段。

因此,我將onListItemClick方法更新為ChoiceListFragment,然后根據所點擊的列表項,創建一個新的Fragment並將其替換為* activity_container *。 更新的方法類似於以下內容:

public void onListItemClick(ListView l, View v, int position, long id) {            
        String itemName = getListView().getItemAtPosition(position).toString();
        switch(position){
        case OPTION_ONE:                getFragmentManager().beginTransaction().replace(R.id.activity_container, new OptionOneFragment()).commit();
            break;
        case RESISTOR_VALUE:
            getFragmentManager().beginTransaction().replace(R.id.activity_container, new OptionTwoFragment()).commit();
            break;
        default:
            Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
            break;
        }       
        super.onListItemClick(l, v, position, id);
    }

這樣,應用程序的每個組件都有自己的片段,由不同的類處理。

您走在正確的軌道上。 在小屏幕上,單擊列表項將啟動一個DetailActivity,這是一個對DetailFragment的簡單包裝。 在更大的屏幕上,單擊列表項將用新的DetailFragment實例替換右側。

如果您使用的是eclipse和ADT,建議您看一下MasterDetailFlow模板,可以通過創建新的Android項目或新的Android Activity來訪問它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM