如何打开底部页段,同时从底部滑动到顶部android像snapchat的故事



我想实现类似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的故事一样.

.

如果你想手动移动,那么最好在你的活动中使用滑动面板请注意。它只是活动中的工作,而不是片断中的工作……这里是细节

https://github.com/umano/AndroidSlidingUpPanel

最新更新