最近做了这个需求选择颜色在这里记录一下下面是布局里面基本一个listview
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE"
android:orientation="vertical"
tools:context="com.atgerunkeji.example.carschool.controller.activity.RichengyanseActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="17dp"
android:singleLine="true"
android:text="选择颜色"
android:textColor="#ff999999"
android:textSize="13sp" />
android:id="@+id/iv_yanse"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="13dp"
android:background="@color/white">
android:id="@+id/btn_queren"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="32dp"
android:background="@drawable/iv_shapeblue"
android:text="确认"
android:textColor="@color/white"
android:textSize="17sp" />
创建数据
datas =new ArrayList<>();
datas.add("工作");
datas.add("学习");
datas.add("运动");
datas.add("重要日");
设置适配器
myRichengyanseAdapter =new MyRichengyanseAdapter(context,datas);
ivYanse.setAdapter(myRichengyanseAdapter);
设置item点击事件
ivYanse.setOnItemClickListener(this);
这是adapter中的内容
class MyRichengyanseAdapterextends BaseAdapter {
private Contextcontext;
private ArrayListdata;
private int tempPosition = -1;//记录已经点击的CheckBox的位置
private Integer[]images = {R.drawable.personal_icon_work, R.drawable.personal_icon_study, R.drawable.personal_icon_sport, R.drawable.personal_icon_import};
public MyRichengyanseAdapter(Context context, ArrayList data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder =null;
if (convertView ==null) {
convertView = View.inflate(context, R.layout.item_richengyanse,null);
holder =new ViewHolder();
holder.chebox = convertView.findViewById(R.id.chebox);
holder.iv_yanse = convertView.findViewById(R.id.iv_yanse);
holder.tv_gongzuo = convertView.findViewById(R.id.tv_gongzuo);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.iv_yanse.setImageResource(images[position]);
holder.tv_gongzuo.setText(data.get(position));
holder.chebox.setId(position);//设置当前position为CheckBox的id
holder.chebox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked) {
if (tempPosition != -1) {
//根据id找到上次点击的CheckBox,将它设置为false.
CheckBox tempCheckBox = (CheckBox) findViewById(tempPosition);
if (tempCheckBox !=null) {
tempCheckBox.setChecked(false);
}
}
//保存当前选中CheckBox的id值
tempPosition = buttonView.getId();
}else {//当CheckBox被选中,又被取消时,将tempPosition重新初始化.
tempPosition = -1;
}
}
});
if (position ==tempPosition)//比较位置是否一样,一样就设置为选中,否则就设置为未选中.
{
holder.chebox.setChecked(true);
}else {
holder.chebox.setChecked(false);
}
return convertView;
}
//返回当前CheckBox选中的位置,便于获取值.
public int getSelectPosition() {
return tempPosition;
}
class ViewHolder {
ImageViewiv_yanse;
TextViewtv_gongzuo;
CheckBoxchebox;
}
}
item布局
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="48dp">
android:id="@+id/iv_yanse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp" />
android:id="@+id/tv_gongzuo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="48dp"
android:text="工作"
android:textColor="#ff333333"
android:textSize="15sp" />
android:id="@+id/chebox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp" />
//实现联动效果
@Override
public void onItemClick(AdapterView parent, View view,int position,long id) {
MyRichengyanseAdapter.ViewHolder holder = (MyRichengyanseAdapter.ViewHolder) view.getTag();
holder.chebox.toggle();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tvleft:
finish();
break;
case R.id.btn_queren:
//确认
int selectPosition =myRichengyanseAdapter.getSelectPosition();
if(selectPosition==-1) {
ToastUtils.showToast(context,"请选择日程颜色");
return;
}
String s =datas.get(selectPosition);
if (TextUtils.isEmpty(s)) {
ToastUtils.showToast(context,"请选择日程颜色");
return;
}
//Activity返回时传递数据,也是通过意图对象
Intent data =new Intent();
//把要传递的数据封装至意图对象中
data.putExtra("richengyanse", s);
//当前Activity销毁时,data这个意图就会传递给启动当前Activity的那个Activity
setResult(2000, data);
finish();
break;
default:
}
}