最近,当我关闭
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
}
像这样创建适配器的submitList()函数:
fun submitList(productScrollModelList: List<HorizontalProductScrollModel>){
this.productScrollModelList.clear()
this.productScrollModelList.addAll(productScrollModelList)
notifyDataSetChanged()
}
在适配器中提交新列表之前清除列表。