Java 使用 DrawerLayout 和 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18412395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:23:04  来源:igfitidea点击:

Using a DrawerLayout along with a ListView

javaandroidlistviewdrawerlayout

提问by Plays2

I'm trying to write an android app with a listview of a few different items and also a drawerlayout that you can drag from the left side of the screen. Here's what i mean....

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

This is what the main screen looks like: enter image description here

这是主屏幕的样子: 在此处输入图片说明

And here's what the drawer menu thing looks like: enter image description here

这是抽屉菜单的样子: 在此处输入图片说明

The problem i'm having is that when I open the side drawer menu thing and tap an option it doesn't work and the menu just closes. However i'm able to interact with the main listview page. Here's what my code looks like:

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

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));

And the activity_main.xml:

和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>

I have a feeling the problem is in the xml but i'm not sure.

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

采纳答案by Philipp Jahoda

Please post the full code of your Activity that holds the NavigationDrawer.

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

Furthermore, I would strongly recommend (and so would Google) that you use Fragments for the individual NavigationDrawer sections("Settings", "Submit Bug" and "Help" in your case).

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

Do not put all the layout components into the layout file of your Activity.

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

The layout therefore should look like the following:

因此,布局应如下所示:

<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>

The FrameLayout content_frame is where your Fragment that holds the ListView will go into.Simply create a custom Fragment (or use a ListFragment) that contains the ListView you displayed in your picture and insert it into the "content_frame".

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

Use this to replace Fragments inside the content frame: (when you switch your section)

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

/** 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);
}

An Example of how your Fragment could look like: YourListFragment.javaThe layout file "list_fragment.xml" simply contains whatever layout components you want. For example a ListView. Initialize an populate the ListView inside the Fragments onCreateView() method.

您的 Fragment 的外观示例:YourListFragment.java布局文件“list_fragment.xml”仅包含您想要的任何布局组件。例如一个 ListView。在 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;
    }
}

All taken from Googles example on how to create a NavigationDrawer:

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

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

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