使用DrawerLayout用一个ListView一起 [英] Using a DrawerLayout along with a ListView

查看:240
本文介绍了使用DrawerLayout用一个ListView一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个Android应用程序与几个不同项的列表视图,也可以从屏幕左侧拖动drawerlayout。这就是我的意思是......

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

这是主屏幕的样子:

This is what the main screen looks like:

和这里的抽屉菜单东西看​​起来是这样的:

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

我遇到的问题是,当我打开抽屉边菜单的东西,挖掘一个选项它不工作,菜单只是关闭。但是我能够与主列表视图页面交互。这里是我的code样子:

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

而activity_main.xml中:

And the 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,但我不知道。

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

推荐答案

请发表您的活动,持有NavigationDrawer充分code。

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

此外,我会强烈建议(等将谷歌),您使用的个人NavigationDrawer部分片段(设置,提交错误,并在你的情况下帮助)。

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

不要把所有的布局组件到活动的布局文件。

因此​​,布局应如下所示:

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>

总的FrameLayout content_frame是你的片段保存在ListView将进入。
只需创建一个自定义的片段(或使用ListFragment),其中包含您在图片中显示的ListView和其插入content_frame。

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

使用它来取代内容框架内的片段:(当您打开一节)

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

你的碎片如何看起来像一个例子: YourListFragment.java
布局文件list_fragment.xml只包含任何你想要的布局组件。例如一个ListView。初始化填充碎片onCreateView()方法中ListView控件。

An Example of how your Fragment could look like: YourListFragment.java The 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.

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:

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

http://developer.android.com/training/implementing-导航/ NAV-drawer.html

这篇关于使用DrawerLayout用一个ListView一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