Android自定义列表选择框

public class ChoiceCustomDialog extends Dialog {
    public ChoiceCustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public ChoiceCustomDialog(Context context) {
        super(context);
    }

    /**
     * Helper class for creating a custom dialog
     */
    public static class Builder {

        private Context context;
        private String title;
        private String[] items;
        ChoiceCustomDialog dialog;

        public int getPositionSelected() {
            return positionSelected;
        }

        public void setPositionSelected(int positionSelected) {
            this.positionSelected = positionSelected;
        }

        private int positionSelected = -5;
        private OnChoiceListener onChoiceListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        public interface OnChoiceListener {
            void onClick(String item, int which);
        }

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setItems(String[] items){
            this.items = items;
            return this;
        }

        /**
         * Set the negative button text and it's listener
         *
         * @param listener
         * @return
         */
        public Builder setNegativeButton(DialogInterface.OnClickListener listener) {
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setOnChoiceListener(OnChoiceListener onChoiceListener) {
            this.onChoiceListener = onChoiceListener;
            return this;
        }

        /**
         * Create the custom dialog
         */
        public ChoiceCustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // instantiate the dialog with the custom Theme
            dialog = new ChoiceCustomDialog(context, R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);

            View layout = inflater.inflate(R.layout.layout_choice_custom_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            if (title != null) {
                ((TextView) layout.findViewById(R.id.choice_dialog_title)).setText(title);
            }

            List choiceDialogBaseContents = new ArrayList<>();
            for (String item:items) {
                ChoiceDialogBaseContent baseContent = new ChoiceDialogBaseContent(item);
                choiceDialogBaseContents.add(baseContent);
            }
            RecyclerView choiceDialogList = (RecyclerView) layout.findViewById(R.id.choice_dialog_list);
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            choiceDialogList.setLayoutManager(layoutManager);
            ChoiceItemAdapter adapter = new ChoiceItemAdapter(choiceDialogBaseContents);
            choiceDialogList.setAdapter(adapter);
//            ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_expandable_list_item_1,items);
//            choiceDialogList.setAdapter(adapter);
//            choiceDialogList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                @Override
//                public void onItemClick(AdapterView adapterView, View view, int i, long l) {
//                    onChoiceListener.onClick(items == null ? null : items[i], i);
//                    dialog.dismiss();
//                }
//            });

            // set the cancel button
            if (negativeButtonClickListener != null) {
                ((ImageView) layout.findViewById(R.id.choice_dialog_negative_button))
                        .setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                            }
                        });
            }
            dialog.setContentView(layout);
            return dialog;
        }

        public class ChoiceItemAdapter extends RecyclerView.Adapter{
            private List choiceDialogBaseContentList;
            private int selectedPosition = positionSelected;

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.choice_dialog_item, parent, false);
                final ViewHolder holder = new ViewHolder(view);
                holder.choiceDialogItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onChoiceListener.onClick(items == null ? null : items[holder.getAdapterPosition()], holder.getAdapterPosition());
                        selectedPosition = holder.getAdapterPosition();
                        notifyDataSetChanged();
                        dialog.dismiss();
                    }
                });
                return holder;
            }

            @Override
            public void onBindViewHolder(ViewHolder holder, int position) {
                ChoiceDialogBaseContent choiceDialogBaseContent = choiceDialogBaseContentList.get(position);
                holder.choiceDialogItemText.setText(choiceDialogBaseContent.getContent());
                if(position % 2 == 0){
                    holder.choiceDialogItem.setBackgroundColor(Color.parseColor("#0D000000"));
                }else{
                    holder.choiceDialogItem.setBackgroundColor(Color.parseColor("#ffffff"));
                }
                if(selectedPosition == position){
                    holder.choiceDialogItemConfrim.setVisibility(View.VISIBLE);
                }else{
                    holder.choiceDialogItemConfrim.setVisibility(View.GONE);
                }


            }

            @Override
            public int getItemCount() {
                return choiceDialogBaseContentList.size();
            }

            public ChoiceItemAdapter(List contents){
                choiceDialogBaseContentList = contents;
            }

            class ViewHolder extends RecyclerView.ViewHolder{
                TextView choiceDialogItemText;
                LinearLayout choiceDialogItem;
                ImageView choiceDialogItemConfrim;
                public ViewHolder(View view){
                    super(view);
                    choiceDialogItemText = (TextView)view.findViewById(R.id.choice_dialog_item_text);
                    choiceDialogItem = (LinearLayout)view.findViewById(R.id.choice_dialog_item);
                    choiceDialogItemConfrim = (ImageView)view.findViewById(R.id.choice_dialog_item_confirm);
                }
            }
        }

    }
}



    
        
        
    
    



    
        
            
            
        
        
    




你可能感兴趣的:(Android)