最近我注意到我的应用程序中有一个问题,当我关闭viewAllfragment
并再次重新打开它时,我看到列表一次又一次地重复,每次都有相同的数据,我试图在片段被销毁时清除列表,并仅在适配器为空时提交数据,但所有这些都没有解决这个问题
这就是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"
tools:context=".ui.view_all.ViewAllFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="@color/lightGray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightGray"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:visibility="gone"
tools:visibility="visible"
android:numColumns="auto_fit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这个GridProductAdapter代码
class GridProductAdapter : BaseAdapter() {
private var productScrollModelList = arrayListOf<HorizontalProductScrollModel>()
// init {
//
// }
fun submitList(productScrollModelList: List<HorizontalProductScrollModel>){
this.productScrollModelList.addAll(productScrollModelList)
notifyDataSetChanged()
Log.d(TAG, "submitList: size ${productScrollModelList.size}")
}
fun clearList(){
this.productScrollModelList.clear()
notifyDataSetChanged()
}
override fun getCount(): Int {
return productScrollModelList.size
}
override fun getItem(i: Int): Any? {
return null
}
override fun getItemId(i: Int): Long {
return 0
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var localConvertView: View? = convertView
val binding: HorizontalScrollItemLayoutBinding
if (localConvertView == null) {
binding = HorizontalScrollItemLayoutBinding.inflate(
LayoutInflater.from(parent.context)
)
localConvertView = binding.root
localConvertView.setTag(R.id.viewBinding, binding)
} else {
binding = localConvertView.getTag(R.id.viewBinding) as HorizontalScrollItemLayoutBinding
}
// binding.hsProductImage.setImageResource(productScrollModelList.get(position).getProductImage());
Glide.with(binding.root.context)
.load(productScrollModelList[position].productImage)
.placeholder(R.drawable.placeholder_image)
.into(binding.hsProductImage)
binding.hsProductName.text = productScrollModelList[position].productName
binding.hsProductDescription.text = productScrollModelList[position].productDescription
binding.hsProductPrice.text = "Rs.${productScrollModelList[position].productPrice}/-"
// binding.getRoot().setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
//
// }
// });
return localConvertView
}
}
我在viewAllfragment中使用它,如下所示
private var _binding: FragmentViewAllBinding? = null
private val binding get() = _binding!!
private val LAYOUT_CODE = 0
private val wishList= arrayListOf<WishListModel>()
private val wishlistAdapter = WishlistAdapter(false)
private val args by navArgs<ViewAllFragmentArgs>()
private var layoutCode:Int =0
private val gridProductAdapter by lazy { GridProductAdapter() }
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentViewAllBinding.inflate(inflater)
layoutCode = args.layoutCode
(requireActivity() as MainActivity).fragmentTitleAndActionBar(args.title)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if(layoutCode == 0) {
wishList.addAll(args.viewAllProductList!!)
wishlistAdapter.asyncListDiffer.submitList(wishList)
// wishlistAdapter.submitList(wishList)
binding.recyclerView.apply {
visibility = View.VISIBLE
layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
adapter = wishlistAdapter
}
}else if(layoutCode == 1) {
//================ Horizontal list ======================//
binding.gridView.visibility = View.VISIBLE
binding.gridView.adapter = gridProductAdapter
Log.d(TAG, "onViewCreated: horizontalProductScrollModelList size ${args.horizontalProductScrollModelList.size}")
if(gridProductAdapter.isEmpty) {
gridProductAdapter.submitList(args.horizontalProductScrollModelList.toList())
}
}
}
override fun onDestroyView() {
super.onDestroyView()
gridProductAdapter.clearList()
_binding = null
}
3条答案
按热度按时间9nvpjoqh1#
像这样创建适配器的submitList()函数:
在适配器中提交新列表前,清除列表。
7gcisfzg2#
您还可以通过重新初始化arraylist来使用新数据更新dataClerview。
eoxn13cs3#
我通过在打开
viewAllfragment
之前清除从HomeFragment获得的args.horizontalProductScrollModelList
来修复此问题