列表多選效果
阿新 • • 發佈:2018-11-27
效果
程式碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_spinner_up" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/colorWhite"> <TextView android:id="@+id/tv_spinner_multi" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:gravity="center" android:text="" android:textAllCaps="false" android:textColor="@color/colorTv" /> <CheckBox android:id="@+id/cb_spinner_multi" android:layout_width="13dp" android:layout_height="9dp" android:layout_centerVertical="true" android:layout_marginLeft="8dp" android:layout_toRightOf="@id/tv_spinner_multi" android:background="@drawable/selector_item_type" android:button="@null" android:checked="true" android:clickable="false" android:focusable="false" android:gravity="center" /> </RelativeLayout>
public class MyAdapter extends BaseAdapter { private Context mContext; private List<String> mList; //用來控制CheckBox的選中狀況 private static HashMap<Integer, Boolean> isSelected; public MyAdapter(Context context, List<String> list, HashMap<Integer, Boolean> isSelected) { this.mList = list; this.mContext = context; this.isSelected = isSelected; initData(); } //初始化isSelected的資料 private void initData() { for (int i = 0; i < mList.size(); i++) { getIsSelected().put(i, false); } } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; String bean = mList.get(position); LayoutInflater inflater = LayoutInflater.from(mContext); if (convertView == null) { convertView = inflater.inflate(R.layout.item_layout, parent, false); holder = new ViewHolder(); holder.tv_spinner_multi = convertView.findViewById(R.id.tv_spinner_multi); holder.cb_spinner_multi = convertView.findViewById(R.id.cb_spinner_multi); holder.rl_spinner_up = convertView.findViewById(R.id.rl_spinner_up); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tv_spinner_multi.setText(bean); //監聽checkBox並根據原來的狀態來設定新的狀態 holder.rl_spinner_up.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (isSelected.get(position)) { isSelected.put(position, false); setIsSelected(isSelected); } else { isSelected.put(position, true); setIsSelected(isSelected); } notifyDataSetChanged(); } }); //根據選中情況設定字型顏色 if (isSelected.get(position)) { holder.tv_spinner_multi.setTextColor(mContext.getResources().getColor(R.color.colorAccent)); } else { holder.tv_spinner_multi.setTextColor(mContext.getResources().getColor(R.color.colorTv)); } // 根據isSelected來設定checkbox的選中狀況 holder.cb_spinner_multi.setChecked(getIsSelected().get(position)); return convertView; } public HashMap<Integer, Boolean> getIsSelected() { return isSelected; } public void setIsSelected(HashMap<Integer, Boolean> isSelected) { this.isSelected = isSelected; } private static class ViewHolder { private TextView tv_spinner_multi; private CheckBox cb_spinner_multi; private RelativeLayout rl_spinner_up; } }
private String area[] = {"全部", "北京", "上海", "廣州", "深圳", "鄭州", "南京"}; private HashMap<Integer, Boolean> isSelectedArea = new HashMap<Integer, Boolean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listview = (ListView) findViewById(R.id.listview); MyAdapter myAdapter = new MyAdapter(this, Arrays.asList(area), isSelectedArea); listview.setAdapter(myAdapter); }