Android抽屉ListView不显示项目

时间:2017-08-19 11:29:22

标签: android android-layout listview

我创建了一个Android应用,并希望在左上方显示抽屉。抽屉显示在那里,但是当我点击抽屉时列表项目没有显示。以下是我的代码:

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.yihanwang.myapplication.MainActivity">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:scaleType="fitXY"
            android:src="@drawable/homepage" />

        <TextView
            android:id="@+id/topTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="165dp"
            android:gravity="center_horizontal"
            android:text="HikingMate"
            android:textColor="#FEFEFE"
            android:textSize="100px"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:id="@+id/subTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/topTextView"
            android:layout_alignRight="@+id/topTextView"
            android:layout_below="@+id/topTextView"
            android:layout_marginEnd="39dp"
            android:layout_marginRight="39dp"
            android:gravity="center_horizontal"
            android:text="Hikers best friend"
            android:textColor="#FEFEFE"
            android:textSize="50px" />

        <Button
            android:id="@+id/mapButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="30dp"
            android:background="@drawable/shape"
            android:elegantTextHeight="true"
            android:text="Plants  near  me"
            android:textColor="#0C640F"
            android:textSize="20sp"
            android:textStyle="bold" />
    </RelativeLayout>

    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#fff"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

以下是我的drawer_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#000"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

以下是我的MainActivity课程:

public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView topText;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private String[] mPlanetTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle("Title");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("Title");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        mDrawerLayout.addDrawerListener(mDrawerToggle);

        button = (Button) findViewById(R.id.mapButton);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent subscription = new Intent(getApplicationContext(), MapActivity.class);
                startActivity(subscription);

            }
        });

        topText = (TextView) findViewById(R.id.topTextView);


    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }


    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        }
    }
}

以下是listview项目数组的字符串资源:

<string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
    </string-array>

EDIT1

我覆盖以下功能:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

并删除此行:mDrawerToggle.setDrawerIndicatorEnabled(false);

现在,当我点击切换时,会显示list-view-items,但切换图标不会更改。以下是截图:

android screenshot

从上面的屏幕截图中可以看到,左上角的切换按钮是黑色图标,但我设置的是一个白色图标。我是按mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer);进行设置的,如果删除此行,似乎该行无效:mDrawerToggle.setDrawerIndicatorEnabled(false);

1 个答案:

答案 0 :(得分:0)

试试这个;

 <LinearLayout
    android:id="@+id/drawer"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#FFF"
    android:orientation="vertical">

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.example.yihanwang.myapplication.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:scaleType="fitXY"
        android:src="@drawable/homepage" />

    <TextView
        android:id="@+id/topTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="165dp"
        android:gravity="center_horizontal"
        android:text="HikingMate"
        android:textColor="#FEFEFE"
        android:textSize="100px"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/subTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/topTextView"
        android:layout_alignRight="@+id/topTextView"
        android:layout_below="@+id/topTextView"
        android:layout_marginEnd="39dp"
        android:layout_marginRight="39dp"
        android:gravity="center_horizontal"
        android:text="Hikers best friend"
        android:textColor="#FEFEFE"
        android:textSize="50px" />

    <Button
        android:id="@+id/mapButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:background="@drawable/shape"
        android:elegantTextHeight="true"
        android:text="Plants  near  me"
        android:textColor="#0C640F"
        android:textSize="20sp"
        android:textStyle="bold" />
</RelativeLayout>

<!-- The navigation drawer -->
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:background="#fff"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

 </LinearLayout>