And*_*rew 6 android kotlin android-recyclerview android-paging android-paging-3
我目前的问题是,LoadStateAdapter
显示加载和错误状态的 my 不在 my 内居中recyclerview
,它有一个 gridlayout 作为布局管理器。我在官方 android 开发者网站上没有找到任何关于此的信息,所以我在这里问:如何将我的LoadStateAdapter
内部集中在我的 Recyclerview 中?
@AndroidEntryPoint
class ShopFragment : Fragment(R.layout.fragment_shop), ShopAdapter.OnItemClickListener {
private val shopViewModel: ShopViewModel by viewModels()
private val shopBinding: FragmentShopBinding by viewBinding()
@Inject lateinit var shopListAdapter: ShopAdapter
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
bindObjects()
}
private fun bindObjects() {
shopBinding.adapter = shopListAdapter.withLoadStateFooter(ShopLoadAdapter(shopListAdapter::retry))
shopListAdapter.clickHandler(this)
}
override fun onDestroyView() {
requireView().findViewById<RecyclerView>(R.id.rv_shop).adapter = null
super.onDestroyView()
}
}
Run Code Online (Sandbox Code Playgroud)
@FragmentScoped
class ShopLoadAdapter(private val retry: () -> Unit): LoadStateAdapter<ShopLoadAdapter.ShopLoadStateViewHolder>() {
inner class ShopLoadStateViewHolder(private val binding: ShopLoadStateFooterBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(loadState: LoadState) {
with(binding) {
shopLoadPb.isVisible = loadState is LoadState.Loading
shopLoadMbtnRetry.isVisible = loadState is LoadState.Error
shopLoadTvError.isVisible = loadState is LoadState.Error
}
}
}
override fun onBindViewHolder(holder: ShopLoadStateViewHolder, loadState: LoadState) = holder.bind(loadState)
override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): ShopLoadStateViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ShopLoadStateFooterBinding.inflate(layoutInflater, parent, false)
return ShopLoadStateViewHolder(binding).also {
binding.shopLoadMbtnRetry.setOnClickListener { retry.invoke() }
}
}
}
Run Code Online (Sandbox Code Playgroud)
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_shop"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/headline"
app:recyclerview_adapter="@{adapter}"
tools:listitem="@layout/shop_list_item"/>
Run Code Online (Sandbox Code Playgroud)
小智 11
如果您使用“withLoadStateFooter”,请尝试此代码。来源
val footerAdapter = MainLoadStateAdapter(adapter)
recyclerView.adapter = adapter.withLoadStateFooter(footer = footerAdapter)
gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (position == adapter.itemCount && footerAdapter.itemCount > 0) {
2
} else {
1
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先,您必须在PagingDataAdapter中创建多种视图类型 ,然后重写getItemViewType方法,如下所示
// Define Loading ViewType
public static final int LOADING_ITEM = 0;
// Define Movie ViewType
public static final int MOVIE_ITEM = 1;
@Override
public int getItemViewType(int position) {
// set ViewType
return position == getItemCount() ? MOVIE_ITEM : LOADING_ITEM;
}
Run Code Online (Sandbox Code Playgroud)
然后动态设置跨度大小
// set Grid span
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
// If progress will be shown then span size will be 1 otherwise it will be 2
return moviesAdapter.getItemViewType(position) == MoviesAdapter.LOADING_ITEM ? 1 : 2;
}
});
Run Code Online (Sandbox Code Playgroud)
您可以查看此paging 3 示例,其中包括在中心显示加载状态视图
归档时间: |
|
查看次数: |
678 次 |
最近记录: |