最近做了這個需求選擇顏色在這里記錄一下下面是布局里面基本一個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="確認(rèn)"
? ? ? ? android:textColor="@color/white"
? ? ? ? android:textSize="17sp" />
創(chuàng)建數(shù)據(jù)
datas =new ArrayList<>();
datas.add("工作");
datas.add("學(xué)習(xí)");
datas.add("運動");
datas.add("重要日");
設(shè)置適配器
myRichengyanseAdapter =new MyRichengyanseAdapter(context,datas);
ivYanse.setAdapter(myRichengyanseAdapter);
設(shè)置item點擊事件
ivYanse.setOnItemClickListener(this);
這是adapter中的內(nèi)容
class MyRichengyanseAdapterextends BaseAdapter {
private Contextcontext;
private ArrayListdata;
private int tempPosition = -1;//記錄已經(jīng)點擊的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);//設(shè)置當(dāng)前position為CheckBox的id
? ? ? ? holder.chebox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
? ? ? ? ? ? public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked) {
if (tempPosition != -1) {
//根據(jù)id找到上次點擊的CheckBox,將它設(shè)置為false.
? ? ? ? ? ? ? ? ? ? ? ? CheckBox tempCheckBox = (CheckBox) findViewById(tempPosition);
if (tempCheckBox !=null) {
tempCheckBox.setChecked(false);
}
}
//保存當(dāng)前選中CheckBox的id值
? ? ? ? ? ? ? ? ? ? tempPosition = buttonView.getId();
}else {//當(dāng)CheckBox被選中,又被取消時,將tempPosition重新初始化.
? ? ? ? ? ? ? ? ? ? tempPosition = -1;
}
}
});
if (position ==tempPosition)//比較位置是否一樣,一樣就設(shè)置為選中,否則就設(shè)置為未選中.
? ? ? ? {
holder.chebox.setChecked(true);
}else {
holder.chebox.setChecked(false);
}
return convertView;
}
//返回當(dāng)前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" />
//實現(xiàn)聯(lián)動效果
@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:
//確認(rèn)
? ? ? ? ? ? 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返回時傳遞數(shù)據(jù)延曙,也是通過意圖對象
? ? ? ? ? ? Intent data =new Intent();
//把要傳遞的數(shù)據(jù)封裝至意圖對象中
? ? ? ? ? ? data.putExtra("richengyanse", s);
//當(dāng)前Activity銷毀時遭笋,data這個意圖就會傳遞給啟動當(dāng)前Activity的那個Activity
? ? ? ? ? ? setResult(2000, data);
finish();
break;
default:
}
}