Recycler视图优化

问题描述 投票:0回答:1

我正在开发一个应用程序,在 viewpager 中有 2 个片段。在第二个片段中,我使用 recyclerview 将图像加载到其中。我面临的问题是关于 UI 阻塞。当我从 Android Studio 启动应用程序,然后选择 viewpager 选项卡 2 时。它会立即显示数据,而无需花费时间,您可以说最多 4 到 5 毫秒。但是,当我自己杀死应用程序然后打开应用程序并选择 viewpager 选项卡 2 时,它会保留/阻止 UI,直到绑定所有视图。

我正在使用 Glide 来显示图像。我尝试了很多修改,但没有得到更好的结果。这是我的代码。

class MyVideoAdp(
    private val context: Context,
    val callback: (Int) -> Unit
) :
    RecyclerView.Adapter<MyVideoAdp.MyVideoVH>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVideoVH {
        val binding = VideosItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return MyVideoVH(binding)
    }

    override fun getItemCount(): Int {
        return mDiffer.currentList.size
    }

    override fun onBindViewHolder(holder: MyVideoVH, position: Int) {
        val item = mDiffer.currentList[position]
        holder.bind(item)
    }


    inner class MyVideoVH(private val binding: VideosItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: VideoModel) {

            GlideApp.with(context)
                .load(item.filePath)
                .encodeFormat(Bitmap.CompressFormat.JPEG)
                .encodeQuality(10)
                .thumbnail(0.1f)  // Reduce thumbnail size to load faster
                .override(binding.videoThumbnail.width / 2, binding.videoThumbnail.height / 2)  // Load a smaller size
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)  // Cache the final image
                .placeholder(drawable.placeholder_ic)
                .dontAnimate()  // Disable animations to make loading faster
                .into(binding.videoThumbnail)


            binding.videoTitle.text = item.fileName // Use the correct property for video title
            binding.videoDuration.text =
                Utils.formatDuration(item.duration) // Helper method to format duration
            binding.videoTimeStamp.text =
                item.creationDate // Use a date formatter if needed

            Log.d(TAG, "onViewCreated: 5 ${System.currentTimeMillis()}")
        }
    }
    object DiffUtilCallback : DiffUtil.ItemCallback<VideoModel>() {
        override fun areItemsTheSame(oldItem: VideoModel, newItem: VideoModel): Boolean {
            return oldItem.id == newItem.id
        }
        override fun areContentsTheSame(oldItem: VideoModel, newItem: VideoModel): Boolean {
            return oldItem == newItem
        }
    }

    private val mDiffer = AsyncListDiffer(this, DiffUtilCallback)

    fun submitList(list: MutableList<VideoModel>) {
        mDiffer.submitList(list)
    }

}
@AndroidEntryPoint
class MyVideosFragment : Fragment() {
    private var _binding: FragmentMyVideosBinding? = null
    private val binding get() = _binding!!
    private val viewModel by activityViewModels<MainVM>()

    private var adp: MyVideoAdp? = null

    @Inject
    lateinit var glide: RequestManager

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        _binding = FragmentMyVideosBinding.inflate(layoutInflater, container, false)
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        lifecycleScope.launch {

            setAdp()
            setObserver()
        }

    }

    private fun setObserver() {
        Log.d(TAG, "onViewCreated: 3 ${System.currentTimeMillis()}")
        viewModel.getAllVideoFiles().observe(viewLifecycleOwner) {
            Log.d(TAG, "onViewCreated: 4 ${System.currentTimeMillis()}")
            adp?.submitList(it.toMutableList())
        }
    }

    private fun setAdp() {
        Log.d(TAG, "onViewCreated: 1 ${System.currentTimeMillis()}")
        adp = MyVideoAdp(requireContext()) {

        }
        binding.myVideosRecycler.setItemAnimator(DefaultItemAnimator())
        binding.myVideosRecycler.adapter = adp
        Log.d(TAG, "onViewCreated: 2 ${System.currentTimeMillis()}")
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }


}
android performance android-recyclerview android-glide user-experience
1个回答
0
投票

你能展示一下 viewModel.getAllVideoFiles() 中发生了什么吗?

© www.soinside.com 2019 - 2024. All rights reserved.