Android中设置ListView的选中的Item的背景颜色【复杂模式】

        前面的一篇文章通过listview的itemclickListener实现了点击listview中的一个item来改变其背景色。。但是大多时候我们需要在getView中做大量的任务。。比如说getView中的item可能要处理一些布局的显隐、radioGroup选择等等。。再者就是前面提到的改变背景色  。。。具体代码:

看看效果图先:

    

     1:item布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/layout_match_parent"
    android:orientation="vertical"
     >

    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="60dp"
        >
     <LinearLayout
        android:id="@+id/before_show"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:gravity="center|left"
        android:paddingLeft="2dp"
        android:background="@drawable/xc_list_item_" >

        <TextView
            style="@style/layout_wrap_content"
            android:text="@string/str_list_lable_xh"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/before_xh_tv"
            style="@style/layout_wrap_content"
            android:layout_marginRight="30dp"
            android:text="1.1"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/before_ms_tv"
            style="@style/layout_wrap_content"
            android:text="向客户了解操作员的抱怨"
            android:textSize="17sp" />
    </LinearLayout>
    <ImageView 
        style="@style/layout_wrap_content"
        android:src="@drawable/xc_list_item_pop"
        android:id="@+id/before_item_pop"
        android:visibility="gone"
        />
    </FrameLayout>
    <LinearLayout
        android:id="@+id/before_hide"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/xc_list_body"
        android:paddingLeft="2dp"
        android:visibility="gone" >

        <RelativeLayout
            style="@style/layout_mh_wv"
            android:layout_marginTop="8dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/befor_zt"
                style="@style/layout_wrap_content"
                android:layout_alignParentLeft="true"
                android:text="@string/str_list_lable_zt"
                android:textSize="17sp" />

            <RadioGroup
                android:focusable="false"
                android:clickable="true"
                android:id="@+id/before_rg"
                style="@style/layout_wrap_content"
                android:layout_alignBottom="@id/befor_zt"
                android:layout_toRightOf="@id/befor_zt"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/before_zc"
                    style="@style/layout_wrap_content"
                    android:text="@string/str_list_lable_zt1"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/before_jc"
                    style="@style/layout_wrap_content"
                    android:text="@string/str_list_lable_zt2"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/before_wh"
                    style="@style/layout_wrap_content"
                    android:text="@string/str_list_lable_zt3"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/before_wcq"
                    style="@style/layout_wrap_content"
                    android:text="@string/str_list_lable_zt4"
                    android:textSize="12sp" />
            </RadioGroup>
        </RelativeLayout>

        <LinearLayout
            style="@style/layout_mh_wv"
            android:layout_marginTop="8dp"
            android:orientation="horizontal" >

            <TextView
                style="@style/layout_wrap_content"
                android:text="@string/str_list_lable_bz"
                android:textSize="17sp" />

            <EditText
                android:id="@+id/before_edit"
                style="@style/layout_mh_wv"
                android:textColor="@android:color/white"
                android:gravity="top"
                android:minLines="3"
                 />
        </LinearLayout>

        <LinearLayout
            style="@style/layout_mh_wv"
            android:layout_marginTop="8dp" >

            <TextView
                style="@style/layout_wrap_content"
                android:text="@string/str_list_lable_zp"
                android:textSize="17sp" />

            <ImageView
                  android:focusable="false"
                android:clickable="true"
                style="@style/layout_weight_horizontal_weitht"
                android:layout_weight="1.0"
                android:scaleType="center"
                android:id="@+id/before_img1"
                android:src="@drawable/xc_list_pic" />

            <ImageView
                  android:focusable="false"
                android:clickable="true"
               style="@style/layout_weight_horizontal_weitht"
                android:layout_weight="1.0"
                android:scaleType="center"
                 android:id="@+id/before_img2"
                android:src="@drawable/xc_list_pic" />

            <ImageView
                  android:focusable="false"
                android:clickable="true"
                style="@style/layout_weight_horizontal_weitht"
                android:layout_weight="1.0"
                android:scaleType="center"
                android:id="@+id/before_img3"
                android:src="@drawable/xc_list_pic" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
2:自定义adapter实现

  

public class CopyOfBeforeAdapter extends BaseAdapter {

	CheckPreparedActivity context;   //activity
	ArrayList<YYWJJ_entity> yyBeforeLists;   //item对应的entity 
	LayoutInflater inflater;
	Resources resources;
	SharedPreferences sp;
	Sp2Json sp2Json;
	HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

	private String imgDir;
	File myFileDir;// 创建文件夹

