简体   繁体   中英

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.

while user sliding from bottom to top the fragment start to open and loading some content.

I am using navigation component and 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

it's working good and here is my 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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