0

在我的片段中,我使用包含SwipeRefreshLayout.

当我滑动刷新时,我可以看到两个刷新指示器,其中一个永远不会消失。

看这里

我的层次结构中显然只有一个SwipeRefreshLayout,当我将代码迁移到 View Binding 时出现了这种情况。


这是我的代码和行为的详细信息。

我有一个显示项目列表的片段,我正在使用com.myapp.ItemsListView为此目的构建的自定义视图。

<?xml version="1.0" encoding="utf-8"?>
<com.myapp.ItemsListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/myItems"
    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" />

为了访问这个自定义视图,我使用了视图绑定。在我的片段中,这就是它的样子:

// ItemsFragment.kt
override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    binding = FragmentItemsBinding.inflate(inflater, container, false)
    return binding?.root
}

然后我通过访问此自定义视图中的任何内容binding?.myItems

我的自定义视图使用 aSwipeRefreshLayout作为其根并包含一个 RecyclerView:

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipeToRefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/items"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

        <TextView
            android:id="@+id/missingContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:text="@string/missing_content_message"
            app:drawableTopCompat="@drawable/ic_missing_content_24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

这个问题之后,我正在使用这段代码来扩充自定义视图:

// ItemsListView.kt
class ItemsListView(context: Context, attrs: AttributeSet) : SwipeRefreshLayout(context, attrs) {
  private var b: ItemsListBinding? = null

  init {
    b = ItemsListBinding.inflate(LayoutInflater.from(context), this, true)
  }
}

从代码中,我自定义我SwipeRefreshLayout的外观与默认值不同:

b?.swipeToRefresh?.setSize(LARGE)
b?.swipeToRefresh?.setColorSchemeResources(
  R.color.colorPrimary,
  R.color.colorPrimaryDark
)

我可以b?.swipeToRefresh设置监听器,显示或隐藏进度指示器。

fun setListener(listener: OnRefreshListener) {
  b?.swipeToRefresh?.setOnRefreshListener(listener)
}
fun showProgress() {
  b?.swipeToRefresh?.isRefreshing = true
}
fun hideProgress() {
  b?.swipeToRefresh?.isRefreshing = false
}

但是,正如您在视频剪辑中看到的那样,有一个黑色进度指示器会一直保持并且永远不会消失。我试图访问SwipeRefreshLayoutviab?.root并导致相同的行为。

最后,我只是不明白为什么SwipeRefreshLayout我的屏幕上显示了两个。我在介绍 View Binding 时注意到了这个问题(我之前使用过 Kotlin Synthetics)。我只想膨胀SwipeRefreshLayout层次结构中的一个。

4

0 回答 0