android - 由于滑动刷新布局,我无法向上滚动

标签 android swiperefreshlayout android-nestedscrollview

这么多类似的问题,但我的有点不同。向下滚动时, Activity 完美运行。但是,当我向上滚动而不是滚动时,它会刷新。我真正想要的是只有当我位于嵌套 ScrollView 的顶部时它才会刷新。我必须快速双击才能向上滚动,这有点困难。

截图:

enter image description here

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


        }
    });

最佳答案

如果 RecyclerViewListView 不是 SwipeRefreshLayout 的直接子级,则会出现此问题。

最简单的解决方案是提供OnChildScrollUpCallback 实现并适本地返回结果。在下面的 Kotlin 代码中,refreshLayoutSwipeRefreshLayoutrecyclerViewRecyclerView,如 xml< 中所示 布局代码。

refreshLayout.setOnChildScrollUpCallback(object : SwipeRefreshLayout.OnChildScrollUpCallback {
  override fun canChildScrollUp(parent: SwipeRefreshLayout, child: View?): Boolean {
    if (recyclerView != null) {
      return recyclerView.canScrollVertically(-1)
    }
    return false
  }
})

虽然 xml 布局是这样的,

 <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>

关于android - 由于滑动刷新布局,我无法向上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55616525/

相关文章:

android - 在滑动刷新布局android中设置进度条的位置

android - 以编程方式滚动到屏幕末尾

textview - 如何在 Android 中设置 NestedScrollView 的最大高度?

android - 嵌套 ScrollView 在 CoordinatorLayout 中占用额外空间

android - 无法通过 adb shell 创建日志文件

android - 如何在框架布局中将按钮显示在彼此下方?

android - 如何处理 UnsatisfiedLinkError?

java - php 响应时出现 JSON 错误

android - 将 SwipeRefreshLayout 与 Viewpager 和 Tablayout 结合使用

android - RecyclerView 和 SwipeRefreshLayout