一般在实现listview单选效果的时候,都是使用自定义Adapter中getview()方法里面,监听点击事件,然后根据不同的状态显示不同的效果,或者在listview中监听点击事件。 这一次通过实现Checkable接口来写单选。然后在getView()中使用这个类。 首先item的xml,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/payment_item_check_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/header_btn_background"
android:gravity="center_vertical"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/payment_style_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/login_btn_color"
android:textSize="21sp" />
<ImageView
android:id="@+id/payment_style_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/image_indictor_right"
android:visibility="gone" />
</RelativeLayout>
这个xml看自己的需要写了,我这里是单选改变字体颜色,背景颜色,勾选的图片显示出来。
然后是实现Checkable接口的类
/**
* 用于listview单选
*/
public class CheckableLayout extends RelativeLayout implements Checkable {
private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
public boolean mChecked;
private TextView styleTv;
private ImageView styleImg;
private RelativeLayout payment_item_check_layout;
public CheckableLayout(Context context) {
super(context);
View.inflate(context, R.layout.payment_style_item_layout, this);
styleTv = (TextView) findViewById(R.id.payment_style_tv);
styleImg = (ImageView) findViewById(R.id.payment_style_img);
payment_item_check_layout = (RelativeLayout) findViewById(R.id.payment_item_check_layout);
}
public void setText(String text){
styleTv.setText(text);
}
//设置是否选中。当我们点击item的时候,会调用这个方法
@Override
public void setChecked(boolean b) {
if (b != mChecked){
mChecked = b;
refreshDrawableState();
}
if (mChecked) {
payment_item_check_layout.setBackgroundColor(getResources().getColor(R.color.order_detail_bt_unpressed, getContext().getTheme()));
styleTv.setTextColor(getResources().getColor(R.color.order_detail_btn_background_color, getContext().getTheme()));
styleImg.setVisibility(View.VISIBLE);
} else {
payment_item_check_layout.setBackgroundColor(getResources().getColor(R.color.header_btn_background, getContext().getTheme()));
styleTv.setTextColor(getResources().getColor(R.color.login_btn_color, getContext().getTheme()));
styleImg.setVisibility(View.GONE);
}
}
//判断是否选中。
@Override
public boolean isChecked() {
return mChecked;
}
//开关,如果当前是选中的状态,调用该方法后取消选中,反之,选中
@Override
public void toggle() {
setChecked(!mChecked);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) mergeDrawableStates(drawableState, CHECKED_STATE_SET);
return drawableState;
}
public boolean getChecked(){
return mChecked;
}
}
之后便是listview使用了
payment_style_listview = (ListView) view.findViewById(R.id.payment_style_listview);
ListAdapter adapter = new ArrayAdapter<String>(context, R.layout.payment_style_item_layout, TypeNameArray){
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
CheckableLayout view;
if (convertView == null) {
view = new CheckableLayout(context);
} else {
view = (CheckableLayout) convertView;
}
view.setText(TypeNameArray[position]);
//在这里返回的view即是实现了单选效果的view
return view;
}
};
payment_style_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//这里可以知道单选的项目内容
}
});
payment_style_listview.setAdapter(adapter);
payment_style_listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//单选模式
payment_style_listview.setSelected(true);//只有这一句写了,下面一句默认单选效果才会有效
payment_style_listview.setItemChecked(0, true);
在上面的代码中要注意一点,只有添加 payment_style_listview.setSelected(true),那么payment_style_listview.setItemChecked(0, true);默认的单选才会有效果。