	/**
	 * @param context
	 * 
	 */
	public CopyOfBeforeAdapter(Context context) {
		this.context = (CheckPreparedActivity) context;
		inflater = LayoutInflater.from(context);
		resources = context.getResources();
		imgDir = Environment.getExternalStorageDirectory() + "/XCTASK1-6";
		yyBeforeLists = new ArrayList<YYWJJ_entity>();
		sp = context.getSharedPreferences(XcApplication.getApplication()
				.getSpName(), context.MODE_PRIVATE);
		sp2Json = new Sp2Json(context);
		setData();
	}

       // 获取listview所需要的entity放入到一个arraylist中
	public void setData() {
		try {
			ArrayList<YYWJJ_entity> datas = DataManager.getDataManager()
					.getYywjjLists();
			for (int j = 0; j < datas.size(); j++) {
				if (sp.getString(datas.get(j).getjXh() + "xh", "").equals(
						datas.get(j).getjXh())) {
					datas.get(j).setjBz(
							sp.getString(datas.get(j).getjXh() + "bz", ""));
					datas.get(j).setjZt(
							sp.getString(datas.get(j).getjXh() + "zt", ""));
				}
			}

			yyBeforeLists.addAll(datas);
		} catch (Exception e) {
		}

	}

	@Override
	public int getCount() {

		if (yyBeforeLists == null) {
			return 0;
		}
		Log.i("msg",  "getCount");
		return yyBeforeLists.size();
	}

