我正在使用 ViewPagerExtension 库https://github.com/astuetz/ViewPagerExtensions处理滚动选项卡视图。我有一个位置列表,其中包含一个事件列表,我想为每个位置表示一个选项卡,并在每个选项卡内显示事件的列表视图。
到目前为止,我已经能够毫无问题地展示这一切。当我在用户单击其中一个事件后尝试恢复正确的位置事件时,就会出现问题。
如果我设置了 OnItemClickListener,我会在列表视图中获得该事件的位置,但我不知道是哪一个,也不知道如何才能恢复它。
下面的代码是我目前所拥有的,我也尝试将 OnPageChangeListener 添加到父 ViewPager,但它似乎不起作用。
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) {}
}
我的问题是,有什么方法可以保持关系位置事件,所以当 OnItemClick 启动时,我知道我现在在哪个选项卡中?
非常感谢您的帮助!
解决方案
我已经能够通过标记每个 ListView 来解决它:
listView.setTag(position);
然后恢复它:
@Override
public void onItemClick(AdapterView<?> parent, View view, int itemPosition,
long id) {
int tabPos = (Integer) parent.getTag();
Event clickedEvent = events.get(itemPosition);
}
我在这里找到了解决方案Android ViewPager get the current View
谢谢您的帮助 :)