Nit*_*oid 19 android listview choice
我想在一行中创建一个自定义列表视图,其中包含两个TextView和一个单选按钮.在listitem上单击单选按钮状态应该切换.我不能在这里使用Simple Adapter.
我已经问过这个问题单选ListView自定义行布局但是找不到任何满意的解决方案.
我目前正在做的是我使用simple_list_item_single_choice并将两个TextView的数据放在一个单独的空格中.但在这里它变得越来越糟糕(如下图所示).

我想要的是修复大小和价格的位置,并将列表视图作为单一选择.
列表的XML布局可以是:
**<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15sp"
android:width="200dp" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15sp"
android:width="70dp" />
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>**
Run Code Online (Sandbox Code Playgroud)
如何制作自定义适配器?
skp*_*202 91
我有类似的情况,但很容易修复.试试这个:
扩展ListActivity并设置自定义列表适配器
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //if you need title to be hidden
setContentView(R.layout.activity_add_to_cart_select_service_plan);
setListAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)创建一个字段以在适配器中存储选定的索引位置
int selectedIndex = -1;
Run Code Online (Sandbox Code Playgroud)提供一个接口
public void setSelectedIndex(int index){
selectedIndex = index;
}
Run Code Online (Sandbox Code Playgroud)根据getView()中选定的索引将checked状态设置为true或false
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RadioButton rbSelect = (RadioButton) convertView
.findViewById(R.id.radio1);
if(selectedIndex == position){
rbSelect.setChecked(true);
}
else{
rbSelect.setChecked(false);
}
}
Run Code Online (Sandbox Code Playgroud)将单选按钮设置为可聚焦且可点击的属性为'false'
<RadioButton
android:id="@+id/radio1"
android:checked="false"
android:focusable="false"
android:clickable="false"
/>
Run Code Online (Sandbox Code Playgroud)设置listview descendantFocusability属于'beforeDescendants'
<ListView
android:id="@android:id/list"
android:choiceMode="singleChoice"
android:descendantFocusability="beforeDescendants"
/>
Run Code Online (Sandbox Code Playgroud)覆盖onListItemClick
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
adapter.setSelectedIndex(position);
adapter.notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)那就是..运行和检查
Jul*_*les 12
我整个上午都在寻找这个问题的答案,到目前为止我发现的最有用的东西就是这篇文章.
虽然我还没有实现它建议的解决方案,但听起来它似乎正在走上正轨.
基本上,单选择ListView期望您提供的小部件实现Checkable接口. LinearLayout等人没有.因此,您需要创建一个继承的自定义布局LinearLayout(或您希望用于项目的任何布局)并实现必要的界面.
用Hack处理了类似的问题(不是完美的解决方案,但仍然有效。)
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i < parent.getCount() ; i ++)
{
View v = parent.getChildAt(i);
RadioButton radio = (RadioButton) v.findViewById(R.id.radio);
radio.setChecked(false);
}
RadioButton radio = (RadioButton) view.findViewById(R.id.radio);
radio.setChecked(true);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53986 次 |
| 最近记录: |