自定义的ListView单选项的选择 [英] Custom ListView with single choice selection

查看:193
本文介绍了自定义的ListView单选项的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的定制的LinearLayout它实现可勾选接口。问题是我能够选择多个项目,即使我已经定义
     listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

I am using following customized LinearLayout which implements Checkable interface. The problem is I am able to select multiple items even though I have defined listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

任何想法可能是错误的?

Any idea what could be wrong ?

package in.co.madhur.ganalyticsdashclock;

import java.util.ArrayList;
import java.util.List;

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

public class CheckedLinearLayout extends LinearLayout implements Checkable
{

    // RadioButton radioButton;
    private boolean isChecked;
    private List<Checkable> checkableViews=new ArrayList<Checkable>();

    public CheckedLinearLayout(Context context, AttributeSet attrs)
    {

        super(context, attrs);
    }

    public CheckedLinearLayout(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

    }

    public CheckedLinearLayout(Context context)
    {
        super(context);
    }

    @Override
    public boolean isChecked()
    {

        return isChecked;
    }

    @Override
    public void setChecked(boolean checked)
    {
        for (Checkable c : checkableViews)
        {
            // Pass the information to all the child Checkable widgets
            c.setChecked(isChecked);
        }

    }

    @Override
    public void toggle()
    {
        this.isChecked = !this.isChecked;
        for (Checkable c : checkableViews)
        {
            // Pass the information to all the child Checkable widgets
            c.toggle();
        }

    }

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();
        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; ++i)
        {
            findCheckableChildren(this.getChildAt(i));
        }
    }

    /**
     * Add to our checkable list all the children of the view that implement the
     * interface Checkable
     */
    private void findCheckableChildren(View v)
    {
        if (v instanceof Checkable)
        {
            this.checkableViews.add((Checkable) v);
        }
        if (v instanceof ViewGroup)
        {
            final ViewGroup vg = (ViewGroup) v;
            final int childCount = vg.getChildCount();
            for (int i = 0; i < childCount; ++i)
            {
                findCheckableChildren(vg.getChildAt(i));
            }
        }
    }

}

以下是列表视图我的单排布局:

Following is my layout of single row in list view:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/AccountTypeImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:contentDescription="AccountImage"
        android:src="@drawable/location_web_site" />

    <TextView
        android:id="@+id/property_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/AccountTypeImage"
        android:ellipsize="end"
        android:paddingLeft="25dp"
        android:text="Header"
        android:textColor="@color/app_text_main_light_color"
        android:textSize="@dimen/text_size_large" />

    <View
        android:id="@+id/profile_header_line"
        android:layout_width="fill_parent"
        android:layout_height="2.0px"
        android:layout_alignParentBottom="true"
        android:background="@color/app_text_main_light_color" />
</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radioselect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <TextView
        android:id="@+id/profile_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="25dp"
        android:text="Profile"
        android:textSize="@dimen/text_size_medium" />
</LinearLayout>

<Space
    android:layout_width="match_parent"
    android:layout_height="10dp" />

推荐答案

我也碰到了同样的问题,我用 RelativeLayout的

I also came across the same problem, I used RelativeLayout

试试这样

public class CheckedLinearLayout extends RelativeLayout implements
        Checkable {

    private boolean isChecked;
    private List<Checkable> checkableViews;

    public CheckedLinearLayout (Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        initialise(attrs);
    }

    public CheckedLinearLayout (Context context, AttributeSet attrs) {
        super(context, attrs);
        initialise(attrs);
    }

    public CheckedLinearLayout (Context context, int checkableId) {
        super(context);
        initialise(null);
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
        for (Checkable c : checkableViews) {
            c.setChecked(isChecked);
        }
    }
    public void toggle() {
        this.isChecked = !this.isChecked;
        for (Checkable c : checkableViews) {
            c.toggle();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            findCheckableChildren(this.getChildAt(i));
        }
    }

    /**
     * Read the custom XML attributes
     */
    private void initialise(AttributeSet attrs) {
        this.isChecked = false;
        this.checkableViews = new ArrayList<Checkable>(5);
    }

    /**
     * Add to our checkable list all the children of the view that implement the
     * interface Checkable
     */
    private void findCheckableChildren(View v) {
        if (v instanceof Checkable) {
            this.checkableViews.add((Checkable) v);
        }

        if (v instanceof ViewGroup) {
            final ViewGroup vg = (ViewGroup) v;
            final int childCount = vg.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                findCheckableChildren(vg.getChildAt(i));
            }
        }
    }
}

您可以通过以下链接

http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html

其具有对自定义单一选择的ListView

这篇关于自定义的ListView单选项的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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