i use the ViewPager for switching views with left/right swipe.
The ViewPager needs an Adapter, so I've built this one:
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) {}
}
Now, i want to get the current focused view by the viewpager.
I tried getChildAt(x), but it does not work.
Are there some example, or do you have any idea how to get the current view?
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…