Android ListView最佳处理方式,ListView拖动防重复数据显示,单击响应子控件。

1、为了防止拖动ListView时,在列表末尾重复数据显示。需要加入 HashMap<Integer,View> lmap = new HashMap<Integer,View>();其中Integer为列表位置,View为子项视图,加入数据前首先if
(lmap.get(position)==null) ,满足条件时,加入lmap.put(position, convertView);如果条件不满足,convertView = lmap.get(position);

2、监听每个子控件时,一定要加入final int currentPosition=position;这样可以牢牢抓住每次点击的响应位置;最好把控件集成到类中。

    1. package logic;
    2. import java.util.HashMap;
    3. import java.util.List;
    4. import java.util.Map;
    5. import logic.PlaceAdapter.ViewHolder;
    6. import org.guiji.BigPictureActivity;
    7. import org.guiji.ClassUserListActivity;
    8. import org.guiji.CommentMoodActivity;
    9. import org.guiji.R;
    10. import org.guiji.UserHomeActivity;
    11. import support.ClassUserListImageDownloadTask;
    12. import support.ImageDownloadTask;
    13. import support.SystemConstant;
    14. import android.content.Context;
    15. import android.content.Intent;
    16. import android.text.Html;
    17. import android.view.LayoutInflater;
    18. import android.view.View;
    19. import android.view.ViewGroup;
    20. import android.view.View.OnClickListener;
    21. import android.widget.BaseAdapter;
    22. import android.widget.ImageView;
    23. import android.widget.TextView;
    24. public class ClassUserListAdapter extends BaseAdapter {
    25. HashMap<Integer,View> lmap = new HashMap<Integer,View>();
    26. private LayoutInflater mInflater=null;
    27. private List<Map<String, String>> mData=null;
    28. private ClassUserListImageDownloadTask imgtask=null;
    29. private Context context;
    30. public List<Map<String, String>> getmData() {
    31. return mData;
    32. }
    33. public void setmData(List<Map<String, String>> mData) {
    34. this.mData = mData;
    35. }
    36. public ClassUserListAdapter(Context context){
    37. this.mInflater = LayoutInflater.from(context);
    38. this.context=context;
    39. }
    40. @Override
    41. public int getCount() {
    42. return mData.size();
    43. }
    44. @Override
    45. public Object getItem(int position) {
    46. return mData.get(position);
    47. }
    48. @Override
    49. public long getItemId(int position) {
    50. return position;
    51. }
    52. @Override
    53. public View getView(int position, View convertView, ViewGroup parent) {
    54. ClassUserListViewHolder holder = null;
    55. if (lmap.get(position)==null) {
    56. imgtask=new ClassUserListImageDownloadTask();
    57. convertView = mInflater.inflate(R.layout.classuserlist_item, null);
    58. holder=new ClassUserListViewHolder();
    59. holder.classuserlist_item_userlogo=(ImageView)convertView.findViewById(R.id.classuserlist_item_userlogo);
    60. holder.classuserlist_item_username=(TextView)convertView.findViewById(R.id.classuserlist_item_username);
    61. holder.classuserlist_item_statuscontent=(TextView)convertView.findViewById(R.id.classuserlist_item_statuscontent);
    62. holder.classuserlist_item_idfans1=(TextView)convertView.findViewById(R.id.classuserlist_item_idfans1);
    63. holder.classuserlist_item_idfans2=(TextView)convertView.findViewById(R.id.classuserlist_item_idfans2);
    64. holder.classuserlist_item_idmood=(TextView)convertView.findViewById(R.id.classuserlist_item_idmood);
    65. holder.classuserlist_item_idhuoyuevalue=(TextView)convertView.findViewById(R.id.classuserlist_item_idhuoyuevalue);
    66. holder.classuserlist_item_msgpic=(ImageView)convertView.findViewById(R.id.classuserlist_item_msgpic);
    67. holder.classuserlist_item_msgcontent=(TextView)convertView.findViewById(R.id.classuserlist_item_msgcontent);
    68. holder.classuserlist_item_idtimeplace=(TextView)convertView.findViewById(R.id.classuserlist_item_idtimeplace);
    69. holder.classuserlist_item_classbutton=(ImageView)convertView.findViewById(R.id.classuserlist_item_classbutton);
    70. lmap.put(position, convertView);
    71. convertView.setTag(holder);
    72. holder.classuserlist_item_username.setText((String)mData.get(position).get("username"));
    73. if(mData.get(position).get("idstatuscontent")!=null){
    74. holder.classuserlist_item_statuscontent.setText((String)mData.get(position).get("idstatuscontent"));
    75. holder.classuserlist_item_statuscontent.setVisibility(0);
    76. }
    77. if(mData.get(position).get("idfans1")!=null){
    78. holder.classuserlist_item_idfans1.setText((String)mData.get(position).get("idfans1"));
    79. holder.classuserlist_item_idfans1.setVisibility(0);
    80. }
    81. if(mData.get(position).get("idfans2")!=null){
    82. holder.classuserlist_item_idfans2.setText((String)mData.get(position).get("idfans2"));
    83. holder.classuserlist_item_idfans2.setVisibility(0);
    84. }
    85. holder.classuserlist_item_idmood.setText((String)mData.get(position).get("idmood"));
    86. if(mData.get(position).get("idhuoyuevalue").length()>=4)
    87. holder.classuserlist_item_idhuoyuevalue.setText("活跃值"+"("+mData.get(position).get("idhuoyuevalue").substring(0, 4)+")");
    88. else
    89. holder.classuserlist_item_idhuoyuevalue.setText("活跃值"+"("+mData.get(position).get("idhuoyuevalue")+"0"+")");
    90. if(mData.get(position).get("idmsgcontent")!=null){
    91. holder.classuserlist_item_msgcontent.setText((String)mData.get(position).get("idmsgcontent"));
    92. holder.classuserlist_item_msgcontent.setVisibility(0);
    93. }
    94. if(mData.get(position).get("idtime")!=null){
    95. holder.classuserlist_item_idtimeplace.setText((String)mData.get(position).get("idtime")+"  "+(String)mData.get(position).get("idplace"));
    96. holder.classuserlist_item_idtimeplace.setVisibility(0);
    97. }
    98. String temp=SystemConstant.baseURLNone+mData.get(position).get("userlogo")+","+
    99. SystemConstant.baseURLNone+mData.get(position).get("idmsgpic");
    100. imgtask.execute(temp,holder);
    101. }else {
    102. convertView = lmap.get(position);
    103. holder = (ClassUserListViewHolder)convertView.getTag();
    104. }
    105. final int currentPosition=position;
    106. holder.classuserlist_item_userlogo.setOnClickListener(new OnClickListener() {
    107. @Override
    108. public void onClick(View v) {
    109. MainService.guiji.setCurrentURL(SystemConstant.baseURL+mData.get(currentPosition).get("userLink"));
    110. Intent it=new Intent(context,UserHomeActivity.class);
    111. context.startActivity(it);
    112. }
    113. });
    114. holder.classuserlist_item_username.setOnClickListener(new OnClickListener() {
    115. @Override
    116. public void onClick(View v) {
    117. MainService.guiji.setCurrentURL(SystemConstant.baseURL+mData.get(currentPosition).get("userLink"));
    118. Intent it=new Intent(context,UserHomeActivity.class);
    119. context.startActivity(it);
    120. }
    121. });
    122. holder.classuserlist_item_msgpic.setOnClickListener(new OnClickListener() {
    123. @Override
    124. public void onClick(View v) {
    125. MainService.guiji.setCurrentURL(SystemConstant.baseURLNone+mData.get(currentPosition).get("idmsgpic"));
    126. Intent it=new Intent(context,BigPictureActivity.class);
    127. context.startActivity(it);
    128. }
    129. });
    130. holder.classuserlist_item_idfans1.setOnClickListener(new OnClickListener() {
    131. @Override
    132. public void onClick(View v) {
    133. MainService.guiji.deleteReply(SystemConstant.baseURL+mData.get(currentPosition).get("idfans1link"));
    134. ((ClassUserListActivity) context).createTask();
    135. }
    136. });
    137. holder.classuserlist_item_idfans2.setOnClickListener(new OnClickListener() {
    138. @Override
    139. public void onClick(View v) {
    140. MainService.guiji.deleteReply(SystemConstant.baseURL+mData.get(currentPosition).get("idfans2link"));
    141. ((ClassUserListActivity) context).createTask();
    142. }
    143. });
    144. holder.classuserlist_item_classbutton.setOnClickListener(new OnClickListener() {
    145. @Override
    146. public void onClick(View v) {
    147. String temp=SystemConstant.baseURL+mData.get(currentPosition).get("idcommentlink");
    148. //              MainService.guiji.setCurrentURL(temp);
    149. MainService.guiji.setPageType(4);
    150. MainService.guiji.setBackURL(temp);
    151. Intent it=new Intent(context,CommentMoodActivity.class);
    152. context.startActivity(it);
    153. }
    154. });
    155. return convertView;
    156. }
    157. public class ClassUserListViewHolder{
    158. public ImageView classuserlist_item_userlogo;
    159. public TextView classuserlist_item_username;
    160. public TextView classuserlist_item_statuscontent;
    161. public TextView classuserlist_item_idfans1;
    162. public TextView classuserlist_item_idfans2;
    163. public TextView classuserlist_item_idmood;
    164. public TextView classuserlist_item_idhuoyuevalue;
    165. public ImageView classuserlist_item_msgpic;
    166. public TextView classuserlist_item_msgcontent;
    167. public TextView classuserlist_item_idtimeplace;
    168. public ImageView classuserlist_item_classbutton;
    169. }
    170. }
05-11 14:11