在SingleChoice模式下使用RadioButtonCheckBox的Android ListView和自定义行布局

在SingleChoice模式下使用RadioButtonCheckBox的Android ListView和自定义行布局,第1张

概述我有一个ListView,它处于singleChoice模式.我想要的只是在侧面显示一个RadioButton,当点击高亮显示它被选中时,当点击另一个时,一个人隐藏并且新一个被选中.我查看了Mark的书,第8章,“用列表获得花哨”和他的RateList示例但它并没有解决我的问题.请帮我.解决方法:编辑值得一提

我有一个ListVIEw,它处于singleChoice模式.我想要的只是在侧面显示一个Radiobutton,当点击高亮显示它被选中时,当点击另一个时,一个人隐藏并且新一个被选中.

我查看了Mark的书,第8章,“用列表获得花哨”和他的RateList示例
但它并没有解决我的问题.
请帮我 .

解决方法:

编辑

值得一提的是我在列表项上有一个@R_404_5971@:有一个图标,一个标题,一个描述,然后是复选框或单选按钮(取决于它是单个还是多个选项列表).我的示例解决方案是由不少于三个不同部分描述的:

>自定义列表项目
>甜蜜温柔的爱:CheckablelinearLayout实现
> ListVIEw配置示例
还有奖金:
>示例Adapter :: getVIEw()实现.

那么,让我们接受魔术,不管吗?

Listitem.xml

<com.dbm.CheckablelinearLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:orIEntation="horizontal">    <ImageVIEw        androID:layout_wIDth="32dp"        androID:layout_height="32dp"        androID:ID="@+ID/myIcon" />    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_weight="1"        androID:orIEntation="vertical">        <TextVIEw            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:textAppearance="@androID:style/TextAppearance.Medium"            androID:textStyle="bold"            androID:ellipsize="end"            androID:ID="@+ID/myTitle" />        <TextVIEw            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:textAppearance="@androID:style/TextAppearance.Small"            androID:textStyle="italic"            androID:ellipsize="end"            androID:ID="@+ID/myDescr" />    </linearLayout>    <CheckedTextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:ID="@androID:ID/text1" /></com.dbm.CheckablelinearLayout>

CheckablelinearLayout.java

