码迷,mamicode.com
首页 > 移动开发 > 详细

android listview 触摸改变颜色直至下一次触摸恢复(包含层叠颜色显示)

时间:2014-06-25 07:20:41      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:android   style   class   blog   code   java   

基本的思路是,在实体类中保存颜色的值或者是保存是否选中的状态(boolean),把实体的类的列表传入BaseAdapter然后调用listview实例的notifyDataSetChanged()方法进行动态更新数据。

包含两种方式(第二种是转的)

下面是一个实例:

bubuko.com,布布扣


ListViewItem:实体类

package cn.com.demotest.entity;

public class ListViewItem {
	private String name;
	private String result;
	private int colorData = 0;
	public ListViewItem(String name, String result, int colorData)
	{
		this.name = name;
		this.result = result;
		this.colorData = colorData;
	}
	public int getColorData() {
		return colorData;
	}
	public void setColorData(int colorData) {
		this.colorData = colorData;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
}

ListViewAdapter:BaseAdapter

package com.example.testdemo;

import java.util.List;

import cn.com.demotest.entity.ListViewItem;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {
	private Context context;
	private List<ListViewItem> data;
	private LayoutInflater inflater;
	private int colorPageId = 0;
	public ListViewAdapter(Context context,List<ListViewItem> data)
	{
		this.context = context;
		this.data = data;
		inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item,null);
		TextView name = (TextView) layout.findViewById(R.id.name);
		TextView result = (TextView) layout.findViewById(R.id.result);
		ListViewItem item = data.get(position);
		name.setText(item.getName());
		result.setText(item.getResult());
		int nameColor = 0;
		int resultColor = 0;
		if(colorPageId == 0)
		{
			nameColor = Color.BLUE;
			colorPageId = 1;
		}
		else if(colorPageId == 1)
		{
			nameColor = Color.GREEN;
			colorPageId = 0;
		}
		name.setTextColor(nameColor);//这里的颜色可以自己动态的设置,可以用Color对象里的颜色值,也可以用R.color里的颜色。
		result.setTextColor(item.getColorData());
		
		return layout;
	}
    public List<ListViewItem> getData() {
        return data;
    }
    public void setData(List<ListViewItem> data) {
        this.data = data;
    }

}

MainActivity:

package com.example.testdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.com.demotest.entity.ListViewItem;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

	private  ListView  listview;
	List<ListViewItem> data;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
      ArrayList<HashMap<String, Object>>  map=new ArrayList<HashMap<String,Object>>();
        
        listview=(ListView) findViewById(R.id.listview);
        data = new ArrayList<ListViewItem>();
        data.add(new ListViewItem(this.getResources().getString(R.string.name),
        		this.getResources().getString(R.string.result),Color.GREEN));
        data.add(new ListViewItem(this.getResources().getString(R.string.name),
        		this.getResources().getString(R.string.result),Color.BLUE));
        
        data.add(new ListViewItem(this.getResources().getString(R.string.name),
        		this.getResources().getString(R.string.result),Color.YELLOW));
        
        data.add(new ListViewItem(this.getResources().getString(R.string.name),
        		this.getResources().getString(R.string.result),Color.RED));
        
        data.add(new ListViewItem(this.getResources().getString(R.string.name),
        		this.getResources().getString(R.string.result),Color.CYAN));
        listview.setAdapter(new ListViewAdapter(MainActivity.this, data));
        
        Button button = (Button) findViewById(R.id.test);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                ListViewAdapter adapter = (ListViewAdapter) listview.getAdapter();
                ListViewItem listViewItem = data.get(0);
                listViewItem.setColorData(Color.RED);
                data.set(0,listViewItem);
                adapter.setData(data);
                adapter.notifyDataSetChanged();
               
            }
        });
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textView"
        android:textSize="20dp"
        />
  <ListView 
      android:id="@+id/listview"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      
  </ListView>
    
    <Button 
        android:id="@+id/test"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="hoadshfo"/>
    
    
</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:textSize="10dp" />

    <TextView
        android:id="@+id/result"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="7" />

</LinearLayout>


下面一种方法更加简便,我是转的:

最近项目ListView浏览时候用改变颜色来记录选中行,网上Baidu,Google了好久,最后结合网上资料和自己的实践,

终于成功实现了功能!效果图如下:

bubuko.com,布布扣

具体的代码如下:

