最简单的ListView中实现单选和多选效果

一.无图无真相
最简单的ListView中实现单选和多选效果_第1张图片
最简单的ListView中实现单选和多选效果_第2张图片
二.关键代码
1.主布局 sample_main.xml(单选和多选切换,设置android:choiceMode,其他不变)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:showDividers="middle"
    android:divider="?android:dividerHorizontal">

    <TextView style="@style/Widget.DescriptionBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/intro_message" />

    
    <ListView android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="@dimen/page_margin"
        android:paddingRight="@dimen/page_margin"
        android:scrollbarStyle="outsideInset"
        android:choiceMode="multipleChoice" />
LinearLayout>

2.item布局


<com.example.android.customchoicelist.CheckableLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="8dp"
    android:id="@+id/ll_contain"
    android:paddingRight="8dp"
    android:minHeight="?android:listPreferredItemHeight"
    android:gravity="center_vertical">

    <TextView android:id="@android:id/text1"
        android:duplicateParentState="true"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@color/hideable_text_color" />

    <ImageView android:src="@drawable/ic_hideable_item"
        android:duplicateParentState="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp" />

com.example.android.customchoicelist.CheckableLinearLayout>

3.自定义单选和多选布局

package com.example.android.customchoicelist;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Checkable;
import android.widget.LinearLayout;

/**
 *自定义单选和多选布局
 */
public class CheckableLinearLayout extends LinearLayout implements Checkable {
    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};

    private boolean mChecked = false;

    public CheckableLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //返回选中的状态
    public boolean isChecked() {
        return mChecked;
    }
    //设置选中的状态
    public void setChecked(boolean b) {
        if (b != mChecked) {
            mChecked = b;
            refreshDrawableState();
        }
    }
    //选择开关
    public void toggle() {
        setChecked(!mChecked);
    }
    //添加选中的条目状态
    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }
}

4.主要代码MainActivity.java

package com.example.android.customchoicelist;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;



/**
 *如何创建自定义的单或多选择
 * @author  孤狼
 * @since  2015-8-3
 */
public class MainActivity extends Activity {
    ListView listView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_main);
        listView =(ListView)findViewById(R.id.listview);
        listView.setAdapter(new MyAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                //获取条目
                CheckableLinearLayout linearLayout = (CheckableLinearLayout) view.findViewById(R.id.ll_contain);
                if (linearLayout.isChecked()) {
                    Log.e("选中true", position + "");
                } else {
                    Log.e("未选中false", position + "");
                }
                /**对于多选,建议创建集合,用于封装用户选中的条目position,存入时判定                     用户来回切换的状态*/
            }
        });
    }

    /**
     * A simple array adapter that creates a list of cheeses.
     */
    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return Cheeses.CHEESES.length;
        }

        @Override
        public String getItem(int position) {
            return Cheeses.CHEESES[position];
        }

        @Override
        public long getItemId(int position) {
            return Cheeses.CHEESES[position].hashCode();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, container, false);
            }
            ((TextView) convertView.findViewById(android.R.id.text1))
                    .setText(getItem(position));
            return convertView;
        }
    }
}

三.说明
通篇没有多余的废话,不浪费大家的宝贵时间,代码简洁,主要部分都有注释,实际开发中的需求,需要牛牛们自己自定义,发挥,如有疑问,请留言—-孤狼

四.示例源码(由于孤狼使用的是Android studio开发,暂时没有eclipse版)

下载示例源码

你可能感兴趣的:(Android开发)