2013-03-15 68 views
0

enter image description here我越來越喜歡它,它不應該只顯示在特定項目上 如何使自定義列表視圖中的單選按鈕爲單一模式。如果我點擊特定的單選按鈕,那麼它必須出現,另一個未選中。如果點擊第一行,然後選中第一行的單選按鈕。點擊第二行選擇的第二行放射按鈕,並取消選擇第一行。點擊第三行選中的第三行單選按鈕,未選中第一行和第二行。自定義列表視圖上的複選框

customlist.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<RadioButton 
     android:id="@+id/radioButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:text="" 

     /> 

    <TextView 
     android:id="@+id/card" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="10dip" android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/radioButton1" 
     android:text="" 
     android:textColor="#000000" 
     android:textSize="15dip" 
     android:textStyle="bold" /> 


    </RelativeLayout> 

    listView = (ListView) findViewById(R.id.cardlist); 

adapter =new MyAdapter(this, app.arryList,app.arryList1); 

listView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 

     Toast.makeText(getApplicationContext(), "am thelist",Toast.LENGTH_LONG).show(); 


    } 
}); 


listView.setAdapter(adapter); 

listView.setChoiceMode(listView.CHOICE_MODE_SINGLE); 
} 



public class MyAdapter extends BaseAdapter { 

Context context = null; 
ArrayList<String> items= null; 
ArrayList<String> items1= null; 


public MyAdapter(Newcard newcard, ArrayList<String> items, 
     ArrayList<String> items1) { 
    // TODO Auto-generated constructor stub 

    this.items = items; 
    this.items1 = items1; 

} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return items.size(); 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return items; 

    //return items.get(position); 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    View layout = null; 
    TextView produ = null; 

    TextView desc = null; 
    Button edit = null; 

    RadioButton radio =null; 

    if (convertView == null) { 

     lay = LayoutInflater.from(getApplicationContext()); 
     layout = lay.inflate(R.layout.customlist, null); 
    } else { 
     layout = convertView; 
    } 

    produ = (TextView) layout.findViewById(R.id.card); 
    produ.setText("" +app.arryList.get(position)); 


    radio = (RadioButton) layout.findViewById(R.id.radioButton1); 



    radio.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

         //check.setVisibility(View.GONE); 


      System.out.println("data "+app.arryList.get(position)); 

     } 
    }); 



     } 
    }); 



    return layout; 

回答

0

我試圖解決你的問題,並從我身邊完成。

我在下面解釋方法,瞭解更多細節和完整源代碼請通過下面的博客。

http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html

希望這將解決您的問題。

enter image description here

我創建的自定義單一選擇列表。您可以更改佈局並相應地添加小部件。

第一步)

package com.custom.view; 

import java.util.ArrayList; 
import java.util.List; 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Checkable; 
import android.widget.RelativeLayout; 


public class CheckableRelativeLayout extends RelativeLayout implements 
     Checkable { 

    private boolean isChecked; 
    private List<Checkable> checkableViews; 

    public CheckableRelativeLayout(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
     initialise(attrs); 
    } 

    public CheckableRelativeLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initialise(attrs); 
    } 

    public CheckableRelativeLayout(Context context, int checkableId) { 
     super(context); 
     initialise(null); 
    } 

    public boolean isChecked() { 
     return isChecked; 
    } 

    public void setChecked(boolean isChecked) { 
     this.isChecked = isChecked; 
     for (Checkable c : checkableViews) { 
      c.setChecked(isChecked); 
     } 
    } 
    public void toggle() { 
     this.isChecked = !this.isChecked; 
     for (Checkable c : checkableViews) { 
      c.toggle(); 
     } 
    } 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 

     final int childCount = this.getChildCount(); 
     for (int i = 0; i < childCount; ++i) { 
      findCheckableChildren(this.getChildAt(i)); 
     } 
    } 

    /** 
    * Read the custom XML attributes 
    */ 
    private void initialise(AttributeSet attrs) { 
     this.isChecked = false; 
     this.checkableViews = new ArrayList<Checkable>(5); 
    } 

    /** 
    * Add to our checkable list all the children of the view that implement the 
    * interface Checkable 
    */ 
    private void findCheckableChildren(View v) { 
     if (v instanceof Checkable) { 
      this.checkableViews.add((Checkable) v); 
     } 

     if (v instanceof ViewGroup) { 
      final ViewGroup vg = (ViewGroup) v; 
      final int childCount = vg.getChildCount(); 
      for (int i = 0; i < childCount; ++i) { 
       findCheckableChildren(vg.getChildAt(i)); 
      } 
     } 
    } 
} 

第二步)

package com.custom.view; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.widget.CheckBox; 
public class InertCheckBox extends CheckBox { 

    public InertCheckBox(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public InertCheckBox(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public InertCheckBox(Context context) { 
     super(context); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     return false; 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     return false; 
    } 

    @Override 
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { 
     return false; 
    } 

    @Override 
    public boolean onKeyPreIme(int keyCode, KeyEvent event) { 
     return false; 
    } 

    @Override 
    public boolean onKeyShortcut(int keyCode, KeyEvent event) { 
     return false; 
    } 

    @Override 
    public boolean onKeyUp(int keyCode, KeyEvent event) { 
     return false; 
    } 

    @Override 
    public boolean onTrackballEvent(MotionEvent event) { 
     return false; 
    } 
} 

步驟3)

<?xml version="1.0" encoding="utf-8"?> 
<com.custom.view.CheckableRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginRight="10dp" 
    android:paddingTop="5dp" 
    android:paddingBottom="5dp"> 


    <com.custom.view.InertCheckBox 
     android:id="@+id/singleitemCheckBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:focusable="false" 
     android:button="@drawable/single_radio_chice" /> 

    <TextView 
     android:id="@+id/singleitemId" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_toRightOf="@+id/singleitemCheckBox" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="20dp" 
     android:textSize="14sp" 
     android:focusable="false" /> 

    <TextView 
     android:id="@+id/nextitem" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_toRightOf="@+id/singleitemId" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="20dp" 
     android:textSize="14sp" 
     android:focusable="false" /> 

</com.custom.view.CheckableRelativeLayout> 


Step4) 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:cacheColorHint="#00000000" 
     android:divider="#b5b5b5" 
     android:dividerHeight="1dp" 
     android:choiceMode="singleChoice" 
     android:listSelector="#00000000" /> 

</RelativeLayout> 

步驟5)single_radio_chice.xml

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 

     <item android:state_checked="false" 
      android:drawable="@drawable/radio_off" /> 

     <item android:state_checked="true" 
      android:drawable="@drawable/radio_on" /> 
    </selector> 
+1

我們如何才能使默認情況下最初選中的單選按鈕? – AndroidGeek 2014-08-07 13:05:30

0

試試這個,使用ListView.CHOICE_MODE_MULTIPLE

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
0

我認爲ListView.CHOICE_MODE_SINGLE會更適合他的需要。

但也許這將是一個不錯的主意,保存您的單選按鈕的數組,並相應地設置它們。

+0

我檢查了ListView.CHOICE_MODE_SINGLE和istView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);兩者都不起作用 – arunk 2013-03-15 13:55:02

+0

可以幫助Emil如何解決 – arunk 2013-03-15 14:00:10

相關問題