首页 > 代码库 > listview 选择后高亮显示

listview 选择后高亮显示

public class UserTypeParentAdapter extends MyBaseAdapter<UserTypeList> {    private int selectItem = -1;    public UserTypeParentAdapter(Context context, List<UserTypeList> datas) {        super(context, datas);    }    public void setSelectItem(int selectItem) {        this.selectItem = selectItem;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        if (convertView == null) {            convertView = mInflater.inflate(R.layout.usertype_superclass_item,null);        }        TextView txtTitle = ViewHolder.get(convertView, R.id.txt_title);        txtTitle.setText(mDatas.get(position).getTypename());        if (position == selectItem) {            txtTitle.setSelected(true);            txtTitle.setEnabled(true);        } else {            txtTitle.setSelected(false);            txtTitle.setEnabled(false);        }        return convertView;    }}
View Code
    lv_superclass.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view,                    int position, long id) {                adapterParent.setSelectItem(position);                  adapterParent.notifyDataSetInvalidated();              }        });
View Code

adapter item布局

<?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="50.0dip"    android:orientation="vertical" >    <TextView         android:id="@id/txt_title"        android:layout_width="match_parent"        android:layout_height="50.0dip"        android:background="@drawable/usertype_super_select"        android:drawableRight="@drawable/arrows_identity_select"        android:gravity="center"        android:textColor="@color/login_font"        android:textSize="16.0sp" /></LinearLayout>
View Code

选择器

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_enabled="true" android:state_focused="true" android:drawable="@color/white"></item>    <item android:state_enabled="true" android:state_selected="true" android:drawable="@color/white"></item>    <item android:state_pressed="true" android:drawable="@color/white"></item>    <item android:drawable="@color/layout_bg"></item></selector>
View Code

 

listview 选择后高亮显示