1、ListView的代码:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:orientation="vertical"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent">  
  6. <include layout="@layout/title_left_button"/>  
  7. <RelativeLayout   
  8.     android:orientation="vertical" android:layout_width="fill_parent"  
  9.     android:layout_height="fill_parent">  
  10.     <include android:id="@+id/cusExp_Item_progress" layout="@layout/progress"/>     
  11.     <com.jemsn.util.ScrollListView_ExpView  
  12.         android:id="@+id/CusExp_items_Detail"  
  13.         android:layout_width="fill_parent"        
  14.         android:layout_height="fill_parent"  
  15.         android:listSelector="@android:color/transparent"     
  16.              android:cacheColorHint="#00000000"           
  17.             <span style="color:#cc0000;"> android:focusableInTouchMode="true"   
  18. </span>     />   
  19.       
  20. </RelativeLayout>    
  21. </LinearLayout>  

其中加红色的一定要加上去,否则得不到我们想要的效果!

2、DataAdpter 的代码如下:

[java] view plaincopy
  1. class ScrollListViewAdapter extends BaseAdapter {  
  2.         List<CustomerExpItem> newsItems;  
  3.         public ScrollListViewAdapter(List<CustomerExpItem> newsitems) {  
  4.             this.newsItems = newsitems;  
  5.         }  
  6.         @Override  
  7.         public int getCount() {  
  8.             return newsItems.size();  
  9.         }  
  10.         @Override  
  11.         public Object getItem(int position) {  
  12.             return newsItems.get(position);  
  13.         }  
  14.         @Override  
  15.         public long getItemId(int position) {  
  16.             return position;  
  17.         }  
  18.         public View getView(int position, View convertView, ViewGroup parent) {  
  19.             LinearLayout view = new LinearLayout(getApplicationContext());  
  20.             view.setOrientation(LinearLayout.HORIZONTAL);  
  21.             ViewHolder viewHolder=null;  
  22.             if (convertView == null) {  
  23.                 viewHolder = new ViewHolder();    
  24.                 // 固定的列  
  25.                 View viewFix = getLayoutInflater().inflate(  
  26.                         R.layout.expdetail_list_fix_items, parent, false);  
  27.                 // 可以滑动的列(Layout的根布局必须是LinearLayout,原因不明)  
  28.                 View viewMovable = getLayoutInflater().inflate(  
  29.                         R.layout.expdetail_list_movable_items, parent, false);  
  30.                 view.addView(viewFix);  
  31.                 view.addView(viewMovable);  
  32.                 viewHolder.fixTxt01 = (TextView) view  
  33.                         .findViewById(R.id.Exp_detail_SheetCode);  
  34.                 viewHolder.fixTxt02 = (TextView) view  
  35.                         .findViewById(R.id.Exp_detail_SheetDate);  
  36.                 view.setTag(viewHolder);  
  37.             } else {  
  38.                 view = (LinearLayout) convertView;  
  39.                 viewHolder = (ViewHolder) view.getTag();   
  40.             }  
  41.             viewHolder.fixTxt01.setText(newsItems.get(position).get_GdsName());  
  42.             viewHolder.fixTxt02.setText(newsItems.get(position).get_GdsCode());  
  43.                         mArrayListMovable.add(view.getChildAt(1));  
  44.             if (position == selectItem) {  
  45.                 viewHolder.fixTxt01.setTextColor(Color.GREEN);  
  46.                 viewHolder.fixTxt02.setTextColor(Color.GREEN);  
  47.                                   
  48.             } else {  
  49.                 viewHolder.fixTxt01.setTextColor(Color.WHITE);  
  50.                 viewHolder.fixTxt02.setTextColor(Color.parseColor("#40e0d0"));  
  51.                             }  
  52.             return view;  
  53.         }  
  54.   
  55.         public void addNewsItem(CustomerExpItem newsitem) {  
  56.             newsItems.add(newsitem);  
  57.         }  
  58.            <span style="color:#ff6666;"public  void setSelectItem(int selectItem) {    
  59.                    this.selectItem = selectItem;    
  60.                 }    
  61.                  private int  selectItem=-1;   
  62. </span> }     
  63.   
  64. }  

其中红色的为关键的代码。

3、ListView行点击,选中的代码:

[java] view plaincopy
  1. AdapterView.OnItemClickListener mLeftListOnItemClick = new AdapterView.OnItemClickListener() {    
  2.         public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {      
  3.             adapter.setSelectItem(arg2);    
  4.             adapter.notifyDataSetInvalidated();     };  

好了我们只要再注册一下ListView的点击事件就可以了!我们还可以添加自己比如更换图片之类!

参考自:http://blog.csdn.net/zz_mm/article/details/7513391

android listview 触摸改变颜色直至下一次触摸恢复(包含层叠颜色显示),布布扣,bubuko.com

android listview 触摸改变颜色直至下一次触摸恢复(包含层叠颜色显示)

标签:android   style   class   blog   code   java   

原文地址:http://blog.csdn.net/coslay/article/details/34165875

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!