2011-12-01 83 views
18

我想在一行中创建一个自定义列表视图,其中包含两个TextViews和一个单选按钮。并在listitem上单击单选按钮状态应该切换。我无法在这里使用简单适配器。自定义单选列表查看

我已经问过这个问题Single choice ListView custom Row Layout,但没有找到任何满意的解决方案。

我现在在做的是我正在使用simple_list_item_single_choice并将两个TextView的数据放在一个由一些空白分隔的单独的数据中。但在这里变得更糟(如下图所示)。

http://db.tt/ulP6pn7V

我要的是解决规模和价格的位置,使列表视图为单一选择。该列表

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>** 

如何让自定义适配器是什么?

+0

检查out this tutorial http://www.vogella.de/articles/AndroidListView/article.html –

+0

你可以在[GitHub](https://github.com/CabezasGonzalezJavier/NotifyDataSetChanged) – Cabezas

+0

检查这个以备将来参考** https://www.youtube.com/watch?v = s4JwU28VMko ** –

回答

12

我一上午都寻找答案这个问题,到目前为止,我已经找到了最有用的东西是this article

虽然我还没有实现它的建议,但它听起来像是在正确的轨道上。

基本上,单选ListView期望您提供的小部件实现Checkable接口。 LinearLayout等没有。因此,您需要创建一个继承LinearLayout(或您希望用于物品的任何布局)的自定义布局,并实现必要的接口。

+0

非常感谢!你真的很喜欢! – Nitin4Android

+1

绝对要走的路。该链接具有您所需的全部功能,只需使用该类并在您的XML中引用它即可(如图所示)。 – Fraggle

+0

非常感谢 - definetely helpful! –

-1

**

<TextView 
    android:id="@+id/tv_size" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:text="TextView" 
    android:textSize="15sp" 
    android:width="200dp" /> 

<TextView 
    android:id="@+id/tv_price" 
    android:layout_width="70dp" 
    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" /> 

**

+0

@ Chunhui: - 我已经完整地阅读过那篇文章,但是找不到想要的答案。 – Nitin4Android

+0

@ Shailendra Rajawat: - 不能得到你想说的。 – Nitin4Android

+0

android:layout_width当你想要特定的宽度时不应该是wrap_content。如果这个替换也无法工作共享代码和问题 –

90

我有类似的情况,但很容易修复。试试这个:

  1. 扩展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); 
    } 
    
  2. 创建一个字段来存储适配器

    int selectedIndex = -1; 
    
  3. 选择索引位置提供一个接口,它

    public void setSelectedIndex(int index){ 
        selectedIndex = index; 
    } 
    
  4. 设置选中状态为真或基于内部getView所选索引()

    @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); 
        } 
    } 
    
  5. Set单选按钮假可聚焦的和可点击attribs为 '假'

    <RadioButton 
        android:id="@+id/radio1" 
        android:checked="false" 
        android:focusable="false" 
        android:clickable="false" 
    /> 
    
  6. 集列表视图descendantFocusability attrib to'beforeDescendants'

    <ListView 
        android:id="@android:id/list" 
        android:choiceMode="singleChoice" 
        android:descendantFocusability="beforeDescendants" 
    /> 
    
  7. Override onListItemClick

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
        super.onListItemClick(l, v, position, id); 
        adapter.setSelectedIndex(position); 
        adapter.notifyDataSetChanged(); 
    } 
    

这it..run和检查

+4

谢谢,应该是接受的答案... – avb

+0

它对我有用,谢谢我已经把一个upvote – vijay

+0

真棒答案..没有自定义views..nothing .. – AndroidMech

3

处理的类似问题与哈克(不是一个完美的solution..but仍然有效。)

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); 



      } 
     }); 
+0

我可以说,这是美好的:) –