2013-03-01 111 views
0

我想開發用於在橫向模式下的片劑Android應用與2個片段:顯示片段並排側

  1. 片段1包含按鈕的列表。
  2. 片段2是一個按鈕相關片段的空間。

隨着「按鈕相關的片段」我的意思如下:當用戶在片段1按下按鈕,與該按鈕相關聯的片段應片段被顯示2.

片段1是種導航面板。

爲了實現這一點,我寫了下面的代碼:

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name="mypackage.MainActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="landscape" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".impl.activities.IntroActivity"></activity> 
    <activity android:name=".impl.activities.SimulationActivity"></activity> 

</application> 

MainActivity看起來是這樣的:

public class MainActivity extends Activity implements IMainActivity { 
    @Override 
    public void onCreate(final Bundle aSavedInstanceState) { 
    super.onCreate(aSavedInstanceState); 
    setContentView(R.layout.main); 
    } 

    @Override 
    public void show(final View aView, final Class<? extends Activity> aActivityClass) { 
    startActivity(new Intent(this, aActivityClass)); 
    } 


} 

main.xml有以下內容:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:name="mypackage.fragments.ContentFragment"/> 

ContentFragment看起來是這樣的:

public class ContentFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    final View view = inflater.inflate(R.layout.mainfrag, container, false); 

    IMainActivity mainActivity = (IMainActivity) this.getActivity(); 
    final IntroButtonClickListener introListener = new IntroButtonClickListener(
     mainActivity, IntroActivity.class); 

    final IntroButtonClickListener simulationListener = new IntroButtonClickListener(
     mainActivity, SimulationActivity.class); 


    view.findViewById(R.id.intro_button).setOnClickListener(introListener); 
    view.findViewById(R.id.simulation_button).setOnClickListener(simulationListener); 

    return view; 
    } 

IntroButtonClickListener

class IntroButtonClickListener implements View.OnClickListener { 
    private IMainActivity mainActivity; 
    private Class<? extends Activity> activityClass; 

    public IntroButtonClickListener(final IMainActivity aMainActivity, 
     final Class<? extends Activity> aActivityClass) { 
    this.mainActivity = aMainActivity; 
    this.activityClass = aActivityClass; 
    } 

    @Override 
    public void onClick(final View aView) { 
    this.mainActivity.show(aView, this.activityClass); 
    } 

} 

mainfrag.xml

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

    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="25" 
    android:id="@+id/intro_button" 
    android:text="@string/intro_button"/> 

    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="25" 
    android:id="@+id/simulation_button" 
    android:text="@string/simulation_button"/> 

    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="25" 
    android:id="@+id/statistics_button" 
    android:text="@string/statistics_button"/> 

    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="25" 
    android:id="@+id/help_button" 
    android:text="@string/help_button"/> 


</LinearLayout> 

此代碼將導致以下結果:

  1. 當我啓動應用程序,也有在屏幕上的4個按鈕(從mainfrag.xml)。
  2. 當我按intro_buttonsimulation_button時,視圖分別變爲IntroFragmentSimulationFragment
  3. 當我按下後退按鈕時,四個按鈕再次可見。

我的問題:當IntroFragmentSimulationFragment變得可見時,按鈕面板消失。

我應該如何修改我的代碼,這樣既按鈕面板各自的「細節」視圖(IntroFragmentSimulationFragment)是在同一時間看到(有足夠的屏幕空間這個)?

回答

0

此類示例顯示在Android SDK中的APIDemos中。他們將左面板作爲列表視圖,選定的項目在右側顯示相關片段。

可以發現,在

SDK \ Android的sdk_r11-WINDOWS \ Android的SDK-WINDOWS \ SAMPLES \ Android的14 \ ApiDemos