gpt4 book ai didi

android - 单选ListView自定义Row Layout

转载 作者:行者123 更新时间:2023-11-29 02:09:32 27 4
gpt4 key购买 nike

我只想制作一个带有两个 TextView(并排成一行)的单选 ListView。

实际上,我想让它让用户根据其尺寸价格值选择产品。这些值显示在该 ListView 中,这两个 TextView 表示 Size-Price 值。

问题是我不能使它成为一个单一的选择列表(也在它前面显示一个单选按钮)。下面是我正在使用的行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/rowLayout" >
<TextView
android:id="@+id/tv_size"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="Size"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:textSize="15sp" />
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
</LinearLayout>

请告诉我如何用 Java 编写代码。如果可以使用 simple_list_item_2 布局那么如何?

最佳答案

我不确定我们是否可以在 ListView 中创建单选按钮组,我正在做的只是在 Java 中处理选择。示例:

    @Override
public View getView(int position, View view, ViewGroup viewGroup) {
final ViewHolder holder;

if (view == null) {
holder = new ViewHolder();

view = mInflater.inflate(R.layout.customer_invoice_row, null);

holder.selectionRB = (RadioButton) view
.findViewById(R.id.selectionRB);
holder.sizeTV = (TextView) view
.findViewById(R.id.sizeTV);
holder.priceTV = (TextView) view
.findViewById(R.id.priceTV);

holder.selectionRB.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
RadioButton rb = (RadioButton) v;
if (mSelectedRB != null) {
if (!rb.getTag().equals(mSelectedRB.getTag()))
mSelectedRB.setChecked(false);

}
mSelectedRB = rb;

}
});

view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}


holder.position = position;

holder.selectionRB.setTag(productObj);
holder.sizeTV.setText(productObj.getSize());
holder.priceTV.setText(productObj.getPrice());
return view;
}

持有者:[可以是内部类]

class ViewHolder {
int position;

RadioButton selectionRB;
TextView sizeTV;
TextView priceTV;
}

并且 mSelectedRB 是您 Activity 的全局成员。

关于android - 单选ListView自定义Row Layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8295026/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com