1,需求就是点击列表的其中任意一项,显示图片,重复点击图片依然显示,其他不选中不显示图片
2.一开始看网上都是用radiobutton来实现单选功能,发现滚动的时候被回收了,并不能记住图片的显示
3.最后参考了网上的做法,自定义一个getview里面的item项
首先,listview的xml文件
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="@dimen/y390"
android:layout_below="@+id/tv_sleep_temperature_number"
android:layout_marginLeft="@dimen/x40"
android:layout_marginRight="@dimen/x40"
android:layout_marginTop="@dimen/y52"
android:cacheColorHint="@android:color/transparent"
android:choiceMode="singleChoice"
android:listSelector="@android:color/transparent"
android:overScrollMode="never"
android:scrollbars="none" />
android:choiceMode=”singleChoice”
实现单选模式
个人建议可以去掉右边的scrollcbar,overScrollMode都为never
android:overScrollMode="never"
android:scrollbars="none"
单选自定义view(item),继承lineralayout,实现checkabe接口,重写ischeck,setchecked的方法。
我们可以ctrl点击进去看到 再ctrl+H
设置当前view选中 setchecked。ischecked 返回当前选中view的布尔值,toggle开关,改变view的相反状态。。
可以看到实现checkable接口的widget。。轻松实现单选功能
public class ChoiceListItemView extends LinearLayout implements Checkable{
private TextView nameTxt;
private CheckBox selectBtn;
public ChoiceListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item_temperature_list_view, this, true);
nameTxt = (TextView) v.findViewById(R.id.item_temperature);
selectBtn = (CheckBox) v.findViewById(R.id.cb_select);
}
public void setName(String text) {
nameTxt.setText(text);
}
@Override
public boolean isChecked() {
return selectBtn.isChecked();
}
@Override
public void setChecked(boolean checked) {
selectBtn.setChecked(checked);
//根据是否选中来选择不同的背景图片
if (checked) {
selectBtn.setBackgroundResource(R.drawable.icon_setting_right);
} else {
selectBtn.setBackgroundResource(R.color.transparent);
}
}
@Override
public void toggle() {
selectBtn.toggle();
}
}
判断当前是否被点中
@Override
public boolean isChecked() {
return selectBtn.isChecked();
}
根据是否点击选中listview的item,改变背景,选中就显示勾,否则就显示透明的占位图。(假如直接不设置不选中的展位图,效果不能实现)
@Override
public void setChecked(boolean checked) {
selectBtn.setChecked(checked);
//根据是否选中来选择不同的背景图片
if (checked) {
selectBtn.setBackgroundResource(R.drawable.icon_setting_right);
} else {
selectBtn.setBackgroundResource(R.color.transparent);
}
}
checkbox的开关
@Override
public void toggle() {
selectBtn.toggle();
}
item的xml布局,左边文字,右边用checkbox替代radiobutton实现单选功能
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/y78"
android:minHeight="@dimen/y78">
<TextView
android:id="@+id/item_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/x24"
android:text=""
android:textSize="@dimen/x64" />
<CheckBox
android:id="@+id/cb_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/x20"
android:button="@null"
android:checked="false"
android:clickable="false"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/y2"
android:layout_alignParentBottom="true"
android:background="@color/header_line_bottom" />
</RelativeLayout>
最后来实现一个简单的适配器
public class RadioAdapter extends BaseAdapter{
private List<String> tempretures;
private Context c;
public RadioAdapter(Context c, List<String> tempretures) {
super();
this.c = c;
this.tempretures = tempretures;
}
@Override
public int getCount() {
return tempretures.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ChoiceListItemView choiceListItemView = new ChoiceListItemView(c, null);
choiceListItemView.setName(tempretures.get(position));
return choiceListItemView;
}
}
构造方法返回数据的size,复写getview。在里面直接new一个刚才自定义的ChoiceListItemView,加载一点简单数据,return 自定义ChoiceListItemView。OK..
当然没有用来加载网络数据。。。new了很多item。可以继续研究。。