如何在像 snapchat 故事一样从底部滑动到顶部 android 时打开底部工作表片段

Posted

技术标签:

【中文标题】如何在像 snapchat 故事一样从底部滑动到顶部 android 时打开底部工作表片段【英文标题】:how to open bottom sheet fragment while sliding from bottom to top android like snapchat stories 【发布时间】:2021-05-27 13:29:14 【问题描述】:

我想实现故事的 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 故事

提前致谢

【问题讨论】:

【参考方案1】:

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

https://github.com/umano/AndroidSlidingUpPanel

【讨论】:

以上是关于如何在像 snapchat 故事一样从底部滑动到顶部 android 时打开底部工作表片段的主要内容,如果未能解决你的问题,请参考以下文章

iOS Push UIViewController 像 Snapchat 这样的滑动手势

如何在像快门一样从上到下加载时为 UIView 设置动画?

ScrollView监听滑动到顶部和底部的方法

关于ivx监听小程序滑动到顶部和底部的经验总结

Js判断H5上下滑动方向及滑动到顶部和底部判断

滚动时如何将 UIScrollView 子视图“粘贴”到顶部/底部?