This solution worked for me. This is based on these two answers in java :
https://stackoverflow.com/a/59827320/11962518
https://stackoverflow.com/a/65202877/11962518
binding.motionLayout.setTransitionListener(new MotionLayout.TransitionListener() {
@Override
public void onTransitionStarted(MotionLayout motionLayout, int startId, int endId) {
binding.swipeLayout.setEnabled(false);
}
@Override
public void onTransitionChange(MotionLayout motionLayout, int startId, int endId, float progress) {
if (progress != 0f) {
binding.swipeLayout.setEnabled(false);
binding.swipeLayout.setRefreshing(false);
}
}
@Override
public void onTransitionCompleted(MotionLayout motionLayout, int currentId) {
}
@Override
public void onTransitionTrigger(MotionLayout motionLayout, int triggerId, boolean positive, float progress) {
}
});
binding.recyclerView.setOnTouchListener((view, motionEvent) -> {
binding.motionLayout.onTouchEvent(motionEvent);
return false;
});
Then override onScrolled method for the RecyclerView like this :
gridLayoutManager = new RtlGridLayoutManager(context, 1);
adapterRowList = new RecyclerAdapterRowItemCommodity(rowList.getRowItemHome(), context);
binding.recyclerView.setAdapter(adapterRowList);
binding.recyclerView.setLayoutManager(gridLayoutManager);
binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy) {
if (!recyclerView.canScrollVertically(-1)) { //Detects start of RecyclerView
binding.swipeLayout.setEnabled(true); // now enable swipe refresh layout
}
});
XML :
Note : You should put your recyclerView inside another layout like this :
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main_lay"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/view_bottom"
app:layout_constraintTop_toBottomOf="@id/top_buttons">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="@dimen/_8cdp"
android:paddingEnd="@dimen/_8cdp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
motion_scene :
Define Transaction like this :
<Transition
motion:constraintSetStart="@id/start"
motion:constraintSetEnd="@+id/end"
motion:duration="1000">
<OnSwipe motion:touchAnchorId="@id/main_lay"
motion:touchAnchorSide="top"
motion:dragDirection="dragUp"/>
</Transition>