-1

I am making a page listener, I've seen a view people do this:

ViewPager.addOnPageChangeListener(this);

What does that mean? What does that code even do, seems like nothing? I thought "this" meant the current Activity or context but I feel like the above code is not doing anything.

Android Dev
  • 305
  • 3
  • 18
  • 3
    The current instance of the class you're in. In this context, the class you're in should be implementing the OnPageChangeListener interface. – Ryan Jan 31 '22 at 22:26
  • Does this answer your question? [What is the meaning of "this" in Java?](https://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java) – cmak Jan 31 '22 at 23:10

1 Answers1

1

It means it is referring to a onPageChangeListner object which u need to create sepearately. So you will have to create a OnPageChangeListener like this:

ViewPager.OnPageChangeListener()  pageChangeListener = new ViewPager.OnPageChangeListener() {}

And then use it like:

ViewPager.addOnPageChangeListener(this)

So basically this means the reference of the onPageChangeListener object

YourHelper
  • 703
  • 7
  • 17
Ruchi
  • 71
  • 3