当前位置:   article > 正文

Android ListView点击之后保持更换的背景色,实现已读功能_listview 保持背景色

listview 保持背景色

        因为项目中有这样的需求:一般消息类或者资讯类的界面,都有类似于网易客户端的点击之后更换背景色,并且保持这个颜色,表示已读取该条信息。这个功能一开始觉得已经实现了,在我的另一篇博客上有:点击打开链接 后来测试人员在测试的时候,发现这样一个奇怪的问题:

       当点击了listview的第0项,背景颜色改变,并且保持着背景没错,但滑动到下一个页面的listview,原本没有点击的item,竟然莫名其妙的出现了更换的背景。。。


      出现上述的情况是因为我实现的原理是这样的:当用户点击某一个item,会进入到监听方法里面,我设置一个变量保存所选中的位置position,也就是如下代码:

  1. arg1.setSelected(true);
  2. selectItem = arg2; // 当前选择的节目item
  3. messageAd.notifyDataSetChanged(); // 通知adapter刷新数据

     在适配器里面,我就使用if语句判断,如果position等于刚才所点击的item,则设置背景颜色,如下代码:

  1. if (selectItem == position) {
  2. //设置背景
  3. // }

      上述设置就会出现错乱的问题,原因以后再深究,下面贴出解决的办法,不多说直接上代码,有注释:

  1. package com.explink.listview;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AdapterView;
  9. import android.widget.AdapterView.OnItemClickListener;
  10. import android.widget.BaseAdapter;
  11. import android.widget.ListView;
  12. import android.widget.TextView;
  13. import android.annotation.SuppressLint;
  14. import android.app.Activity;
  15. import android.content.Context;
  16. import android.graphics.Color;
  17. public class MainActivity extends Activity {
  18. private ListView mListView;
  19. private MyAdapter mAdapter;
  20. List<User> userList;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. initView();
  26. initData();
  27. }
  28. private void initView() {
  29. mListView = (ListView) findViewById(R.id.mListView);
  30. }
  31. private void initData() {
  32. User user = new User("卢磊", "13319405061");
  33. User user1 = new User("小明", "13319405062");
  34. User user2 = new User("小黄", "13319405063");
  35. User user3 = new User("小绿", "13319405064");
  36. User user4 = new User("卢磊", "13319405061");
  37. User user5 = new User("小明", "13319405062");
  38. User user6 = new User("小黄", "13319405063");
  39. User user7 = new User("小绿", "13319405064");
  40. User user8 = new User("卢磊", "13319405061");
  41. User user9 = new User("小明", "13319405062");
  42. User user10 = new User("小黄", "13319405063");
  43. User user11 = new User("小绿", "13319405064");
  44. User user12 = new User("卢磊", "13319405061");
  45. User user13 = new User("小明", "13319405062");
  46. User user14 = new User("小黄", "13319405063");
  47. User user15 = new User("小绿", "13319405064");
  48. User user16 = new User("卢磊", "13319405061");
  49. User user17 = new User("小明", "13319405062");
  50. User user18 = new User("小黄", "13319405063");
  51. User user19 = new User("小绿", "13319405064");
  52. User user20 = new User("卢磊", "13319405061");
  53. User user21 = new User("小明", "13319405062");
  54. User user22 = new User("小黄", "13319405063");
  55. User user23 = new User("小绿", "13319405064");
  56. // 一般从网络上获取的数据都是List集合
  57. userList = new ArrayList<User>();
  58. userList.add(user);
  59. userList.add(user1);
  60. userList.add(user2);
  61. userList.add(user3);
  62. userList.add(user4);
  63. userList.add(user5);
  64. userList.add(user6);
  65. userList.add(user7);
  66. userList.add(user8);
  67. userList.add(user9);
  68. userList.add(user10);
  69. userList.add(user11);
  70. userList.add(user12);
  71. userList.add(user13);
  72. userList.add(user14);
  73. userList.add(user15);
  74. userList.add(user16);
  75. userList.add(user17);
  76. userList.add(user18);
  77. userList.add(user19);
  78. userList.add(user20);
  79. userList.add(user21);
  80. userList.add(user22);
  81. userList.add(user23);
  82. mAdapter = new MyAdapter(MainActivity.this, userList);
  83. mListView.setAdapter(mAdapter);
  84. mListView.setOnItemClickListener(new OnItemClickListener() {
  85. @Override
  86. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  87. long arg3) {
  88. mAdapter.recordPosition(arg2);
  89. mAdapter.notifyDataSetChanged();
  90. }
  91. });
  92. }
  93. @SuppressLint("UseSparseArrays")
  94. public class MyAdapter extends BaseAdapter {
  95. private LayoutInflater inflater;
  96. private List<User> userList;
  97. private List<Integer> pList = new ArrayList<Integer>();
  98. public MyAdapter(Context context, List<User> userList) {
  99. this.inflater = LayoutInflater.from(context);
  100. this.userList = userList;
  101. }
  102. @Override
  103. public int getCount() {
  104. return this.userList.size();
  105. }
  106. @Override
  107. public Object getItem(int position) {
  108. return userList.get(position);
  109. }
  110. @Override
  111. public long getItemId(int position) {
  112. return position;
  113. }
  114. public void recordPosition(int position) {
  115. pList.add(position);
  116. }
  117. @Override
  118. public View getView(int position, View convertView, ViewGroup parent) {
  119. ViewHolder holder;
  120. if (convertView == null) {
  121. convertView = inflater.inflate(R.layout.list_item, null);
  122. holder = new ViewHolder();
  123. holder.name_tv = (TextView) convertView
  124. .findViewById(R.id.name_tv);
  125. holder.phone_tv = (TextView) convertView
  126. .findViewById(R.id.phone_tv);
  127. convertView.setTag(holder);
  128. } else {
  129. holder = (ViewHolder) convertView.getTag();
  130. }
  131. holder.name_tv.setText(userList.get(position).getName());
  132. holder.phone_tv.setText(userList.get(position).getPhoneNo());
  133. //防止convertView复用背景,导致第二页出现灰色背景,所以在判断设定为白色
  134. convertView.setBackgroundColor(Color.WHITE);
  135. for (int i = 0; i < pList.size(); i++) {
  136. if (pList.get(i) == position) {
  137. convertView.setBackgroundColor(Color.GRAY);
  138. }
  139. }
  140. return convertView;
  141. }
  142. public class ViewHolder {
  143. public TextView name_tv;
  144. public TextView phone_tv;
  145. }
  146. }
  147. }
      数据源的类为:

  1. package com.explink.listview;
  2. public class User {
  3. public User(){
  4. }
  5. public User(String name,String phoneNo){
  6. this.name = name;
  7. this.phoneNo = phoneNo;
  8. }
  9. private String name;
  10. private String phoneNo;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public String getPhoneNo() {
  18. return phoneNo;
  19. }
  20. public void setPhoneNo(String phoneNo) {
  21. this.phoneNo = phoneNo;
  22. }
  23. }

           资源已经上传到csdn,传送门在此: android listview已读功能  ,上一篇的博客如果有需要查看的,可以参考如下链接: android listview保持背景颜色

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/293705
推荐阅读
相关标签
  

闽ICP备14008679号