本次设计的思想是利用listview的item单击动作控制RadioButton的显示
mail.xml

 <ListView
            android:id="@+id/chooselanguage_list"
            android:layout_width="match_parent"
            android:layout_height="492dip" />

 


item.xml:在item行的样式设计中,最重要的是要阻断item中控件的事件android:descendantFocusability="blocksDescendants";并且将RadioButton的clicable设置为false。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:gravity="center_vertical"
   android:descendantFocusability="blocksDescendants"
   >
<!--  android:descendantFocusability="blocksDescendants" -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="65dip"
        android:layout_marginLeft="15dip"
        android:singleLine="true"
        android:textColor="#5A6361"
        android:gravity="center_vertical"
        android:textSize="26sp" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="65dip"
        android:layout_alignParentRight="true" >

        <RadioButton
            android:id="@+id/radiobutton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="false"
            android:paddingRight="45dip" />
    </RelativeLayout>

</RelativeLayout>

 .java

private int clickPosition = -1;
@Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            // TODO Auto-generated method stub
            ViewHolder holder = null;
            if (arg1 == null) {
                holder = new ViewHolder();
                arg1 = mInflater.inflate(R.layout.chooselanguage_item, null);

                holder.title = (TextView) arg1.findViewById(R.id.title);

                holder.RadioBtn = (RadioButton) arg1
                        .findViewById(R.id.radiobutton);
                arg1.setTag(holder);

            } 
          holder = (ViewHolder) arg1.getTag();

            

            holder.title.setText((String) mData.get(arg0).get("title"));
            // holder.RadioBtn.setChecked((Boolean)mData.get(arg0).get("choice"));
            if (clickPosition == arg0) {
                holder.RadioBtn.setChecked(true);
                
            } else {
                holder.RadioBtn.setChecked(false);
                
            }
            
            
            return arg1;

        }

    }
@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        clickPosition = arg2;
         mAdapter.notifyDataSetInvalidated();
    }

 

posted on 2013-07-23 15:06  郭建伟  阅读(2122)  评论(0)    收藏  举报