因为项目中有这样的需求:一般消息类或者资讯类的界面,都有类似于网易客户端的点击之后更换背景色,并且保持这个颜色,表示已读取该条信息。这个功能一开始觉得已经实现了,在我的另一篇博客上有:点击打开链接 后来测试人员在测试的时候,发现这样一个奇怪的问题:
当点击了listview的第0项,背景颜色改变,并且保持着背景没错,但滑动到下一个页面的listview,原本没有点击的item,竟然莫名其妙的出现了更换的背景。。。
出现上述的情况是因为我实现的原理是这样的:当用户点击某一个item,会进入到监听方法里面,我设置一个变量保存所选中的位置position,也就是如下代码:
arg1.setSelected(true);
selectItem = arg2; // 当前选择的节目item
messageAd.notifyDataSetChanged(); // 通知adapter刷新数据
在适配器里面,我就使用if语句判断,如果position等于刚才所点击的item,则设置背景颜色,如下代码:
if (selectItem == position) {
//设置背景
// }
上述设置就会出现错乱的问题,原因以后再深究,下面贴出解决的办法,不多说直接上代码,有注释:
package com.explink.listview;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
public class MainActivity extends Activity {
private ListView mListView;
private MyAdapter mAdapter;
List<User> userList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
mListView = (ListView) findViewById(R.id.mListView);
}
private void initData() {
User user = new User("卢磊", "13319405061");
User user1 = new User("小明", "13319405062");
User user2 = new User("小黄", "13319405063");
User user3 = new User("小绿", "13319405064");
User user4 = new User("卢磊", "13319405061");
User user5 = new User("小明", "13319405062");
User user6 = new User("小黄", "13319405063");
User user7 = new User("小绿", "13319405064");
User user8 = new User("卢磊", "13319405061");
User user9 = new User("小明", "13319405062");
User user10 = new User("小黄", "13319405063");
User user11 = new User("小绿", "13319405064");
User user12 = new User("卢磊", "13319405061");
User user13 = new User("小明", "13319405062");
User user14 = new User("小黄", "13319405063");
User user15 = new User("小绿", "13319405064");
User user16 = new User("卢磊", "13319405061");
User user17 = new User("小明", "13319405062");
User user18 = new User("小黄", "13319405063");
User user19 = new User("小绿", "13319405064");
User user20 = new User("卢磊", "13319405061");
User user21 = new User("小明", "13319405062");
User user22 = new User("小黄", "13319405063");
User user23 = new User("小绿", "13319405064");
// 一般从网络上获取的数据都是List集合
userList = new ArrayList<User>();
userList.add(user);
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
userList.add(user5);
userList.add(user6);
userList.add(user7);
userList.add(user8);
userList.add(user9);
userList.add(user10);
userList.add(user11);
userList.add(user12);
userList.add(user13);
userList.add(user14);
userList.add(user15);
userList.add(user16);
userList.add(user17);
userList.add(user18);
userList.add(user19);
userList.add(user20);
userList.add(user21);
userList.add(user22);
userList.add(user23);
mAdapter = new MyAdapter(MainActivity.this, userList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
mAdapter.recordPosition(arg2);
mAdapter.notifyDataSetChanged();
}
});
}
@SuppressLint("UseSparseArrays")
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<User> userList;
private List<Integer> pList = new ArrayList<Integer>();
public MyAdapter(Context context, List<User> userList) {
this.inflater = LayoutInflater.from(context);
this.userList = userList;
}
@Override
public int getCount() {
return this.userList.size();
}
@Override
public Object getItem(int position) {
return userList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public void recordPosition(int position) {
pList.add(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.name_tv = (TextView) convertView
.findViewById(R.id.name_tv);
holder.phone_tv = (TextView) convertView
.findViewById(R.id.phone_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name_tv.setText(userList.get(position).getName());
holder.phone_tv.setText(userList.get(position).getPhoneNo());
//防止convertView复用背景,导致第二页出现灰色背景,所以在判断设定为白色
convertView.setBackgroundColor(Color.WHITE);
for (int i = 0; i < pList.size(); i++) {
if (pList.get(i) == position) {
convertView.setBackgroundColor(Color.GRAY);
}
}
return convertView;
}
public class ViewHolder {
public TextView name_tv;
public TextView phone_tv;
}
}
}
数据源的类为:
package com.explink.listview;
public class User {
public User(){
}
public User(String name,String phoneNo){
this.name = name;
this.phoneNo = phoneNo;
}
private String name;
private String phoneNo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
}
资源已经上传到csdn,传送门在此: android listview已读功能 ,上一篇的博客如果有需要查看的,可以参考如下链接: android listview保持背景颜色