提示ListView中的可视检查状态

时间:2015-05-08 07:58:02

标签: android listview drawerlayout

我对Android很陌生,我不明白如何实现我的目标。不过,这很简单。

我使用Drawerlayout(API级别16)创建了一个滑动菜单。在这里,我放置了一个ListView,它显示了一个自定义布局(顺便说一下ImageView)。有一个选择器可以根据每个项目的选中状态更改背景。

用户应该切换每个项目:当选中一个项目时,背景应该改变其颜色。

使用下面的代码我可以"选择"一个项目,我的意思是只有当用户点击一个项目时才会改变背景(到list_item_bg_checked),而不管检查状态如何(存储到NavDrawerItem的活动变量中)。

拜托,你能把我放在正确的路上吗?

activity_content.xml

<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"
    android:layout_gravity="left"
    android:layout_alignParentLeft="true">

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:choiceMode="multipleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="10dp"
        android:listSelector="@drawable/list_selector"
        android:background="#00000000"/>

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

drawer_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="98dp"
    android:layout_height="150dp"
    android:background="#00000000">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="98dp"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon"
        android:layout_centerVertical="true" />

</RelativeLayout>

list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/list_item_bg_normal"  />
    <item android:drawable="@drawable/list_item_bg_checked" android:state_checked="true"/>
</selector>

ContentActivity.java

package my.own.package;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.VideoView;

import java.util.ArrayList;

import my.own.package.slidingmenu.adapter.NavDrawerListAdapter;
import my.own.package.slidingmenu.model.NavDrawerItem;

public class ContentActivity extends Activity {
    private static final String TAG = ContentActivity.class.getSimpleName();
    public static final int OPTION_1 = 0;
    public static final int OPTION_2 = 1;
    public static final int OPTION_3 = 2;

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;

    private TypedArray navMenuIcons;
    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

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

        navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();
        navDrawerItems.add(new NavDrawerItem(navMenuIcons.getResourceId(0, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuIcons.getResourceId(1, -1)));
        navDrawerItems.add(new NavDrawerItem(navMenuIcons.getResourceId(2, -1)));

        // init the variable's state
        navDrawerItems.get(OPTION_1).setActive(false);
        navDrawerItems.get(OPTION_2).setActive(true);
        navDrawerItems.get(OPTION_3).setActive(false);

        navMenuIcons.recycle();
        adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
        mDrawerList.setAdapter(adapter);
        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // init the checked state of the items
        mDrawerList.setItemChecked(OPTION_1, navDrawerItems.get(OPTION_1).getActive());
        mDrawerList.setItemChecked(OPTION_2, navDrawerItems.get(OPTION_2).getActive());
        mDrawerList.setItemChecked(OPTION_3, navDrawerItems.get(OPTION_3.getActive());

    }

    private class SlideMenuClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final NavDrawerItem item = (NavDrawerItem) mDrawerList.getItemAtPosition(position);
            item.setActive(!item.getActive());
            mDrawerList.setItemChecked(position, item.getActive());            }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以跟踪当前所选元素的位置:

 OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
                mSelectedItem = position;
                mAdapter.notifyDataSetChanged();
        }
    };

并覆盖适配器的getView方法:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View view = View.inflate(context, R.layout.item_list, null);

        if (position == mSelectedItem) {
            // set your color
        }

        return view;
    }