Android ListViewエントリをクリックしてCheckBoxラジオを実現し、選択をキャンセルする

14805 ワード

前言
ListViewCheckBox付きラジオはAndroid開発でよく使われるが、通常は1つのbeanでCheckBoxの状態を記録し、getViewのたびにこのbeanの状態を判断する.次に、エントリをクリックして実現します.
効果図
図の中で1つの底のdialogを弾いて、実はページと同じです
コードクリップ
JavaBeanエンティティークラスデータ

    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;
                        }
                        }

SaleAdapter:Adapterアダプタに重要なgetViewコードが貼られており、getViewではposition位置状態およびjavaBeanに基づいて状態判断を行います.
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;
    }

}

SaleActivity:OnItemClickListenerはSaleEntityのisChecked状態によってCheckBoxが選択されているかどうかを制御します.
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);
                }
            }
        });
    }

}

終了:
何か間違いがあれば、指摘してください.ありがとう!