0

我有一个具有以下布局的自定义 listView,我希望它的行为类似于普通的 SingleChoiceListView。虽然我为 Single_Choice_Mode 设置了列表视图,但是当我单击时,单选按钮没有被选中。我究竟做错了什么?单行的 xml 布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center_vertical"
          android:minHeight="?listPreferredItemHeightSmall"
          android:orientation="horizontal"
          android:clickable="true"
          android:focusable="true"
          android:paddingLeft="16dip"
          android:paddingRight="12dip" >
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:orientation="vertical" >
        <TextView
                android:id="@android:id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:gravity="center_vertical|left"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="?textColorAlertDialogListItem" />
        <TextView
                android:id="@android:id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:gravity="center_vertical|left"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="?textColorAlertDialogListItem" />

    </LinearLayout>

    <TextView
            android:id="@+id/preco"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:ellipsize="marquee"
            android:gravity="fill"
            android:singleLine="true"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/appgreen" />
    <RadioButton
            android:id="@+id/radio"
            android:layout_width="35dip"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:focusable="false"
            android:gravity="center_vertical"
            android:paddingRight="12dip" />
</LinearLayout>
4

3 回答 3

0

我认为当您单击列表视图中的某个项目时 - 列表视图在您的复选框之前处理此单击。您可以覆盖 listView 上的 onItemClickListener 并在其中通过自定义布局中的 findViewId 找到您的复选框,然后以编程方式更改复选框的状态。

于 2013-01-24T13:23:57.567 回答
0

使用它代替android.r.layout.simple_list_item_single_choice. 创建你自己的r.layout.my_single_choice.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:gravity="center_vertical"
    android:padding="5dip"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/black"
    android:textStyle="bold" >
</CheckedTextView>
于 2013-09-20T05:12:36.010 回答
0

我知道我有点晚了,但我刚刚遇到同样的问题并在这里找到了解决方案(翻译): https ://extra--vision-blogspot-com.translate.goog/2016/07/android-single -choice-listview.html?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en-US&_x_tr_pto=nui

我使用了一个自定义适配器来保存所选项目的位置,就像这个例子中的那样:

public final class SingleChoiceAdapter extends ArrayAdapter<String> {
    private static final String[] FRUITS = {
        "Apple", "Banana", "Lemon", "Orange", "Grape"
    };
    private final LayoutInflater mInflater;
    private int mSelectedIndex = -1;
    SingleChoiceAdapter(Context context) {
        super(context, 0);
        addAll(FRUITS);
        mInflater = LayoutInflater.from(context);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = mInflater.inflate(R.layout.item_layout, parent, false);
        }
    
        TextView textView       = (TextView)    view.findViewById(R.id.text_view);
        RadioButton radioButton = (RadioButton) view.findViewById(R.id.radio_button);
        String fruit = getItem(position);
        textView.setText(fruit);
        radioButton.setChecked(position == mSelectedIndex);
        return view;
    }
    void setSelectedIndex(int index) {
        mSelectedIndex = index;
        notifyDataSetChanged();
    }
}

然后setOnItemClicklistener在列表视图上实现一个,就像这个例子:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView listView = new ListView(this);
        listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        final SingleChoiceAdapter adapter = new SingleChoiceAdapter(this);
        listView.setOnItemClickListener(new     AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                adapter.setSelectedIndex(i);
            }
        });
        listView.setAdapter(adapter);
        setContentView(listView);
    }
}
于 2021-11-01T04:01:01.963 回答