java - 具有单项选择的自定义 ListView

标签 java android android-layout listview

我正在使用以下自定义的 LinearLayout,它实现了 Checkable 接口(interface)。问题是我可以选择多个项目,即使我已经定义了 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));
            }
        }
    }

}

以下是我在 ListView 中的单行布局:

<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

尝试这样

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的示例

关于java - 具有单项选择的自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19812809/

相关文章:

Java Swing JButton

java - NIO:发送消息然后立即断开连接

android - 更新 gradle 问题后依赖

java - Android:数组适配器不显示ListView

android - 在 onCreate 中从 NavigationDrawer 内的 ListView 中选择第一项?

java - 如何在 QuartzJobBean 的子类中获取 Session/Request 对象

java - 从 firebase 数据库中删除一个值

android - 如何更改 ListView 的文本大小

android - 使用滚动按钮时应用程序崩溃

android - 在android中实现布局时出错