Disable ViewPager Swipe (DVPS)


DVPS:   SOLD       




Hi Dev, the story is I'm developing an application home page that has 2 tabs, and at the request of the client, so I only need to display one tab, without having to delete the other tab. So, how do you do it? I tried this piece of code but it didn't work, actually this is implemented in Java, but it can also be done in Kotlin, the problem is it doesn't work, maybe my case is different from the case experienced by the answerer.

viewPager.setPagingEnabled(false)

Okay, it doesn't mean there is no solution for my case, of course there is, namely by making my own ViewPager widget, how do I do it? see my summary below.

Actually there is no built-in way from the official Android viewPager to disable swiping between viewPager pages, so what is possible to do is to create your own ViewPager extension/widget by creating a class, then extending ViewPager, then we override its members. The members that we will get are onTouchEven and onInterceptTouchEvent, both of which we can use to prevent swipe actions, here is the complete code.

class SwipeLockableViewPager(context: Context, attrs: AttributeSet): ViewPager(context, attrs) {
    private var swipeEnabled = false

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return when (swipeEnabled) {
            true -> super.onTouchEvent(event)
            false -> false
        }
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        return when (swipeEnabled) {
            true -> super.onInterceptTouchEvent(event)
            false -> false
        }
    }

    fun setSwipePagingEnabled(swipeEnabled: Boolean) {
        this.swipeEnabled = swipeEnabled
    }
}

Create a new class and fill it with the code above, then replace the default Android viewPager with your viewPager class, the method is as below.

<mypackage.SwipeLockableViewPager
        android:id="@+id/myViewPager"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

Now in a particular activity you can disable/enable swipe viewPager at will and at any time by calling its function.

myViewPager.setSwipePagingEnabled(false)

Okay, that's all, hope it's useful and good luck!

Comments

abahe say:
thank you thank you,  btw is there anything about endless scroll or lazy load in kotlin?

answer:

Yes, bro, usually we use lateinit or lazy.

Object creation is a heavy process due to the initialization of all public and private properties defined in the class when the constructor is called.

Kotlin has several ways to initialize properties only when needed. Let's try to understand some basic differences between the two.

Lateinit Property
Lateinit properties are properties  var that can be initialized later when first used, usually declared in the constructor or in any function according to its use.
data class User (val id : Long,
                 val username : String) : Serializable

lateinit var lateinitUser : User
Lazy Property
Lazy properties are properties  val that can also be initialized later when called for the first time.
val lazyUser : User? by lazy {
        User(id = 1, username = "agrawalsuneet")
    }

Post a Comment

Previous Next

نموذج الاتصال