ListView带CheckBox实现单选在Android开发中会经常用到,通常的做法就是用一个bean记录CheckBox的状态,每次getView时去判断这个bean的状态。下面我们就用点击条目来实现。
public class SaleEntity {
private String price;
private String title;
private String time;
private boolean checked; // item状态
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public SaleEntity() {
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
public class SaleAdapter extends BaseAdapter {
private Context context;
private List list;
public SaleAdapter(Context context, List list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.item_sale, null);
holder.rl = convertView.findViewById(R.id.item_rl);
holder.rb = convertView.findViewById(R.id.item_rb);
holder.price = convertView.findViewById(R.id.item_price);
holder.time = convertView.findViewById(R.id.item_time);
holder.title = convertView.findViewById(R.id.item_title);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(list.get(position).getTitle());
holder.time.setText(list.get(position).getTime());
holder.price.setText(list.get(position).getPrice());
// 判断checkbox的状态
if (list.get(position).isChecked()){
holder.rb.setImageResource(R.mipmap.check_true); // 选中
}else{
holder.rb.setImageResource(R.mipmap.check_no); // 未选中
}
return convertView;
}
class ViewHolder{
RelativeLayout rl;
ImageView rb;
TextView price,title,time;
}
}
public class SaleActivity extends ToolbarActivity{
private List list;
private SaleAdapter adapter;
private TextView name;
private TextView price;
private RelativeLayout rl1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sale);
setCenterTitle("优惠券",true);
RelativeLayout rl = $(R.id.rl);
name = $(R.id.yhq_name);
price = $(R.id.yhq_price);
rl1 = $(R.id.rl1);
initData();
rl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
}
private void initData() {
list = new ArrayList<>();
for (int i = 0; i < 8; i++) {
SaleEntity saleEntity = new SaleEntity();
saleEntity.setPrice(i+"元");
saleEntity.setTitle(i+"标题");
saleEntity.setTime("有效期"+i+"年");
list.add(saleEntity);
}
}
private void showDialog() {
View view = View.inflate(this, R.layout.dialog_sale, null);
final ListView recy = view.findViewById(R.id.recy);
TextView btmName = view.findViewById(R.id.btm_name);
RelativeLayout img_rl = view.findViewById(R.id.img_rl);
final Dialog dialog = new Dialog(this, R.style.dialog);
dialog.setContentView(view);
Window window = dialog.getWindow();
//设置弹出窗口大小
assert window != null;
window.setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
//设置显示位置
window.setGravity(Gravity.BOTTOM);
//设置动画效果
window.setWindowAnimations(R.style.AnimBottom);
dialog.show();
if (list != null && list.size() > 0){
adapter = new SaleAdapter(this, list);
recy.setAdapter(adapter);
}else{
btmName.setVisibility(View.GONE);
recy.setVisibility(View.GONE);
img_rl.setVisibility(View.VISIBLE);
}
recy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
int currentNun = -1;
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
for (SaleEntity saleEntity : list){ // 遍历list集合中的数据
saleEntity.setChecked(false); // 全部设置为未选中
}
if (currentNun == -1){ // 如果选中
list.get(position).setChecked(true);
currentNun = position;
}else if(currentNun == position){ // 同一个item选中变未选中
for (SaleEntity saleEntity : list){
saleEntity.setChecked(false);
}
currentNun = -1;
}else if(currentNun != position){ // 不是同一个item选中当前的, 去除上一个选中的状态
for (SaleEntity saleEntity : list){
saleEntity.setChecked(false);
}
list.get(position).setChecked(true);
currentNun = position;
}
if (list.get(position).isChecked()){
name.setText(list.get(position).getTitle());
price.setText("¥"+list.get(position).getPrice());
rl1.setVisibility(View.VISIBLE);
}else{
name.setText("");
price.setText("");
rl1.setVisibility(View.GONE);
}
Toast.makeText(parent.getContext(),list.get(position).getTitle(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();//刷新adapter
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
for (SaleEntity saleEntity : list){
saleEntity.setChecked(false);
}
}
});
btmName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
for (SaleEntity saleEntity : list){
saleEntity.setChecked(false);
}
}
});
}
}
如有哪里不对,还请大家指出。谢谢!!