首页 > 代码库 > android listview 触摸改变颜色直至下一次触摸恢复(包含层叠颜色显示)
android listview 触摸改变颜色直至下一次触摸恢复(包含层叠颜色显示)
基本的思路是,在实体类中保存颜色的值或者是保存是否选中的状态(boolean),把实体的类的列表传入BaseAdapter然后调用listview实例的notifyDataSetChanged()方法进行动态更新数据。
包含两种方式(第二种是转的)
下面是一个实例:
ListViewItem:实体类
package cn.com.demotest.entity; public class ListViewItem { private String name; private String result; private int colorData = http://www.mamicode.com/0;>
ListViewAdapter:BaseAdapterpackage 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 = http://www.mamicode.com/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 = http://www.mamicode.com/new ArrayList();>
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了好久,最后结合网上资料和自己的实践,
终于成功实现了功能!效果图如下:
具体的代码如下:
1、ListView的代码:
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <include layout="@layout/title_left_button"/>
- <RelativeLayout
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <include android:id="@+id/cusExp_Item_progress" layout="@layout/progress"/>
- <com.jemsn.util.ScrollListView_ExpView
- android:id="@+id/CusExp_items_Detail"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:listSelector="@android:color/transparent"
- android:cacheColorHint="#00000000"
- <span style="color:#cc0000;"> android:focusableInTouchMode="true"
- </span> />
- </RelativeLayout>
- </LinearLayout>
其中加红色的一定要加上去,否则得不到我们想要的效果!
2、DataAdpter 的代码如下:
[java] view plaincopy
- class ScrollListViewAdapter extends BaseAdapter {
- List<CustomerExpItem> newsItems;
- public ScrollListViewAdapter(List<CustomerExpItem> newsitems) {
- this.newsItems = newsitems;
- }
- @Override
- public int getCount() {
- return newsItems.size();
- }
- @Override
- public Object getItem(int position) {
- return newsItems.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- LinearLayout view = new LinearLayout(getApplicationContext());
- view.setOrientation(LinearLayout.HORIZONTAL);
- ViewHolder viewHolder=null;
- if (convertView == null) {
- viewHolder = new ViewHolder();
- // 固定的列
- View viewFix = getLayoutInflater().inflate(
- R.layout.expdetail_list_fix_items, parent, false);
- // 可以滑动的列(Layout的根布局必须是LinearLayout,原因不明)
- View viewMovable = getLayoutInflater().inflate(
- R.layout.expdetail_list_movable_items, parent, false);
- view.addView(viewFix);
- view.addView(viewMovable);
- viewHolder.fixTxt01 = (TextView) view
- .findViewById(R.id.Exp_detail_SheetCode);
- viewHolder.fixTxt02 = (TextView) view
- .findViewById(R.id.Exp_detail_SheetDate);
- view.setTag(viewHolder);
- } else {
- view = (LinearLayout) convertView;
- viewHolder = (ViewHolder) view.getTag();
- }
- viewHolder.fixTxt01.setText(newsItems.get(position).get_GdsName());
- viewHolder.fixTxt02.setText(newsItems.get(position).get_GdsCode());
- mArrayListMovable.add(view.getChildAt(1));
- if (position == selectItem) {
- viewHolder.fixTxt01.setTextColor(Color.GREEN);
- viewHolder.fixTxt02.setTextColor(Color.GREEN);
- } else {
- viewHolder.fixTxt01.setTextColor(Color.WHITE);
- viewHolder.fixTxt02.setTextColor(Color.parseColor("#40e0d0"));
- }
- return view;
- }
- public void addNewsItem(CustomerExpItem newsitem) {
- newsItems.add(newsitem);
- }
- <span style="color:#ff6666;"> public void setSelectItem(int selectItem) {
- this.selectItem = selectItem;
- }
- private int selectItem=-1;
- </span> }
- }
其中红色的为关键的代码。
3、ListView行点击,选中的代码:
[java] view plaincopy
- AdapterView.OnItemClickListener mLeftListOnItemClick = new AdapterView.OnItemClickListener() {
- public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {
- adapter.setSelectItem(arg2);
- adapter.notifyDataSetInvalidated(); };
好了我们只要再注册一下ListView的点击事件就可以了!我们还可以添加自己比如更换图片之类!
参考自:http://blog.csdn.net/zz_mm/article/details/7513391
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。