我正在尝试使用此链接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>
答案 0 :(得分:0)
您正在研究为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中启动一个空项目并选择自动创建一个实现导航抽屉的活动,然后按照他们的实现为您自己的应用创建一个