1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| package com.example.test22_2;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ListActivity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
public class MainActivity extends AppCompatActivity { private ListView listView; private List<Map<String,Object>> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = new ArrayList<Map<String,Object>>(); listView = (ListView) findViewById(R.id.listView); Map<String,Object> map1 = new HashMap<String,Object>(); Map<String,Object> map2 = new HashMap<String,Object>(); Map<String,Object> map3 = new HashMap<String,Object>(); Map<String,Object> map4 = new HashMap<String,Object>(); Map<String,Object> map5 = new HashMap<String,Object>(); Map<String,Object> map6 = new HashMap<String,Object>(); map1.put("title","姓名"); map1.put("info","李坤松"); map1.put("picture",R.drawable.img); list.add(map1); map2.put("title","性别"); map2.put("info","男"); map2.put("picture",R.drawable.img); list.add(map2); map3.put("title","学号"); map3.put("info","2012020047"); map3.put("picture",R.drawable.img); list.add(map3); map4.put("title","年龄"); map4.put("info","19"); map4.put("picture",R.drawable.img); list.add(map4); map5.put("title","居住地"); map5.put("info","开封"); map5.put("picture",R.drawable.img); list.add(map5); map6.put("title","邮箱"); map6.put("info","2012020047@henu.edu.cn"); map6.put("picture",R.drawable.img); list.add(map6); MyAdapter adapter = new MyAdapter(this,list,R.layout.user); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { adapter.setCurrentItem(i); adapter.setClick(true); adapter.notifyDataSetChanged(); } }); } class MyAdapter extends BaseAdapter { private Context context; private int resource; private List<Map<String,Object>> list; private int mCurrentItem=0; private boolean isClick=false;
public MyAdapter(Context context, List<Map<String, Object>> list, int resource) { this.context = context; this.list = list; this.resource = resource; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { Util util = null; final int flag = i; if(view == null){ util = new Util(); LayoutInflater inflater = LayoutInflater.from(context); view = inflater.inflate(resource, null); util.picture = (ImageView) view.findViewById(R.id.picture); util.info = (TextView) view.findViewById(R.id.info); util.title = (TextView) view.findViewById(R.id.title); view.setTag(util); } else { util = (Util) view.getTag(); } Map<String, Object> map = list.get(i); util.info.setText((String) map.get("info")); util.title.setText((String) map.get("title")); util.picture.setImageResource((Integer) map.get("picture")); LinearLayout parent= (LinearLayout) view.findViewById(R.id.parent1); if (mCurrentItem==i&&isClick){ parent.setBackgroundColor(Color.parseColor("#708090")); }else{ parent.setBackgroundColor(Color.parseColor("#FFB6C1")); } return view; } public void setCurrentItem(int currentItem){ this.mCurrentItem=currentItem; } public void setClick(boolean click){ this.isClick=click; }
class Util{ TextView title; TextView info; ImageView picture; } } }
|