导航抽屉 - 碎片无法正常工作?

时间:2014-01-20 07:28:33

标签: android navigation

我已经在我的应用中实现了导航抽屉但是遇到了问题。我在抽屉布局中有3个列表项(铝,金,锌)和相应的3个碎片。 当我从Gold转换到Zinc时,铝片会显示一两秒,然后锌片显示出来,反之亦然,从锌转换成金色。

爪哇

public class MainActivity extends FragmentActivity {
final String[] data ={"Aluminium","Gold","Zinc"};
final String[] fragments ={
        "com.Chinmay.navigationdrawer.Gold",
        "com.Chinmay.navigationdrawer.Aluminium",
        "com.Chinmay.navigationdrawer.Zinc"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    final ListView navList = (ListView) findViewById(R.id.left_drawer);
    navList.setAdapter(adapter);
    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){

            drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                @Override
                public void onDrawerClosed(View drawerView){
                    super.onDrawerClosed(drawerView);
                    android.support.v4.app.FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                    tx.replace(R.id.content_frame, Fragment.instantiate(MainActivity.this, fragments[pos]));
                    tx.commit();
                }
            });
            drawer.closeDrawer(navList);
            android.support.v4.app.FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
            tx.replace(R.id.content_frame,Fragment.instantiate(MainActivity.this, fragments[0]));
            tx.commit();
        }
    });
}

}

XML

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="match_parent"
        android:background="#000000"
        android:layout_height="match_parent" >

    </RelativeLayout>
</FrameLayout>
<ListView android:id="@+id/left_drawer"
    android:layout_width="220dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="1dp"
    android:background="#000000"/>
</android.support.v4.widget.DrawerLayout>

请帮帮我。我是初学者,学习android开发。

提前致谢!

0 个答案:

没有答案