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<ChoiceDialogBaseContent> 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<String> adapter = new ArrayAdapter<String>(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<ChoiceItemAdapter.ViewHolder>{
            private List<ChoiceDialogBaseContent> 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<ChoiceDialogBaseContent> 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);
                }
            }
        }

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_choice_dialog_bg"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/choice_dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#666666"
            android:textSize="25sp"
            android:layout_centerInParent="true" />
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/cancel"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"
            android:padding="7dp"
            android:id="@+id/choice_dialog_negative_button"/>
    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/choice_dialog_list"></android.support.v7.widget.RecyclerView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/choice_dialog_item">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/choice_dialog_item_text"
                android:layout_marginLeft="20dp"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                android:textSize="20sp"
                android:textColor="#666666"/>
            <ImageView
                android:layout_width="20dp"
                android:layout_height="15dp"
                android:layout_marginRight="20dp"
                android:layout_gravity="center"
                android:src="@drawable/selection"
                android:id="@+id/choice_dialog_item_confirm"
                android:visibility="gone"/>
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorWhiteAlpha20"
            android:layout_marginLeft="30dp" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值