2011-12-01 59 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

我可以說,這是美好的:) –