Android仿抖音评论列表(加评论功能)/网易云音乐列表

老规矩 先上效果图

Android仿抖音评论列表(加评论功能)/网易云音乐列表_第1张图片
1542355190753.gif

附下Demo TikTok

如果想要实现抖音一样的评论功能,就要再列表dialog上面再弹出一个dialog.

那么问题来了:当评论的dialog弹出来的时候两个dialog就会被顶起,但是我们想要的是评论列表不要被顶起
给评论列表的dialog设置此方法即可

 Window win = dialog.getWindow();
        WindowManager.LayoutParams params = win.getAttributes();
        win.setSoftInputMode(params.SOFT_INPUT_ADJUST_NOTHING);

添加依赖 版本根据你build版本来

implementation 'com.android.support:design:27.1.1'

自定义类:ListBottomSheetDialogFragment继承BottomSheetDialogFragment

/**
 * @Author: 淘跑
 * @Date: 2018/7/2 15:18
 * @Use:
 */
public class ListBottomSheetDialogFragment extends BottomSheetDialogFragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //给dialog设置主题为透明背景 不然会有默认的白色背景
         setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomBottomSheetDialogTheme);
    }

    /**
     * 如果想要点击外部消失的话 重写此方法
     *
     * @param savedInstanceState
     * @return
     */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        //设置点击外部可消失
        dialog.setCanceledOnTouchOutside(true);
        //设置使软键盘弹出的时候dialog不会被顶起
        Window win = dialog.getWindow();
        WindowManager.LayoutParams params = win.getAttributes();
        win.setSoftInputMode(params.SOFT_INPUT_ADJUST_NOTHING);
        //这里设置dialog的进出动画 
        win.setWindowAnimations(R.style.DialogBottomAnim);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 在这里将view的高度设置为精确高度,即可屏蔽向上滑动不占全屏的手势。
        //如果不设置高度的话 会默认向上滑动时dialog覆盖全屏
        View view = inflater.inflate(R.layout.fragment_item_list_dialog, container, false);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                getScreenHeight(getActivity()) / 2));
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(new ItemAdapter(100));
        view.findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //弹出评论输入框
                InputDialog inputDialog = new InputDialog(getActivity());
                Window window = inputDialog.getWindow();
                WindowManager.LayoutParams params = window.getAttributes();
                //设置软键盘通常是可见的
                window.setSoftInputMode(params.SOFT_INPUT_STATE_VISIBLE);
                inputDialog.show();
            }
        });
    }

    private class ItemAdapter extends RecyclerView.Adapter {
        private final int mItemCount;

        ItemAdapter(int itemCount) {
            mItemCount = itemCount;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()), parent);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.text.setText(String.valueOf(position));
        }

        @Override
        public int getItemCount() {
            return mItemCount;
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            final TextView text;

            MyViewHolder(LayoutInflater inflater, ViewGroup parent) {
                super(inflater.inflate(R.layout.fragment_item_list_dialog_item, parent, false));
                text = (TextView) itemView.findViewById(R.id.text);
            }
        }

    }

    /**
     * 得到屏幕的高
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        return height;
    }

}

自定义布局文件 fragment_item_list_dialog




    


item布局文件 fragment_item_list_dialog_item




    

自定义样式style: CustomBottomSheetDialogTheme这里设置的是背景也是透明的


动画

  
    

dialog_bottom_in.xml



    

dialog_bottom_out



    

评论输入Dialog

/**
 * @Author:淘跑
 * @Date: 2018/11/14 17:20
 * @Use:
 */
public class InputDialog extends Dialog {
    public InputDialog(@NonNull Context context) {
        super(context);
//        super(context, R.style.CustomDialog);
        init(context);
    }

    public Context mContext;
    public View mRootView;

    public void init(Context context) {
        mContext = context;
        mRootView = LayoutInflater.from(context).inflate(R.layout.dialog_input, null);
        setContentView(mRootView);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        window.setGravity(Gravity.BOTTOM);
    }

}

评论输入Dialog 布局




    

你可能感兴趣的:(Android仿抖音评论列表(加评论功能)/网易云音乐列表)