1

我正在尝试编写一个带有几个不同项目的列表视图以及一个可以从屏幕左侧拖动的抽屉布局的 android 应用程序。这就是我的意思....

这是主屏幕的样子: 在此处输入图像描述

这是抽屉菜单的样子: 在此处输入图像描述

我遇到的问题是,当我打开侧抽屉菜单并点击一个选项时它不起作用并且菜单只是关闭。但是我能够与主列表视图页面进行交互。这是我的代码的样子:

String[] homeArray = { "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test" };

private ListView homeListView;
private ArrayAdapter arrayAdapter;



private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mDrawerTitles;

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



    mTitle = mDrawerTitle = getTitle();
    mDrawerTitles = getResources().getStringArray(R.array.Menu);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mDrawerTitles));

还有activity_main.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    >

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

<ListView
    android:id="@+id/left_drawer"
    android:layout_height="match_parent"
    android:layout_width="240dp"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="1dp"
    android:background="#111"
    />
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              tools:context=".ListActivity" >

<ListView
        android:id="@+id/homeListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
</ListView>

</LinearLayout>

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

我感觉问题出在 xml 中,但我不确定。

4

1 回答 1

3

请发布包含 NavigationDrawer 的 Activity 的完整代码。

此外,我强烈建议(谷歌也是如此)您将片段用于各个 NavigationDrawer 部分(在您的情况下为“设置”、“提交错误”和“帮助”)。

不要把所有的布局组件都放到你的Activity的布局文件中。

因此布局应如下所示:

<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">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        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>

FrameLayout content_frame 是保存 ListView 的 Fragment 所在的位置。 只需创建一个包含您在图片中显示的 ListView 的自定义 Fragment(或使用 ListFragment)并将其插入“content_frame”。

使用它来替换内容框架内的片段:(当您切换部分时)

/** Swaps fragments in the main content view */
private void selectItem(int position) {
    // Create a new fragment and specify the planet to show based on position
    Fragment fragment = new YourListFragment(); // this fragment contains the list with all the "test" items

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, fragment)
                   .commit();

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

片段外观示例:YourListFragment.java 布局文件“list_fragment.xml”只包含您想要的任何布局组件。例如一个列表视图。在 Fragments onCreateView() 方法中初始化填充 ListView。

public class YourListFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.list_fragment, container, false);

        return rootView;
    }
}

全部取自谷歌关于如何创建 NavigationDrawer 的示例:

http://developer.android.com/training/implementing-navigation/nav-drawer.html

于 2013-08-23T21:58:03.097 回答