zoukankan      html  css  js  c++  java
  • Android 通用Dialog中设置RecyclerView

    通用Dialog

    public class IOSRecyclerViewDialog{
        private Context context;
        private Dialog dialog;
        private LinearLayout lLayout_bg;
        private TextView txt_title;
        private TextView txt_msg;
        private Button btn_neg;
        private Button btn_pos;
        //private ImageView img_line;
        private Display display;
        private boolean showTitle = false;
        private boolean showMsg = false;
        private boolean showPosBtn = false;
        private boolean showNegBtn = false;
        private View view;
        private FrameLayout frameLayout;
    
        public IOSRecyclerViewDialog(Context context) {
            this.context = context;
            WindowManager windowManager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            display = windowManager.getDefaultDisplay();
        }
    
        public IOSRecyclerViewDialog builder() {
            view = LayoutInflater.from(context).inflate(R.layout.dialog_recycler_view, null);
            lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
            txt_title = (TextView) view.findViewById(R.id.txt_title);
            txt_title.setVisibility(View.GONE);
            txt_msg = (TextView) view.findViewById(R.id.txt_msg);
            txt_msg.setVisibility(View.GONE);
            btn_neg = (Button) view.findViewById(R.id.btn_neg);
            btn_neg.setVisibility(View.GONE);
            btn_pos = (Button) view.findViewById(R.id.btn_pos);
            btn_pos.setVisibility(View.GONE);
    
            frameLayout = (FrameLayout) view.findViewById(R.id.customPanel);
    
            // ????Dialog????????
            dialog = new Dialog(context, R.style.AlertDialogStyle);
            dialog.setContentView(view);
            dialog.setCancelable(false);
            // ????dialog????????
            lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                    .getWidth() * 0.85), LayoutParams.WRAP_CONTENT));
    
            return this;
        }
    
        public IOSRecyclerViewDialog setCustomView(View v, LayoutParams params){
            if (frameLayout.getChildCount() > 0)
                frameLayout.removeAllViews();
    
            txt_msg.setVisibility(View.GONE);
            frameLayout.addView(v, params);
            return this;
        }
    
        public IOSRecyclerViewDialog setTitle(String title) {
            showTitle = true;
            if ("".equals(title)) {
                txt_title.setText("????");
            } else {
                txt_title.setText(title);
            }
            return this;
        }
    
        public IOSRecyclerViewDialog setMsg(String msg) {
            frameLayout.setVisibility(View.GONE);
            showMsg = true;
            if ("".equals(msg)) {
                txt_msg.setText("????");
            } else {
                txt_msg.setText(msg);
            }
            return this;
        }
    
    
        public IOSRecyclerViewDialog setCancelable(boolean cancel) {
            dialog.setCancelable(cancel);
            return this;
        }
    
        public IOSRecyclerViewDialog setCanceledOnTouchOutside(boolean cancel) {
            dialog.setCanceledOnTouchOutside(cancel);
            return this;
        }
    
        public IOSRecyclerViewDialog setPositiveButton(String text,
                                           final View.OnClickListener listener) {
            showPosBtn = true;
            if ("".equals(text)) {
                btn_pos.setText("???");
            } else {
                btn_pos.setText(text);
            }
            btn_pos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.onClick(v);
                    dialog.dismiss();
                }
            });
            return this;
        }
    
        public IOSRecyclerViewDialog setNegativeButton(String text,
                                           final View.OnClickListener listener) {
            showNegBtn = true;
            if ("".equals(text)) {
                btn_neg.setText("???");
            } else {
                btn_neg.setText(text);
            }
            btn_neg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.onClick(v);
                    dialog.dismiss();
                }
            });
            return this;
        }
    
        private void setLayout() {
            if (!showTitle && !showMsg) {
                txt_title.setText("???");
                txt_title.setVisibility(View.VISIBLE);
            }
    
            if (showTitle) {
                txt_title.setVisibility(View.VISIBLE);
            }
    
            if (!showPosBtn && !showNegBtn) {
                btn_pos.setText("???");
                btn_pos.setVisibility(View.VISIBLE);
                //btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
                btn_pos.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
    
            if (showPosBtn && showNegBtn) {
                btn_pos.setVisibility(View.VISIBLE);
                //btn_pos.setBackgroundResource(R.drawable.alertdialog_right_selector);
                btn_neg.setVisibility(View.VISIBLE);
                //btn_neg.setBackgroundResource(R.drawable.alertdialog_left_selector);
                //img_line.setVisibility(View.VISIBLE);
            }
    
            if (showPosBtn && !showNegBtn) {
                btn_pos.setVisibility(View.VISIBLE);
                //btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
            }
    
            if (!showPosBtn && showNegBtn) {
                btn_neg.setVisibility(View.VISIBLE);
                //btn_neg.setBackgroundResource(R.drawable.alertdialog_single_selector);
            }
        }
    
        public void show() {
            setLayout();
            dialog.show();
        }
    }

    其中用到的资源文件 dialog_recycler_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lLayout_bg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/alert_bg"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/txt_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="30px"
            android:gravity="center"
            android:textColor="@android:color/black"
            android:textSize="50px"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/txt_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="15dp"
            android:gravity="center"
            android:textColor="@android:color/black"
            android:textSize="16sp" />
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/customPanel"/>
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/alertdialog_line" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="20px">
    
            <!--<Button-->
                <!--android:id="@+id/btn_neg"-->
                <!--android:layout_width="wrap_content"-->
                <!--android:layout_height="43dp"-->
                <!--android:layout_weight="1"-->
                <!--android:background="@drawable/alertdialog_left_selector"-->
                <!--android:gravity="center"-->
                <!--android:textColor="@color/actionsheet_blue"-->
                <!--android:textSize="16sp" />-->
    
            <!--<ImageView-->
                <!--android:id="@+id/img_line"-->
                <!--android:layout_width="0.5dp"-->
                <!--android:layout_height="43dp"-->
                <!--android:background="@color/alertdialog_line" />-->
    
            <!--<Button-->
                <!--android:id="@+id/btn_pos"-->
                <!--android:layout_width="wrap_content"-->
                <!--android:layout_height="43dp"-->
                <!--android:layout_weight="1"-->
                <!--android:background="@drawable/alertdialog_right_selector"-->
                <!--android:gravity="center"-->
                <!--android:textColor="@color/actionsheet_blue"-->
                <!--android:textSize="16sp"-->
                <!--/>-->
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="120px"
                android:layout_marginTop="90px"
                android:layout_weight="1"
                android:gravity="center"
                android:layout_marginLeft="60px"
                android:layout_marginRight="60px"
                style="@style/text_white.58"
                android:textColor="@color/colorPrimaryDark"
                android:background="@drawable/regist_selector_bg"
                android:text="@string/logingbutton"
                android:id="@+id/btn_neg"
                android:layout_below="@+id/forget_password"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="120px"
                android:layout_marginTop="90px"
                android:layout_weight="1"
                android:gravity="center"
                style="@style/text_white.58"
                android:layout_alignParentRight="true"
                android:layout_marginRight="60px"
                android:background="@drawable/btn_circle_seletor"
                android:text="注册"
                android:id="@+id/btn_pos"
                android:layout_below="@+id/forget_password"/>
        </LinearLayout>
    
    </LinearLayout>
    
    <style name="AlertDialogStyle" parent="@android:style/Theme.Dialog">
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowFrame">@null</item>
            <item name="android:backgroundDimEnabled">true</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowIsTranslucent">true</item>
    </style>

    使用

        RecyclerView recyclerView = new RecyclerView(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(homeAdapter);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                homeAdapter.getItemCount() * Utils.getHeightInPx(this)/12);
    
        new IOSRecyclerViewDialog(this).builder()
                .setCancelable(false)
                .setCanceledOnTouchOutside(false)
                .setCustomView(recyclerView, lp)
                .setTitle(selectedBrand.getName())
                .setPositiveButton(getString(R.string.add_attention), v ->
                                presenter.saveData()
                ).setNegativeButton(getString(R.string.cancle), v -> {
                    presenter.getSelectedData().clear();
                    presenter.getSelectedData().addAll(originalAllSelected);
                    presenter.saveData();
                })
                .show();
  • 相关阅读:
    web test Grinder / nGrinder / ngrinder / gor / tcpcopy
    【python学习】之五、可调用对象
    【C/C++学习】之五、dynamic_cast
    【网络编程】之一、初识WinSocket
    【啊哈!算法】之四、选择排序
    【IOS学习】之二、虚拟机MAC Lion 10.7 下问题集锦和xcode的安装
    【网络编程】之二、socket API学习
    【C/C++学习】之六、reinterpret_cast
    【啊哈!算法】之五、归并排序
    【IOS学习】之一、VM8下安装mac lion10.7 黑苹果之续
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7562220.html
Copyright © 2011-2022 走看看