简体   繁体   English

如何在从下到上滑动时打开底部片断 android 就像 snapchat 故事

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

I want to implement something like snapchat preview of the stories.我想实现类似故事的 snapchat 预览。

while user sliding from bottom to top the fragment start to open and loading some content.当用户从底部滑动到顶部时,片段开始打开并加载一些内容。

I am using navigation component and BottomSheetDialogFragment.我正在使用导航组件和 BottomSheetDialogFragment。

for now in my main fragment I am detecting touch listener for sliding from bottom to top:现在在我的主要片段中,我正在检测从下到上滑动的触摸监听器:

    @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;
}

when slide from bottom to top I am navigating to bottomSheetDialogFragment当从下到上滑动时,我正在导航到 bottomSheetDialogFragment

it's working good and here is my class:它运行良好,这是我的 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;
}}

right now my issue is when MyBottomSheetFragment open is opening and I need to manually move my finger to dialog to dragged, what I want to achieve is I want when it's open I can continue sliding without moving the finger of the screen like snapchat stories .现在我的问题是当 MyBottomSheetFragment 打开时,我需要手动将手指移动到对话框以进行拖动,我想要实现的是当它打开时我可以继续滑动而无需像 snapchat stories 那样移动屏幕的手指。

Thanks in advance .提前谢谢

if you want to move manually then its better to use slideup panel in your activity note.如果您想手动移动,那么最好在活动记录中使用上滑面板。 it's work only in activity not fragment...here is details它只在活动而不是片段中起作用……这是详细信息

https://github.com/umano/AndroidSlidingUpPanel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM