2013-03-01 47 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