Obtaining current tab of ViewPager in Android

WiRED

New Member
I am working with a scrolling tabs view using the ViewPagerExtension library https://github.com/astuetz/ViewPagerExtensions. I have a list of locations, which contain a list of events, and I want to represent a tab for each location and within each tab, show a listview of the events. So far, I have been able to show all this with no problem. The problems come when I try to recover the correct location-event after the user clicks one of the events.If I set a OnItemClickListener, I get the position of that event within a listview, but I don't know which one, and I don't know how I would be able to recover it.The code below is what I have so far, and I also have tried to add an OnPageChangeListener to the parent ViewPager, but it does not seem to work.\[code\]public class CustomPageAdapter extends PagerAdapter {protected transient Activity mContext;private ArrayList<Location> locations;private EventAdapter eventAdapter;private ArrayList<Event> events;public CustomPageAdapter(Activity context, ArrayList<Location> locations) { this.mContext = context; this.locations = locations;}@Overridepublic int getCount() { return this.locations.size();}@Overridepublic Object instantiateItem(View container, int position) { ListView listView = new ListView (mContext); events = this.locations.get(position).getEvents(); eventAdapter = new EventAdapter(mContext, R.layout.event_item, events); listView.setAdapter(eventAdapter); listView.setPadding(10, 10, 10, 10); listView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.textured_paper)); listView.setDivider(mContext.getResources().getDrawable(R.drawable.textured_paper)); listView.setDividerHeight(30); // always after setDivider() listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int itemPosition, long id) { Event clickedEvent = events.get(itemPosition); } }); ((ViewPager) container).addView(listView, 0); return listView;}@Overridepublic void destroyItem(View container, int position, Object view) { //((ViewPager) container).removeView((View) view);}@Overridepublic boolean isViewFromObject(View view, Object object) { return view == ((View) object);}@Overridepublic void finishUpdate(View container) {}@Overridepublic void restoreState(Parcelable state, ClassLoader loader) {}@Overridepublic Parcelable saveState() { return null;}@Overridepublic void startUpdate(View container) {}\[/code\]}My question is, is there any way that I can keep the relation location-event, so when the OnItemClick is launched, I know in which tab I am right now?Thank you very much for your help!SOLUTIONI have been able to solve it by tagging each ListView:\[code\]listView.setTag(position);\[/code\]and then recovering it:\[code\]@Override public void onItemClick(AdapterView<?> parent, View view, int itemPosition, long id) { int tabPos = (Integer) parent.getTag(); Event clickedEvent = events.get(itemPosition); }\[/code\]I found the solution here Android ViewPager get the current ViewThank you for your help :)
 
Back
Top