对 ListView 进行单选和多选操作的时候 首先要进行ListView的模式设置
if (isOne) { mBtn.setText("单选"); mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } else { mBtn.setText("多选"); mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); }
package com.aegis.testlist; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener { private ListView mListView; private Button mBtn; private boolean isOne = false; private mySelectAdapter adapter; private List<String> datas = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for (int i = 0; i < 20; i++) { datas.add("数据" + i); } initView(); } private void initView() { mListView = (ListView) findViewById(R.id.ListView); mBtn = (Button) findViewById(R.id.Btn); mBtn.setOnClickListener(this); if (isOne) { mBtn.setText("单选"); mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } else { mBtn.setText("多选"); mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); } adapter = new mySelectAdapter(this,datas); mListView.setAdapter(adapter); mListView.setOnItemClickListener(this); mListView.setScrollbarFadingEnabled(true); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.Btn: break; } } class mySelectAdapter extends BaseAdapter { private List<String> list = new ArrayList<>(); private int selectedPosition = -1;// 选中的位置 private Context ctx; private List<String> lists; public mySelectAdapter(Context context, List<String> Lists) { ctx = context; lists = Lists; for (int i = 0; i < lists.size(); i++) { } } public void setSelectedPosition(int position) { selectedPosition = position; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { SelectHolder holder = null; if (convertView == null) { convertView = View.inflate(ctx, R.layout.item_chatdialog, null); holder = new SelectHolder(convertView); convertView.setTag(holder); } else { holder = (SelectHolder) convertView.getTag(); } String data = datas.get(position); holder.mPosition.setText(position + 1 + ""); holder.mContent.setText(data); if (isOne) { if (position % 2 == 0) { if (selectedPosition == position) { convertView.setSelected(true); convertView.setPressed(true); holder.mContent.setTextColor(getResources().getColor(R.color.dialog_select_yes)); holder.mPosition.setTextColor(getResources().getColor(R.color.white)); holder.mPosition.setBackgroundResource(R.mipmap.shuzidiwen1); } else { convertView.setSelected(false); convertView.setPressed(false); holder.mContent.setTextColor(getResources().getColor(R.color.dialog_select_yes)); holder.mPosition.setTextColor(getResources().getColor(R.color.dialog_choose_yes)); holder.mPosition.setBackgroundResource(R.mipmap.shuzidiwen2); } } else { if (selectedPosition == position) { convertView.setSelected(true); convertView.setPressed(true); holder.mContent.setTextColor(getResources().getColor(R.color.dialog_select_yes)); holder.mPosition.setTextColor(getResources().getColor(R.color.white)); holder.mPosition.setBackgroundResource(R.mipmap.shuzidiwen1); } else { convertView.setSelected(false); convertView.setPressed(false); holder.mContent.setTextColor(getResources().getColor(R.color.dialog_select_yes)); holder.mPosition.setTextColor(getResources().getColor(R.color.dialog_choose_yes)); holder.mPosition.setBackgroundResource(R.mipmap.shuzidiwen2); } } } else { if (mListView.isItemChecked(position)) { holder.mContent.setTextColor(getResources().getColor(R.color.dialog_select_yes)); holder.mPosition.setTextColor(getResources().getColor(R.color.white)); holder.mPosition.setBackgroundResource(R.mipmap.shuzidiwen1); } else { holder.mContent.setTextColor(getResources().getColor(R.color.dialog_select_yes)); holder.mPosition.setTextColor(getResources().getColor(R.color.dialog_choose_yes)); holder.mPosition.setBackgroundResource(R.mipmap.shuzidiwen2); } } return convertView; } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (isOne) { adapter.setSelectedPosition(position); adapter.notifyDataSetInvalidated(); } else { adapter.notifyDataSetChanged(); } } class SelectHolder { public TextView mPosition, mContent; public LinearLayout mLayout; public SelectHolder(View convertView) { this.mPosition = (TextView) convertView.findViewById(R.id.item_chatDialog_position); this.mContent = (TextView) convertView.findViewById(R.id.item_chatDialog_content); this.mLayout = (LinearLayout) convertView.findViewById(R.id.item_chatDialog_layout); } } }