2

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.

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;
}

@Override
public int getCount() {
    return this.locations.size();
}

@Override
public 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;
}

@Override
public void destroyItem(View container, int position, Object view) {
    //((ViewPager) container).removeView((View) view);
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((View) object);
}

@Override
public void finishUpdate(View container) {}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {}

@Override
public Parcelable saveState() {
    return null;
}

@Override
public void startUpdate(View container) {}

}

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!

SOLUTION

I have been able to solve it by tagging each ListView:

listView.setTag(position);

and then recovering it:

@Override
        public void onItemClick(AdapterView<?> parent, View view, int itemPosition,
                long id) {

            int tabPos = (Integer) parent.getTag();
            Event clickedEvent = events.get(itemPosition);

        }

I found the solution here Android ViewPager get the current View

Thank you for your help :)

Community
  • 1
  • 1
albertfdp
  • 425
  • 4
  • 11

0 Answers0