私のフラグメントでは、を含むカスタム ビューを使用してアイテムのリストを表示していますSwipeRefreshLayout
。
スワイプして更新すると、2 つの更新インジケーターが表示され、そのうちの 1 つが消えることはありません。
私の階層には明らかに 1 つしかありません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" />
このカスタム ビューにアクセスするために、View Binding を使用しています。私のフラグメントでは、次のようになります。
// ItemsFragment.kt
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentItemsBinding.inflate(inflater, container, false)
return binding?.root
}
を介して、このカスタム ビュー内のあらゆるものにアクセスしますbinding?.myItems
。
そして、私のカスタム ビューはSwipeRefreshLayout
ルートとして a を使用し、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
}
ただし、ビデオ クリップでわかるように、黒の進行状況インジケーターが表示され、消えることはありません。SwipeRefreshLayout
ビアにアクセスしようとしb?.root
ましたが、同じ動作になります。
SwipeRefreshLayout
結局、画面に2 つ表示される理由がわかりません。View Binding を導入したときにこの問題に気付きました (以前は Kotlin Synthetics を使用していました)。SwipeRefreshLayout
階層内の1 つだけを膨らませたいと思います。