我在我的android應用中實現了導航抽屜。但現在我希望能夠在用戶單擊導航欄中的任何列表項時使用片段更改佈局。 這是我迄今爲止得到:Android - 導航抽屜碎片
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">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
</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="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Java文件
public class MainActivity extends Activity {
final String[] data ={"one","two","three"};
@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);
}
});
drawer.closeDrawer(navList);
}
});
}
}
使用上面的代碼,我實現了導航抽屜在我的應用程序,我看導航抽屜中的「一個」,「兩個」和「三個」列表項目,但除了抽屜關閉之外,點擊它們時沒有任何反應。 所以,我的問題是: 如何將片段功能添加到上述給定的代碼?
我是初學者。提前致謝!
你必須通過FragmentActivity來做到這一點。 – Kishan
@ kishandhamat爲什麼是這樣? – Raghunandan
這可能有助於http://www.gadgetsaint.com/android/android-navigation-drawer-toolbar/#。WOBABBJ97fY – ASP