Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager OnItemClickListener

I'm new to Viewpager, and after reading http://developer.android.com/reference/android/support/v4/view/ViewPager.html from google, I can't seem to find anything related on viewPager.setOnItemClickListener(new OnItemClickListener() { Do we have other options to act as Item Clicked?

Testing out

viewPager.setOnItemClickListener(new OnItemClickListener() {             @Override             public void onItemClick(AdapterView<?> parent, View view,                     int position, long id) {                 Intent i = new Intent(MainActivity.this, SingleItemView.class);                 i.putExtra("flag", flag);                 i.putExtra("position", position);                 startActivity(i);             }          }); 

Error : The method setOnItemClickListener(new AdapterView.OnItemClickListener(){}) is undefined for the type ViewPager

like image 408
KC Chai Avatar asked May 03 '13 03:05

KC Chai


People also ask

What is ViewPager used for?

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

Is ViewPager deprecated?

This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated.

How do I get ViewPager?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


1 Answers

There is no OnItemClick callback method for ViewPager. If you want click events on each page then you'll have to build your listener into the page content within your Adapter.

something like this:

@Override public Object instantiateItem(View collection, final int pos) { //have to make final so we can see it inside of onClick()     LayoutInflater inflater = (LayoutInflater) collection.getContext()             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);       View page = inflater.inflate(R.layout.YOUR_PAGE, null);      page.setOnClickListener(new OnClickListener(){         public void onClick(View v){             //this will log the page number that was click             Log.i("TAG", "This page was clicked: " + pos);         }     });       ((ViewPager) collection).addView(page, 0);     return page; } 

exactly what you need will depend a bit on what else you are doing inside of instantiateItem() which you haven't posted so I can't give you a more specific answer.

like image 98
FoamyGuy Avatar answered Oct 09 '22 03:10

FoamyGuy