public class CheckablelinearLayout extends linearLayout implements Checkable {    private CheckedTextVIEw mCheckedTextVIEw;    private final Drawable mCheckDrawable;    private final Drawable mRadioDrawable;    private boolean mIsChecked;       /**        * Constructor.        *        * @param context The context to operate in.        * @param attrs The attributes defined in XML for this element.        */    public CheckablelinearLayout(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray typedArray = null;        // Cache the check Box drawable.        typedArray = context.gettheme().obtainStyledAttributes(new int[] {androID.R.attr.ListChoiceIndicatorMultiple});        if ((typedArray != null) && (typedArray.length() > 0)) {            mCheckDrawable = typedArray.getDrawable(0);        }        else {            // Fallback if the target theme doesn't define a check Box drawable.            // Perhaps an application specific drawable should be used instead of null.            mCheckDrawable = null;        }        // Careful with resources like this, we don't need any memory leaks.        typedArray.recycle();        // Cache the radio button drawable.        typedArray = context.gettheme().obtainStyledAttributes(new int[] {androID.R.attr.ListChoiceIndicatorSingle});        if ((typedArray != null) && (typedArray.length() > 0)) {            mRadioDrawable = typedArray.getDrawable(0);        }        else {            // Fallback if the target theme doesn't define a radio button drawable.            // Perhaps an application specific drawable should be used instead of null            mRadioDrawable = null;        }        // Careful with resources like this, we don't need any memory leaks.        typedArray.recycle();        mIsChecked = false;    }    /*     * (non-Javadoc)     * @see androID.Widget.Checkable#isChecked()     */    public boolean isChecked() {        return mIsChecked;    }    /*     * (non-Javadoc)     * @see androID.vIEw.VIEw#onAttachedToWindow()     */    @OverrIDe    protected voID onAttachedToWindow() {        super.onAttachedToWindow();        // Check if there is a valID GUI element that can visualize the current check-state.        if (mCheckedTextVIEw != null) {            VIEwParent p = getParent();            // Check if the parent of this List item is a ListVIEw            if (p instanceof ListVIEw) {                int choiceMode = ((ListVIEw) p).getChoiceMode();                // DecIDe which check-state notation to visualize (check Box, radio button or none).                switch (choiceMode) {                    case ListVIEw.CHOICE_MODE_MulTIPLE:                        mCheckedTextVIEw.setcheckmarkDrawable(mCheckDrawable);                        break;                    case ListVIEw.CHOICE_MODE_SINGLE:                        mCheckedTextVIEw.setcheckmarkDrawable(mRadioDrawable);                        break;                    default:                        mCheckedTextVIEw.setcheckmarkDrawable(null);                        break;                }            }        }    }    /*     * (non-Javadoc)     * @see androID.vIEw.VIEw#onFinishInflate()     */    @OverrIDe    protected voID onFinishInflate() {        super.onFinishInflate();        mCheckedTextVIEw = (CheckedTextVIEw) findVIEwByID(androID.R.ID.text1);    }    /*     * (non-Javadoc)     * @see androID.Widget.Checkable#setChecked(boolean)     */    public voID setChecked(boolean checked) {        mIsChecked = checked;        if (mCheckedTextVIEw != null) {            mCheckedTextVIEw.setChecked(mIsChecked);        }    }    /*     * (non-Javadoc)     * @see androID.Widget.Checkable#toggle()     */    public voID toggle() {        setChecked(!mIsChecked);    }}

exampleListVIEw.xml

注意!如果您将androID:choiceMode属性设置为“multipleChoice”,则会自动获取复选框,如果您将其设置为“singleChoice”,则会自动获取复选框,前提是您使用上述实现.

<linearLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent">    <ListVIEw        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:choiceMode="singleChoice"        androID:ID="@+ID/myList" /></linearLayout>

额外奖励:MyCustomAdapter :: getVIEw()

这个依赖于Cursor.当然,您将根据自己的需要实施它.

private final class VIEwHolder {    public ImageVIEw iconVIEw;    public TextVIEw TitleVIEw;    public TextVIEw descriptionVIEw;}/* * (non-Javadoc) * @see androID.Widget.Adapter#getVIEw(int, androID.vIEw.VIEw, androID.vIEw.VIEwGroup) */public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {    VIEw vIEw = null;    // Only do something if the requested position exists within the Cursor.    if (mCursor.movetoposition(position)) {        VIEwHolder vIEwHolder;        vIEw = convertVIEw;        if (vIEw == null) {            // Create and initialize a new vIEw if not created already for this position.            vIEw = mLayoutInflater.inflate(R.layout.Listitem, null);            // Don't "find vIEw by ID" each and every time, but rather save a reference            // to them and associate the references with the List item itself by storing             // them in the List items "tag" attribute. When the vIEw is re-used later on,             // you already have a reference to its vIEws and don't need to find them             // again, which is a time-consuming operation.            vIEwHolder = new VIEwHolder();            vIEwHolder.iconVIEw = (ImageVIEw) vIEw.findVIEwByID(R.ID.myIcon);            vIEwHolder.TitleVIEw = (TextVIEw) vIEw.findVIEwByID(R.ID.myTitle);            vIEwHolder.descriptionVIEw = (TextVIEw) vIEw.findVIEwByID(R.ID.myDescr);            vIEw.setTag(vIEwHolder);        }        else {            // Get the references to the vIEws for this, already existing List item.            vIEwHolder = (VIEwHolder) vIEw.getTag();        }        // Create a bitmap from the byte array in the database.        byte[] buffer = mCursor.getBlob(mIconColumnIndex);        Bitmap icon = null;        // Try to decode the byte array if it exists.        if (buffer != null) {            icon = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);        }        // Update the vIEws with new data.        vIEwHolder.iconVIEw.setimageBitmap(icon);        String Title = mCursor.getString(mTitleColumnIndex);        vIEwHolder.TitleVIEw.setText(Title);        String description = mCursor.getString(mDescriptionColumnIndex);        vIEwHolder.descriptionVIEw.setText(description);    }    // Return a vIEw showing the correct data for the item at 'position'.    return vIEw;}

原始答案:

我可以建议这个链接:

http://tokudu.com/2010/android-checkable-linear-layout/

当我处于你的确切位置时,我自己非常高兴:-)如果还有什么不清楚的地方,请随时指出你的问题,我很乐意帮助或协助进一步的代码示例(正如前面提到的那样:几天前我一直在你的位置.

总结

以上是内存溢出为你收集整理的在SingleChoice模式下使用RadioButton / CheckBox的Android ListView和自定义行布局全部内容,希望文章能够帮你解决在SingleChoice模式下使用RadioButton / CheckBox的Android ListView和自定义行布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1102032.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存