自定义ListView具有单选选项

时间:2013-11-06 13:02:33

标签: java android android-layout listview

我正在使用以下定制的LinearLayout来实现Checkable接口。问题是我能够选择多个项目,即使我已经定义了     listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

知道可能出现什么问题吗?

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

}

以下是我在列表视图中的单行布局:

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

1 个答案:

答案 0 :(得分:2)

我也遇到了同样的问题,我使用了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

它在Custom Single Choice ListView

上有一个例子