美文网首页
Android 采用 DialogFragment 实现微信文

Android 采用 DialogFragment 实现微信文

作者: zero_li | 来源:发表于2018-11-21 12:39 被阅读0次

    DialogFragment

    public  class MyDialogFragment extends DialogFragment {
    
        private static final String ARGS_CONTENT = "CONTENT";
        private static final String ARGS_LINE_NUM = "line_num";
    
        public static MyDialogFragment newInstance(String content, int lineNum){
            Bundle bundle = new Bundle();
            bundle.putString(ARGS_CONTENT, content);
            bundle.putInt(ARGS_LINE_NUM, lineNum);
    
            MyDialogFragment imageFragment = new MyDialogFragment();
            imageFragment.setArguments(bundle);
            return imageFragment;
        }
    
    
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_text,null);
            TextView textView = view.findViewById(R.id.tv_text);
            textView.setText(getArguments().getString(ARGS_CONTENT));
    
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
    
            int lineNum = getArguments().getInt(ARGS_LINE_NUM);
            if(lineNum>1){
                textView.setGravity(Gravity.CENTER_VERTICAL);
            }
    
            // 此处不能使用 AlertDialog实现,否则文字无法居中
            Dialog dialog = new Dialog(getContext(),R.style.Dialog_FullScreen);
            dialog.setContentView(view);
            return dialog;
        }
    
    
    }
    
    

    布局

    • ScrollView 属性 android:fillViewport="true"

    • 包一层RelativeLayout

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ll_dialog"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:padding="@dimen/sh_item_12_dp">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/tv_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:gravity="center"
                android:scrollbars="vertical"
                android:text="123456"
                android:textSize="@dimen/text_size_max" />
    
        </RelativeLayout>
    
    </ScrollView>
    

    dialog样式

    <style name="Dialog.FullScreen" parent="Theme.AppCompat.Dialog">
            <item name="android:windowBackground">@color/white</item>
            <item name="android:windowIsFloating">false</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowContentOverlay">@null</item>
    </style>
    

    调用方法

    private void showBigText(String content, int lineNum){
            StringBuilder stringBuilder = new StringBuilder(content);
    //        for (int i= 0; i<100; i++){
    //            stringBuilder.append(content);
    //        }
            MyDialogFragment dialogFragment = MyDialogFragment.newInstance(stringBuilder.toString(), lineNum);
            FragmentManager fragmentManager =((ChatActivity)mContext).getSupportFragmentManager();
            dialogFragment.show(fragmentManager,"1234");
        }
    
    

    在RecyclerView.Adapter中的调用方法

    • 结合RxBinding, 实现0.5 秒内双击触发
                if(textView.getTag() != null){
                    // 复用时,先取消,再重新设置监听
                    Disposable disposable = (Disposable)textView.getTag();
                    disposable.dispose();
                }
    
                Observable<Unit> observable = RxView.clicks(textView).share();
                Disposable disposable = observable.buffer(observable.debounce(500, TimeUnit.MILLISECONDS))
    //                    .compose(RxLifecycleUtils.bindToLifecycle((IView) mContext)))
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(list -> {
                                    if (list.size() > 1) {
                                        showBigText(stringBuilder.toString(), textView.getLineCount());
                                    }
                                }
                                , throwable -> {});
                disposable.isDisposed();
                textView.setTag(disposable);
    

    相关文章

      网友评论

          本文标题:Android 采用 DialogFragment 实现微信文

          本文链接:https://www.haomeiwen.com/subject/skaxqqtx.html