Android 自定义listview上的复选框

Android 自定义listview上的复选框,android,radio-button,Android,Radio Button,我越来越喜欢这样,它不应该只显示在特定的项目 如何将自定义listview中的单选按钮设置为单一模式。如果我点击某个单选按钮,那么它必须出现,而另一个按钮则为未选中。如果单击第一行,则选中第一行上的单选按钮。单击第二行上的单选按钮,选中第二行,未选中第一行。单击第三行上的单选按钮,选中第一行,未选中第二行 customlist.xml listView=(listView)findViewById(R.id.cardlist); adapter=新的MyAdapter(这个,app.arryL

我越来越喜欢这样,它不应该只显示在特定的项目 如何将自定义listview中的单选按钮设置为单一模式。如果我点击某个单选按钮,那么它必须出现,而另一个按钮则为未选中。如果单击第一行,则选中第一行上的单选按钮。单击第二行上的单选按钮,选中第二行,未选中第一行。单击第三行上的单选按钮,选中第一行,未选中第二行

customlist.xml


listView=(listView)findViewById(R.id.cardlist);
adapter=新的MyAdapter(这个,app.arryList,app.arryList 1);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
Toast.makeText(getApplicationContext(),“am thelist”,Toast.LENGTH\u LONG.show();
}
});
setAdapter(适配器);
listView.setChoiceMode(listView.CHOICE\u MODE\u SINGLE);
}
公共类MyAdapter扩展了BaseAdapter{
Context=null;
arraylistitems=null;
ArrayList items1=null;
公共MyAdapter(新卡、新卡、阵列列表项、,
ArrayList项目1){
//TODO自动生成的构造函数存根
这个项目=项目;
this.items1=items1;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回items.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
退货项目;
//返回项目。获取(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图布局=空;
TextView produ=null;
TextView desc=null;
按钮编辑=空;
RadioButton单选=空;
if(convertView==null){
lay=LayoutInflater.from(getApplicationContext());
布局=铺放充气(R.layout.customlist,空);
}否则{
布局=转换视图;
}
produ=(TextView)layout.findviewbyd(R.id.card);
produ.setText(“+app.arryList.get(position));
单选=(RadioButton)布局。findViewById(R.id.radioButton1);
radio.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
//check.setVisibility(View.GONE);
System.out.println(“数据”+app.arryList.get(位置));
}
});
}
});
返回布局;

尝试此操作,使用
ListView。选择\u MODE\u MULTIPLE

 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

我认为
ListView.CHOICE\u MODE\u SINGLE
更适合他的需要


但是,保存一组单选按钮并进行相应设置可能是一个不错的主意。

我试图解决您的问题,并从我的角度完成了这项工作

我将在下面解释这种方法,更多细节和完整的源代码请浏览下面的博客

希望这能解决你的问题

我创建了自定义单选列表。您可以更改布局并相应地添加小部件

步骤1)

步骤3)


步骤4)
步骤5)single_radio_chice.xml

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item android:state_checked="false" 
            android:drawable="@drawable/radio_off" />

        <item android:state_checked="true" 
            android:drawable="@drawable/radio_on" />
    </selector>


我使用ListView.CHOICE\u MODE\u SINGLE和istView.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE)进行了检查;两者都不起作用。我们怎样才能使最初默认选中的radiobutton生效?
package com.custom.view;

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.RelativeLayout;


public class CheckableRelativeLayout extends RelativeLayout implements
        Checkable {

    private boolean isChecked;
    private List<Checkable> checkableViews;

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

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

    public CheckableRelativeLayout(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));
            }
        }
    }
}
package com.custom.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.CheckBox;
public class InertCheckBox extends CheckBox {

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

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

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyShortcut(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        return false;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<com.custom.view.CheckableRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_marginRight="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">


    <com.custom.view.InertCheckBox 
        android:id="@+id/singleitemCheckBox"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:focusable="false" 
        android:button="@drawable/single_radio_chice" />

    <TextView
        android:id="@+id/singleitemId" 
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" 
        android:layout_toRightOf="@+id/singleitemCheckBox"
        android:layout_centerVertical="true" 
        android:layout_marginLeft="20dp"
        android:textSize="14sp" 
        android:focusable="false" />

    <TextView
        android:id="@+id/nextitem" 
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" 
        android:layout_toRightOf="@+id/singleitemId"
        android:layout_centerVertical="true" 
        android:layout_marginLeft="20dp"
        android:textSize="14sp" 
        android:focusable="false" />

</com.custom.view.CheckableRelativeLayout>


Step4)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:choiceMode="singleChoice"
        android:listSelector="#00000000" />

</RelativeLayout>
 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item android:state_checked="false" 
            android:drawable="@drawable/radio_off" />

        <item android:state_checked="true" 
            android:drawable="@drawable/radio_on" />
    </selector>