	@Override
	public Object getItem(int position) {
		if (yyBeforeLists == null) {
			return null;
		}
		Log.i("msg", position+ "getItem");
		return yyBeforeLists.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		TextView xhTextView = null;
		TextView msTextView = null;
		RadioGroup ztGroup = null;
		EditText bzEditText = null;
		LinearLayout showLayout = null;
		LinearLayout hideLayout = null;
		ImageView image1 = null;
		ImageView image2 = null;
		ImageView image3 = null;
		RadioButton radioButton1 = null;
		RadioButton radioButton2 = null;
		RadioButton radioButton3 = null;
		RadioButton radioButton4 = null;
		
		ImageView popImageView = null;

		convertView = inflater.inflate(R.layout.xc_tb_check_item, null);
		xhTextView = (TextView) convertView.findViewById(R.id.before_xh_tv);
		msTextView = (TextView) convertView.findViewById(R.id.before_ms_tv);
		ztGroup = (RadioGroup) convertView.findViewById(R.id.before_rg);
		bzEditText = (EditText) convertView.findViewById(R.id.before_edit);
		showLayout = (LinearLayout) convertView.findViewById(R.id.before_show);
		hideLayout = (LinearLayout) convertView.findViewById(R.id.before_hide);
		image1 = (ImageView) convertView.findViewById(R.id.before_img1);
		image2 = (ImageView) convertView.findViewById(R.id.before_img2);
		image3 = (ImageView) convertView.findViewById(R.id.before_img3);
		radioButton1 = (RadioButton) convertView.findViewById(R.id.before_zc);
		radioButton2 = (RadioButton) convertView.findViewById(R.id.before_jc);
		radioButton3 = (RadioButton) convertView.findViewById(R.id.before_wh);
		radioButton4 = (RadioButton) convertView.findViewById(R.id.before_wcq);

		popImageView = (ImageView) convertView.findViewById(R.id.before_item_pop);
                //entity有属性isExpand用来标示显隐
		hideLayout
				.setVisibility(yyBeforeLists.get(position).isExpand() ? 0 : 8);
		final LinearLayout finalLayout = showLayout;
		 //点击来更新对应position上entity的isExpand值,然后通过notifyDataSetChanged来刷新listview
		finalLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.i("tag", "click");
				yyBeforeLists.get(position).setExpand(
						!yyBeforeLists.get(position).isExpand());
				notifyDataSetChanged();
			}
		});

		
		if(yyBeforeLists.get(position).isExpand()){
			finalLayout.setBackgroundResource(R.drawable.xc_list_item_s);
		}else {
			finalLayout.setBackgroundResource(R.drawable.xc_list_item_);
		}
		//通过对应position上enttity的状态属性来显隐左上角的勾
		if(yyBeforeLists.get(position).getjZt().equals("") || yyBeforeLists.get(position).getjZt().equals("0")){
			popImageView.setVisibility(View.GONE);
			
		}else {
			popImageView.setVisibility(View.VISIBLE);
		}
		
		Log.i("msg", "zt  "+position+":"+yyBeforeLists.get(position).getjZt());
		
		xhTextView.setText(yyBeforeLists.get(position).getjXh());
		String type = yyBeforeLists.get(position).getjType();
		if (type.equals("0")) {
			msTextView.setText(yyBeforeLists.get(position).getjMs());
		} else {
			msTextView.setText(resources
					.getText(R.string.check_before_str_default));
		}

		if (yyBeforeLists.get(position).getjBz() != null) {
			bzEditText.setText(yyBeforeLists.get(position).getjBz());
		}

		sp2Json.saveData((yyBeforeLists.get(position).getjXh()) + "xh",
				yyBeforeLists.get(position).getjXh());

		ztGroup.setId(position);
		ztGroup.setOnCheckedChangeListener(null);
		if (yyBeforeLists.get(position).getjZt().equals("1")) {
			ztGroup.check(R.id.before_zc);
		} else if (yyBeforeLists.get(position).getjZt().equals("2")) {
			ztGroup.check(R.id.before_jc);
		} else if (yyBeforeLists.get(position).getjZt().equals("3")) {
			ztGroup.check(R.id.before_wh);
		} else if (yyBeforeLists.get(position).getjZt().equals("4")) {
			ztGroup.check(R.id.before_wcq);
		} else {
			ztGroup.clearCheck();
		}
		Log.i("msg", position+ "getView");
		ztGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {

				try {
					switch (checkedId) {
					case R.id.before_zc:
						sp2Json.saveData(yyBeforeLists.get(position).getjXh()
								+ "zt", "1");
					      //更新对应position上enttity的状态属性来显隐左上角的勾
						yyBeforeLists.get(position).setjZt("1");
						break;
					case R.id.before_jc:
						sp2Json.saveData(yyBeforeLists.get(position).getjXh()
								+ "zt", "2");
						yyBeforeLists.get(position).setjZt("2");
						break;
					case R.id.before_wh:
						sp2Json.saveData(yyBeforeLists.get(position).getjXh()
								+ "zt", "3");
						yyBeforeLists.get(position).setjZt("3");
						break;
					case R.id.before_wcq:
						sp2Json.saveData(yyBeforeLists.get(position).getjXh()
								+ "zt", "4");
						yyBeforeLists.get(position).setjZt("4");
						break;
					default:
						break;
					}
				} catch (Exception e) {
				}
			}
		});

		bzEditText.requestFocus();
		bzEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {

				try {
					String txt = s.toString();
					sp2Json.saveData(yyBeforeLists.get(position).getjXh()
							+ "bz", txt);
					hashMap.put(position, s.toString());
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
		});
		if (hashMap.get(position) != null) {
			bzEditText.setText(hashMap.get(position));
		}

		image1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkStrogeState()) {

					if (getPositionDrawable(1, position, context) != null) {
						final ImageDialog dialog = new ImageDialog(context);
						dialog.setImageView(getPositionDrawable(1, position,
								context));
						dialog.setDialogLayoutParamtrs();
						if (sp.getString("isComplete", "0").equals("1")) {
							dialog.getRightButton().setEnabled(false);
						}
						dialog.setImageListener(new ImageListener() {
							@Override
							public void onButtonClick(int id) {
								switch (id) {
								case ImageDialog.POSITIVE: {
									context.setPicInfo(position
											+ "-"
											+ yyBeforeLists.get(position)
													.getjXh() + "-" + 1);
									startForPicture();
									dialog.dismiss();
								}
									break;
								case ImageDialog.NEGATIVE:
									dialog.dismiss();
									break;
								default:
									break;
								}
							}
						});
						dialog.show();
					} else {
						context.setPicInfo(position + "-"
								+ yyBeforeLists.get(position).getjXh() + "-"
								+ 1);
						startForPicture();
					}
				} else {
					Toast.makeText(context, "请先插入SD卡", Toast.LENGTH_SHORT)
							.show();
				}

			}
		});
		image2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkStrogeState()) {
					if (getPositionDrawable(2, position, context) != null) {
						final ImageDialog dialog = new ImageDialog(context);
						dialog.setImageView(getPositionDrawable(2, position,
								context));
						dialog.setDialogLayoutParamtrs();
						if (sp.getString("isComplete", "0").equals("0")) {
							dialog.getRightButton().setEnabled(false);
						}
						dialog.setImageListener(new ImageListener() {
							@Override
							public void onButtonClick(int id) {
								switch (id) {
								case ImageDialog.POSITIVE: {
									context.setPicInfo(position
											+ "-"
											+ yyBeforeLists.get(position)
													.getjXh() + "-" + 2);
									startForPicture();
								}
									break;
								case ImageDialog.NEGATIVE:
									dialog.dismiss();
									break;
								default:
									break;
								}
							}
						});
						dialog.show();
					} else {
						context.setPicInfo(position + "-"
								+ yyBeforeLists.get(position).getjXh() + "-"
								+ 2);
						startForPicture();
					}

				} else {
					Toast.makeText(context, "请先插入SD卡", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});
		image3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkStrogeState()) {
					if (getPositionDrawable(3, position, context) != null) {
						final ImageDialog dialog = new ImageDialog(context);
						dialog.setImageView(getPositionDrawable(3, position,
								context));
						dialog.setDialogLayoutParamtrs();
						if (sp.getString("isComplete", "0").equals("0")) {
							dialog.getRightButton().setEnabled(false);
						}
						dialog.setImageListener(new ImageListener() {
							@Override
							public void onButtonClick(int id) {
								switch (id) {
								case ImageDialog.POSITIVE: {
									context.setPicInfo(position
											+ "-"
											+ yyBeforeLists.get(position)
													.getjXh() + "-" + 3);
									startForPicture();
								}
									break;
								case ImageDialog.NEGATIVE:
									dialog.dismiss();
									break;
								default:
									break;
								}
							}
						});
						dialog.show();
					} else {
						context.setPicInfo(position + "-"
								+ yyBeforeLists.get(position).getjXh() + "-"
								+ 3);
						startForPicture();
					}
				} else {
					Toast.makeText(context, "请先插入SD卡", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});

		if (!imgDir.equals("")) {
			File imgFile = new File(imgDir);
			if (imgFile.exists()) {
				for (File file : imgFile.listFiles()) {
					String imgName = file.getName();
					if (imgName.contains(yyBeforeLists.get(position).getjXh()
							+ "#" + 1)) {
						Bitmap mapBitmap = BitmapFactory.decodeFile(file
								.getPath());
						BitmapDrawable drawable = new BitmapDrawable(mapBitmap);
						image1.setImageDrawable(drawable);
					} else if (imgName.contains(yyBeforeLists.get(position)
							.getjXh() + "#" + 2)) {
						Bitmap mapBitmap = BitmapFactory.decodeFile(file
								.getPath());
						BitmapDrawable drawable = new BitmapDrawable(mapBitmap);
						image2.setImageDrawable(drawable);

					} else if (imgName.contains(yyBeforeLists.get(position)
							.getjXh() + "#" + 3)) {
						Bitmap mapBitmap = BitmapFactory.decodeFile(file
								.getPath());
						BitmapDrawable drawable = new BitmapDrawable(mapBitmap);
						image3.setImageDrawable(drawable);

					}
				}
			}
		}

		return convertView;
	}

	private BitmapDrawable getPositionDrawable(int tag, int position,
			Context context) {
		BitmapDrawable drawable = null;
		if (!imgDir.equals("")) {
			File imgFile = new File(imgDir);
			if (imgFile.exists()) {
				for (File file : imgFile.listFiles()) {
					String imgName = file.getName();
					if (imgName.contains(yyBeforeLists.get(position).getjXh()
							+ "#" + tag)) {
						Bitmap mapBitmap = BitmapFactory.decodeFile(file
								.getPath());
						drawable = new BitmapDrawable(context.getResources(),
								mapBitmap);
					}
				}
			}
		}
		return drawable;

	}

	class ViewHolder {
		private TextView xhTextView;
		private TextView msTextView;
		private RadioGroup ztGroup;
		private EditText bzEditText;
		private LinearLayout showLayout;
		private LinearLayout hideLayout;
		private ImageView image1;
		private ImageView image2;
		private ImageView image3;
		RadioButton radioButton1;
		RadioButton radioButton2;
		RadioButton radioButton3;
		RadioButton radioButton4;

	}

	/**
	 * sd卡的bound状态
	 * 
	 * @return
	 */
	private boolean checkStrogeState() {
		boolean state = true;
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			state = true;
		} else {
			state = false;
		}
		return state;
	}

	/**
	 * 拍照
	 * 
	 * @param serNum
	 */
	private void startForPicture() {
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		context.startActivityForResult(intent, 1);
	}

}
效果图:

说明:当点击整个item的时候隐藏的布局会显隐【显示则隐藏、隐藏则显示】 ,当checkbox有状态被选择的时候,item左上角的勾会显示

   最后加个entity的构造。。

YYWJJ_entity entiry1 = new YYWJJ_entity();
		entiry1.setCmdid(cmdid);
		entiry1.setjType("0");
		entiry1.setjXh("1.1");
		entiry1.setjZt("0");
		entiry1.setExpand(false);

ok。。。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值