簡體   English   中英

如何在從下到上滑動時打開底部片斷 android 就像 snapchat 故事

[英]how to open bottom sheet fragment while sliding from bottom to top android like snapchat stories

我想實現類似故事的 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

它運行良好,這是我的 class:

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 那樣移動屏幕的手指。

提前謝謝

如果您想手動移動,那么最好在活動記錄中使用上滑面板。 它只在活動而不是片段中起作用……這是詳細信息

https://github.com/umano/AndroidSlidingUpPanel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM