簡體   English   中英

在片段中創建導航抽屜

[英]Creating Navigation Drawer in Fragment

我正在嘗試使用此鏈接http://developer.android.com/training/implementing-navigation/nav-drawer.html#top的指令為片段創建左側導航。 問題在這一行,但我不知道如何解決

mDrawerList.setAdapter(adapter);

這是完整的代碼:

public class BattleFieldFragment extends Fragment {
private String[] mLeftDrawerTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.activity_battle_field, parent, false);
    mLeftDrawerTitles = getResources().getStringArray(R.array.bar_left_drawer);
    mDrawerLayout = (DrawerLayout) v.findViewById(R.id.layout_battle_field_drawer);
    mDrawerList = (ListView) v.findViewById(R.id.layout_battle_field_left_drawer);

    // Set the adapter for the list view
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.drawer_battle_field, mLeftDrawerTitles);
    mDrawerList.setAdapter(adapter);
    return v;
}

我沒有寫xml代碼。 請讓我知道是否需要更多信息。

這是所請求的activity_battle_field.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_battle_field_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- The main content view -->
<FrameLayout
    android:id="@+id/layout_battle_field"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- The navigation drawer -->
 <!-- should not be larger than 320 to show content -->
<ListView android:id="@+id/layout_battle_field_left_drawer"
    android:layout_width="180dp"
    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> 

您正在處理為Activity構建的示例,現在嘗試在Fragment中實現它。 我將給您一個簡短的示例,說明我是如何在應用程序中完成此操作的,您只需更改布局名稱即可。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.skoric.tdm.app.MainActivity" >

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<fragment
    android:id="@+id/navigation_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.skoric.tdm.drawer.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

fragment_navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView
    android:id="@+id/drawerList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:listSelector="@drawable/drawer_list_selector"
    android:background="@color/white" />

</android.support.v4.widget.DrawerLayout>

NavigationDrawerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
    View view = inflater.inflate(R.layout.fragment_navigation_drawer, container);

    drawerListView = (ListView) view.findViewById(R.id.drawerList);

    ...

    drawerListView.setAdapter(adapter);

    return view;
}

但這只是代碼的一部分,我個人建議您在Eclipse或Android Studio中啟動一個空項目,然后選擇自動創建一個活動並實現導航抽屜,然后按照其實現為您自己的應用創建一個活動。

暫無
暫無

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

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