![](https://img.haomeiwen.com/i5949429/9aa67fbc784c0d6d.png)
image.png
public class SelectTagAdapter extends BaseAdapter {
private Context context;
private String[] data;
private int selectPosition;
private boolean selected = false;
public void setSelectPosition(int selectPosition) {
selected = true;
this.selectPosition = selectPosition;
}
public SelectTagAdapter(Context context, String[] data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data==null?0:data.length;
}
@Override
public Object getItem(int position) {
return data[position];
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHoler{
private TextView tv;
private LinearLayout ll;
}
public int getSelectPosition() {
return selectPosition;
}
public boolean isSelected() {
return selected;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHoler holer;
if (convertView == null){
holer = new ViewHoler();
convertView = View.inflate(context, R.layout.item_select_tag_grv,null);
holer.tv = ((TextView) convertView.findViewById(R.id.item_select_tag_tv));
holer.ll = ((LinearLayout) convertView.findViewById(R.id.item_select_tag_ll));
convertView.setTag(holer);
}else{
holer = (ViewHoler) convertView.getTag();
}
holer.tv.setText(data[position]);
if (selected && selectPosition == position){
holer.ll.setSelected(true);
holer.tv.setTextColor(Color.parseColor("#ffffff"));
}else{
holer.ll.setSelected(false);
holer.tv.setTextColor(Color.parseColor("#999999"));
}
return convertView;
}
}
xml:
<?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:layout_height="match_parent">
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/item_select_tag_ll"
android:gravity="center"
android:background="@drawable/select_tag_bg"
android:layout_width="@dimen/dimen_78dp"
android:layout_height="@dimen/dimen_28dp">
<TextView
android:id="@+id/item_select_tag_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已接听"
android:textColor="#999999"
android:textSize="@dimen/text14" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
select_tag_bg:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/bg_ff5722_radius_15dp"/>
<item android:state_selected="false" android:drawable="@drawable/bg_white_radius_15dp"/>
</selector>
网友评论