片段内刷卡刷新显示白色片段

时间:2020-08-30 22:18:38

标签: android android-fragments androidx

我试图在包含RecyclerView的片段内添加“刷卡刷新”,但添加后片段变成白色。 尝试使用简单的textview并在片段.setRefreshing(true)中使用,但片段仍为白色。 如果我将“滑动”刷新到活动的布局,则一切工作都像一件魅力。 我的问题是我在做错什么,或者为什么我不能让“滑动”刷新片段中的内容?

我的片段XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeToRefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</layout>

活动XML:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.main.MainActivity">
            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:paddingBottom="100dp">
                <FrameLayout
                    android:id="@+id/screenContainer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.core.widget.NestedScrollView>
        <!--            TODO REFACTOR COLORS -->
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:backgroundTint="#000000"
            app:fabCradleMargin="10dp"
            app:hideOnScroll="true"
            app:menu="@menu/bottom_app_bar"
            app:navigationIcon="@drawable/ic_menu_24dp" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="#e0f2f1"
            app:layout_anchor="@id/bottomAppBar"
            app:shapeAppearance="@style/FabDiamondOverlay"
            app:srcCompat="@drawable/ic_add_24dp" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

等级:

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

仿真器:

enter image description here

在github上上传示例项目。

https://github.com/tirlaovidiu/swipe-to-refresh-bug-example

已解决::

NestedScrollView替换为ConstraitLayout,问题消失了。

1 个答案:

答案 0 :(得分:1)

您可以使用此 在xml片段中:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>

片段:

public class SwipeLayout extends Fragment implements OnRefreshListener {
SwipeRefreshLayout swipeLayout;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.principal, container, false);

    swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorSchemeColors(android.R.color.holo_green_dark, 
            android.R.color.holo_red_dark, 
            android.R.color.holo_blue_dark, 
            android.R.color.holo_orange_dark); (...)

@Override
public void onRefresh() {
    new myTask().execute();     
}

更改您的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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"
    android:layout_height="match_parent">

    
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:fillViewport="true"
            android:overScrollMode="always"
            android:paddingBottom="100dp">
            <FrameLayout
                android:id="@+id/screenContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.core.widget.NestedScrollView>
        <!--            TODO REFACTOR COLORS -->
        <com.google.android.material.bottomappbar.BottomAppBar
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:backgroundTint="#000000"
            app:fabCradleMargin="10dp"
            app:hideOnScroll="true"
            app:menu="@menu/bottom_app_bar"
            app:navigationIcon="@drawable/ic_menu_24dp"
            tools:ignore="BottomAppBar" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="#e0f2f1"
            app:layout_anchor="@id/bottomAppBar"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:shapeAppearance="@style/FabDiamondOverlay"
            app:srcCompat="@drawable/ic_add_24dp" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.screenContainer, SwipeLayout.getInstance());
    transaction.commit();