布局
单行布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin14"
android:layout_marginLeft="@dimen/margin14"
android:orientation="horizontal">
<TextView
android:id="@+id/item_addressName"
style="@style/orderText"
android:text=""/>
<TextView
android:layout_marginLeft="@dimen/margin30"
android:id="@+id/item_addressPhone"
style="@style/orderText"
android:text="1111"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/margin14"
android:layout_marginTop="@dimen/margin10"
android:layout_marginLeft="@dimen/margin14"
android:layout_marginBottom="@dimen/margin14"
android:orientation="horizontal">
<TextView
android:id="@+id/item_addressAddress"
style="@style/orderText"
android:layout_weight="1"
android:text=""/>
<TextView
android:layout_marginLeft="@dimen/margin30"
android:id="@+id/item_addressPostcode"
style="@style/orderText"
android:text="100000"/>
</LinearLayout>
<include layout="@layout/liner1"></include>
<RelativeLayout
android:layout_marginBottom="@dimen/margin10"
android:layout_width="match_parent"
android:layout_marginTop="@dimen/margin14"
android:layout_marginLeft="@dimen/margin16"
android:layout_marginRight="@dimen/margin16"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/item_shopping_check"
style="@style/check"
android:paddingRight="@dimen/margin14"
android:paddingBottom="@dimen/margin14"
android:paddingTop="@dimen/margin8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设为默认"
android:textSize="@dimen/margin14"
android:textColor="@color/black"
/>
<TextView
android:layout_toLeftOf="@+id/orderEditor"
android:id="@+id/orderDelete"
style="@style/address"
android:text="删除"
android:textColor="@color/black"></TextView>
<TextView
android:layout_alignParentRight="true"
android:id="@+id/orderEditor"
style="@style/address"
android:layout_marginRight="0dp"
android:text="编辑"
android:background="@color/bottomFont"
android:textColor="@color/white"></TextView>
</RelativeLayout>
<include layout="@layout/liner"></include>
</LinearLayout>
主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/address_recyclerview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
实现单选的主要代码
第一种方法:bean里面check来判断
mCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (addressInfo info:mDataList){
info.setCheck(false);
}
mDataList.get(position).setCheck(true);
notifyDataSetChanged();
}
});
if (data.isCheck()) {
mCheck.setChecked(true);
} else {
mCheck.setChecked(false);
}
第二种通过Map记录状态实现
private Map<Integer, Boolean> isCheck = new HashMap<Integer, Boolean>();
chSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setStates(position,true);
notifyDataSetChanged();
}
});
/**
* 设置状态
*/
if (getStates(position)==null){
isCheck.put(position,false);
}
chSelect.setChecked(isCheck.get(position));
/**
* 设置默认的状态值
*
* @param positon
*/
public void setStates(int positon, boolean b) {
for (int i = 0; i < mDataList.size(); i++) {
isCheck.put(i,false);
}
isCheck.put(positon,true);
}