简体   繁体   中英

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

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 .

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 !

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. They are very effective. If that is not possible make sure images are at least .png format instead of .JPG

More efficient way Use horizontal recycler view.

Here is the tutorial: https://demonuts.com/android-horizontal-recyclerview/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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