Reputation: 61
So many similar questions but mine is a little different. When scrolling down, the activity works perfectly. However, when I scroll up, instead of scrolling, it refreshes. What I actually want is it will refresh only when I'm on the top of the nested scroll view. I have to double tap quickly so I can scroll up which is kinda hard.
Screenshot:
Details.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".details">
<android.support.design.circularreveal.
coordinatorlayout.CircularRevealCoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#0e0d0e"
app:expandedTitleTextAppearance="@android:color/transparent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/img_beach"
android:contentDescription="@null"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:title="Beach Name"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/btnSubmit"
android:src="@drawable/send"
android:backgroundTint="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:elevation="6dp"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:pressedTranslationZ="12dp"
app:useCompatPadding="true"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_rating"
android:src="@drawable/ic_add_black_24dp"
android:backgroundTint="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:elevation="6dp"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|start"
app:pressedTranslationZ="12dp"
app:useCompatPadding="true"
/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/beach_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="Food Name"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/layout_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/money" />
<TextView
android:id="@+id/beach_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:text="1,000"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:id="@+id/number_button"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="10dp"
app:backGroundColor="@color/number_button"
app:finalNumber="20"
app:initialNumber="1"
app:textSize="8sp" />
<LinearLayout
android:id="@+id/layout_checkDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/checkInBTN"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="CHECK IN"
android:textSize="12sp" />
<TextView
android:id="@+id/checkInTV"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="10dp"
android:gravity="center_vertical"
android:text="CHECK IN DATE"
android:textAlignment="center" />
<Button
android:id="@+id/checkOutBTN"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="CHECK OUT"
android:textSize="12sp" />
<TextView
android:id="@+id/checkOutTV"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="10dp"
android:gravity="center_vertical"
android:text="CHECK OUT DATE"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_checkAvailability"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/checkAvailabilityBTN"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center|center_horizontal"
android:text="CHECK AVAILABILITY"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:isIndicator="true"
android:max="5"
android:rating="0" />
<TextView
android:id="@+id/beach_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="@android:color/black"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/beach_review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="REVIEWS"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold|italic" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerComment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="12dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/beach_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="GALLERY"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold|italic" />
<ImageView
android:id="@+id/img_beach2"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:src="@drawable/background" />
<ImageView
android:id="@+id/img_beach3"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="20dp"
android:scaleType="centerCrop"
android:src="@drawable/background" />
<ImageView
android:id="@+id/img_beach4"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="20dp"
android:scaleType="centerCrop"
android:src="@drawable/background" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.circularreveal.
coordinatorlayout.CircularRevealCoordinatorLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Details.java
mSwipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
if(!menuID.isEmpty() && menuID != null){
Query query = ratingTbl.orderByChild("menuID").equalTo(menuID);
FirebaseRecyclerOptions<Rating> options = new FirebaseRecyclerOptions.Builder<Rating>()
.setQuery(query, Rating.class)
.build();
adapter = new FirebaseRecyclerAdapter<Rating, ShowCommentViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ShowCommentViewHolder holder, int position, @NonNull Rating model) {
holder.ratingBar.setRating(Float.parseFloat(model.getRateValue()));
holder.txtComment.setText(model.getComment());
holder.txtUserEmail.setText(model.getUserEmail());
}
@NonNull
@Override
public ShowCommentViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_comment_layout, parent, false);
return new ShowCommentViewHolder(view);
}
};
loadComment(menuID);
}
}
});
mSwipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
if(!menuID.isEmpty() && menuID != null){
Query query = ratingTbl.orderByChild("menuID").equalTo(menuID);
FirebaseRecyclerOptions<Rating> options = new FirebaseRecyclerOptions.Builder<Rating>()
.setQuery(query, Rating.class)
.build();
adapter = new FirebaseRecyclerAdapter<Rating, ShowCommentViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ShowCommentViewHolder holder, int position, @NonNull Rating model) {
holder.ratingBar.setRating(Float.parseFloat(model.getRateValue()));
holder.txtComment.setText(model.getComment());
holder.txtUserEmail.setText(model.getUserEmail());
}
@NonNull
@Override
public ShowCommentViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_comment_layout, parent, false);
return new ShowCommentViewHolder(view);
}
};
loadComment(menuID);
}
}
});
Upvotes: 5
Views: 8010
Reputation: 206
This is how i solved it
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if(scrollY > 5){
mSwipeRefreshLayout.setEnabled(false);
}else{
mSwipeRefreshLayout.setEnabled(true);
}
}
});
Upvotes: 4
Reputation: 85
Did you try to wrap your SwipeRefreshLayout with NestedScrollView?
Worked for me:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="0dp"
android:layout_height="0dp">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<Your_View_Here>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Upvotes: 6
Reputation: 1263
If RecyclerView
or ListView
is not the direct child of SwipeRefreshLayout
then this issue occurs.
Simplest solution is to provide OnChildScrollUpCallback
implementation and return the results appropriately. In Kotlin code below, refreshLayout
is SwipeRefreshLayout
and recyclerView
is RecyclerView
as can be seen in xml
layout code as well.
refreshLayout.setOnChildScrollUpCallback(object : SwipeRefreshLayout.OnChildScrollUpCallback {
override fun canChildScrollUp(parent: SwipeRefreshLayout, child: View?): Boolean {
if (recyclerView != null) {
return recyclerView.canScrollVertically(-1)
}
return false
}
})
While xml
layout is something like this,
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refreshLayout"
...
lots of other views i.e TextView, ImageView, MotionLayout
...
...
...
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView".../>
...
...
...
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Upvotes: 13
Reputation: 121
I added OnScrollChangeListener
on the ScrollView
to enable/disable SwipeRefreshLayout
based on the scroll's position. I enable swipe refresh only when the scrollY is at the top and it doesn't get in the way of my scrollView.
The code in Kotlin looks like this:
scrollView.setOnScrollChangeListener { v: View, scrollX: Int, scrollY: Int, _: Int, _: Int ->
swipeRefreshLayout.isEnabled = scrollY == 0
}
Upvotes: 4
Reputation: 61
I have already fixed it by putting the Swipe Refresh Layout inside the Coordinator Layout. Thanks Much to all Viewers :)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.circularreveal.coordinatorlayout.
CircularRevealCoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
tools:context=".details"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#0e0d0e"
app:expandedTitleTextAppearance="@android:color/transparent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/img_beach"
android:contentDescription="@null"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:title="Beach Name"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/btnSubmit"
android:src="@drawable/send"
android:backgroundTint="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:elevation="6dp"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:pressedTranslationZ="12dp"
app:useCompatPadding="true"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_rating"
android:src="@drawable/ic_add_black_24dp"
android:backgroundTint="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:elevation="6dp"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|start"
app:pressedTranslationZ="12dp"
app:useCompatPadding="true"
/>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipe_layout"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/beach_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="Food Name"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/layout_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/money" />
<TextView
android:id="@+id/beach_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:text="1,000"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:id="@+id/number_button"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="10dp"
app:backGroundColor="@color/number_button"
app:finalNumber="20"
app:initialNumber="1"
app:textSize="8sp" />
<LinearLayout
android:id="@+id/layout_checkDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/checkInBTN"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="CHECK IN"
android:textSize="12sp" />
<TextView
android:id="@+id/checkInTV"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="10dp"
android:gravity="center_vertical"
android:text="CHECK IN DATE"
android:textAlignment="center" />
<Button
android:id="@+id/checkOutBTN"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="CHECK OUT"
android:textSize="12sp" />
<TextView
android:id="@+id/checkOutTV"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="10dp"
android:gravity="center_vertical"
android:text="CHECK OUT DATE"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_checkAvailability"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/checkAvailabilityBTN"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center|center_horizontal"
android:text="CHECK AVAILABILITY"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:isIndicator="true"
android:max="5"
android:rating="0" />
<TextView
android:id="@+id/beach_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="@android:color/black"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/beach_review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="REVIEWS"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold|italic" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerComment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="12dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/beach_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="GALLERY"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold|italic" />
<ImageView
android:id="@+id/img_beach2"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:src="@drawable/background" />
<ImageView
android:id="@+id/img_beach3"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="20dp"
android:scaleType="centerCrop"
android:src="@drawable/background" />
<ImageView
android:id="@+id/img_beach4"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="20dp"
android:scaleType="centerCrop"
android:src="@drawable/background" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Upvotes: 0