简体   繁体   English

在一些Android设备中,ViewPage刷卡很难

[英]ViewPage swipe is hard in some android devices

I Have a view pager for image slider in my activity I have a problem when I try to swipe to another page in some android device like xiaomim, note 8 devices, the movement of the viewPager became heavy 我在我的活动中有一个用于图像滑块的视图寻呼机我有一个问题,当我尝试在xiaomim等一些Android设备中刷到另一个页面时,注意8个设备,viewPager的运动变得沉重

this is my code : 这是我的代码:

override fun isViewFromObject(view: View, p1: Any): Boolean {
    return view == p1
}


//
override fun instantiateItem(container: ViewGroup, position: Int): Any {
    val itemView: View = LayoutInflater.from(container.context).inflate(R.layout.image_slider_view, container, false)
    val mSliderImage: ImageView = itemView.findViewById(R.id.slider_image)
    Glide.with(itemView).load(images[position]).into(mSliderImage)


    itemView.setOnClickListener {
        if (clickable) {
            val intent = Intent(itemView.context, ImageSliderActivity::class.java)
            intent.putExtra("slider", images)
            intent.putExtra("position", position)
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                val activityOptions = ActivityOptions.makeSceneTransitionAnimation(activity)
                itemView.context.startActivity(intent, activityOptions.toBundle())
            } else {
                itemView.context.startActivity(intent)
            }
        }
    }
    container.addView(itemView)
    Util.rotateViewsIfRTL(itemView)
    return itemView
}


override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
    (container as ViewPager).removeView(`object` as View)
}

override fun getCount(): Int {
    return images.count()
}

i don't know how many image you have , and what their sizes , i once faced this problem with image in viewpager . 我不知道你有多少图像,以及它们的大小,我曾经在viewpager遇到过这个问题。

In my case I have more then 10 images and each image have weight more 800kb, and I check this issue, and it really works fine if i change OffscreenPageLimit to 1 page and i tried to reduce the size of the image ! 在我的情况下,我有超过10个图像,每个图像的重量超过800kb,我检查这个问题,它真的很好,如果我将OffscreenPageLimit更改为1页,我试图减少图像的大小!

viewPager.setOffscreenPageLimit(1);

Try the following, 试试以下,

First instead of images, load any random string or integer and test if it still scrolls heavily. 首先是代替图像,加载任何随机字符串或整数,并测试它是否仍然大量滚动。 If it works fine then it means the images are heavy to load. 如果它工作正常则意味着图像加载很重。

If images are heavy, check the following: 如果图像很重,请检查以下内容:

  1. Try to WEBP format for images and load them. 尝试WEBP格式的图像并加载它们。 They are very effective. 它们非常有效。 If that is not possible make sure images are at least .png format instead of .JPG 如果不可能,请确保图像至少为.png格式而不是.JPG

More efficient way Use horizontal recycler view. 更有效的方式使用水平回收站视图。

Here is the tutorial: https://demonuts.com/android-horizontal-recyclerview/ 这是教程: https//demonuts.com/android-horizo​​ntal-recyclerview/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM