Android 采用 DialogFragment 实现微信文字放大显示

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




    

        

    


dialog样式


调用方法

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 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 实现微信文字放大显示)