0

我想实现类似故事的 snapchat 预览。

当用户从底部滑动到顶部时,片段开始打开并加载一些内容。

我正在使用导航组件和 BottomSheetDialogFragment 。

现在在我的主要片段中,我正在检测从底部到顶部滑动的触摸侦听器:

    @Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {

            downX = event.getX();
            downY = event.getY();
            return true;
        }
        case MotionEvent.ACTION_MOVE:
            Log.d(TAG, "onTouch: ACTION_MOVE ");
            moveY = event.getY();
            moveX = event.getX();
            float deltaY2 = downY - moveY;
            float deltaX2 = downX - moveX;
            if (Math.abs(deltaX2) > MIN_DISTANCE) {
                if (deltaX2 < 0) {
                    Log.d(TAG, "onTouch: ACTION_MOVE RIGHT");
                    return true;
                } else if (deltaX2 > 0) {
                    Log.d(TAG, "onTouch: ACTION_MOVE LEFT");
                    return true;
                }
            }

            if (deltaY2 < 0) {
                Log.d(TAG, "onTouch: ACTION_MOVE bottom");
                return true;
            }
            if (deltaY2 > 0) {
                Log.d(TAG, "onTouch: ACTION_MOVE UP");
                if (isFirst) {
                    isFirst = false;  
            Navigation.findNavController(getView()).navigate(MainFragmentDirections.actionMainFragmentToMyBottomSheetFragment());
                }
                return false;
            }
            break;
        case MotionEvent.ACTION_UP: {
            isFirst = true;
        }


    }
    return false;
}

当从下到上滑动时,我正在导航到 bottomSheetDialogFragment

它工作得很好,这是我的课:

public class MyBottomSheetFragment extends BottomSheetDialogFragment {

private static final String TAG = "MyBottomSheetFragment";
private View mInflatedView ;


public MyBottomSheetFragment() {
    // Required empty public constructor
}


public static MyBottomSheetFragment newInstance(String param1, String param2) {
    MyBottomSheetFragment fragment = new MyBottomSheetFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    mInflatedView = inflater.inflate(R.layout.fragment_my_bottom_sheet, container, false);

    return mInflatedView;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface;
            setupFullHeight(bottomSheetDialog);
        }
    });
    return dialog;
}

private void setupFullHeight(BottomSheetDialog bottomSheetDialog) {
    FrameLayout bottomSheet = bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);
    BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);

    ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams();
    int windowHeight = getWindowHeight();
    if (layoutParams != null) {
        layoutParams.height = windowHeight;
    }
    bottomSheet.setLayoutParams(layoutParams);
    behavior.setPeekHeight(100);

}

private int getWindowHeight() {
    // Calculate window height for fullscreen use
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.heightPixels;
}}

现在我的问题是当 MyBottomSheetFragment 打开时,我需要手动将手指移动到对话框以进行拖动,我想要实现的是当它打开时我可以继续滑动而无需像 snapchat stories 那样移动屏幕的手指。

提前谢谢

4

1 回答 1

0

如果您想手动移动,那么最好在活动记录中使用上滑面板。它只在活动而不是片段中起作用……这是详细信息

https://github.com/umano/AndroidSlidingUpPanel
于 2021-02-27T07:08:07.113 回答