我使用 ViewPager 通过左/右滑动来切换视图。
ViewPager 需要一个适配器,所以我构建了这个:
public class ListViewPagerAdapter extends PagerAdapter {
protected static final String TAG = "ListViewPagerAdapter";
protected static final int NUM_VIEWS = 3;
protected final Activity mActivity;
public ListViewPagerAdapter(Activity activity) {
mActivity = activity;
}
@Override
public int getCount() {
return NUM_VIEWS;
}
@Override
public void startUpdate(View container) {}
@Override
public Object instantiateItem(View container, int position) {
// ViewPager
ViewPager viewPager = (ViewPager) container;
// Wird verwendet, um die Views aufzurufen
LayoutInflater layoutInflater = mActivity.getLayoutInflater();
// Standardmäßig ist news eingeblendet
View view = layoutInflater.inflate(R.layout.news_fragment, null);
// Falls sich die Position verändert, so verändert sich auch die View
if (position == 0) {
view = layoutInflater.inflate(R.layout.favorite_fragment, null);
} else if (position == 2) {
view = layoutInflater.inflate(R.layout.videos_fragment, null);
}
// View einblenden
viewPager.addView(view, 0);
return view;
}
@Override
public void destroyItem(View container, int position, Object object) {
// ViewPager
ViewPager viewPager = (ViewPager) container;
// View
View view = (View) object;
// View löschen
viewPager.removeView(view);
}
@Override
public void finishUpdate(View container) {}
@Override
public boolean isViewFromObject(View view, Object object) {
View _view = (View) object;
return view == _view;
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {}
}
现在,我想通过 viewpager 获取当前的焦点视图。我尝试了 getChildAt(x),但它不起作用。
有没有一些例子,或者你知道如何获得当前视图?
谢谢