1. 程式人生 > >根據資料動態改變Android列表背景顏色

根據資料動態改變Android列表背景顏色

       自己的專案中有一個需求:需要根據列表項的狀態來動態覺得背景的顏色,這個需求看似簡單,只要在adapter的getView中根據資料的狀態來設定背景色即可,但是我在這麼做的時候卻得不到想要的結果,這裡我設定了狀態為“未檢查”的不變色,“無問題”的設定為綠色,“有問題”的設定為黃色:

if(null==result||result.isEmpty()) {
			holder.state.setText("[未檢查]");
			holder.modifyBtn.setVisibility(View.GONE);
		}else if(goodcode.equals(result)){
			holder.state.setText("[無問題]");
			holder.modifyBtn.setVisibility(View.GONE);
			holder.checkItem.setBackgroundColor(Color.GREEN);
		}else {
			holder.state.setText("[有問題]");
			holder.modifyBtn.setVisibility(View.GONE);
			holder.checkItem.setBackgroundColor(Color.YELLOW);
		}
		return convertView;

顯示結果如下:

可見,這並不是我們希望的,而且隨著列表的上下拖動,顏色會無規律的變化,這與getView()方法的呼叫有關。

我們需要在程式碼中把color作為一個引數傳遞進去adapter,並在getView()中設定三種情況,一種是狀態為”無問題“的,一種是狀態為”有問題“的,還有一種是”未檢查的“:

@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = new ViewHolder();
				
		if(convertView==null) {
			holder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.realchecklist_item, null);	
			holder.checkItem = (RelativeLayout) convertView.findViewById(R.id.checkitem);
			holder.equipNo = (TextView) convertView.findViewById(R.id.equipNo);
			holder.state = (TextView) convertView.findViewById(R.id.state);
			holder.modifyBtn = (Button) convertView.findViewById(R.id.modify_btn);
			HashMap<String,Object> map = data.get(position);
			id = (String)map.get("id");
			System.out.println("id+++++++++++++++++++>"+id);
			holder.modifyBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(context,RealCheckProblemListActivity.class);
					intent.putExtra("id", id);
					context.startActivity(intent);
				}
				
			});
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder)convertView.getTag();
		}		
		System.out.println("position----------------->"+position);
		holder.equipNo.setText((String)data.get(position).get("equipNo"));
		Float allScore = (Float)data.get(position).get("allScore");		
		int score = allScore.intValue();
		String result = (String)data.get(position).get("result");
		Integer color = (Integer)data.get(position).get("color");
		System.out.println("color--------------------->"+color);
		<span style="background-color: rgb(255, 204, 153);">if(color==1) {
			holder.checkItem.setBackgroundColor(Color.GREEN);
		}else if(color==2) {
			holder.checkItem.setBackgroundColor(Color.YELLOW);
		}
		else {
			//這個else一定要加上,否則顏色顯示會混亂
			holder.checkItem.setBackgroundColor(Color.TRANSPARENT); // 背景設定為透明色,對於不知道背景色是什麼顏色的設定是很好的
		}</span>
		RealCheckDBManager manager = new RealCheckDBManager(context);
		String goodcode = manager.getResultCode("優");
		
		if(null==result||result.isEmpty()) {
			holder.state.setText("[未檢查]");
			holder.modifyBtn.setVisibility(View.GONE);
		}else if(goodcode.equals(result)){
			holder.state.setText("[無問題]");
			holder.modifyBtn.setVisibility(View.GONE);
			//holder.checkItem.setBackgroundColor(Color.GREEN);
		}else {
			holder.state.setText("[有問題]");
			holder.modifyBtn.setVisibility(View.GONE);
			//holder.checkItem.setBackgroundColor(Color.YELLOW);
		}
		return convertView;
	}
修改過之後,顯示就正常了:



還要注意一點,就是對於不需要改變背景色的地方,可以設定為透明,

holder.checkItem.setBackgroundColor(Color.TRANSPARENT);

這種情況對於背景是圖片的情況尤其有用,因為你無法準備知道原來的背景顏色是什麼。