当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to add a Fragment to a ViewPager?

问题描述:

I've a SplashScreen, which is a ViewPager and I need to add the Login Fragment to it on tap.

This is my PagerAdapter:

public class SplashSAdapter extends PagerAdapter {

Context ssContext;

Activity _activity;

private int[] ssImages = new int[] { R.drawable.splashscreen1,R.drawable.splashscreen2, R.drawable.splashscreen3 };

public SplashSAdapter(Context ssContext, Activity activity) {

this.ssContext = ssContext;

this._activity = activity;

}

@Override

public int getCount() {

return ssImages.length;

}

@Override

public boolean isViewFromObject(View ssView, Object ssObject) {

return ssView == ((ImageView) ssObject);

}

@Override

public Object instantiateItem(ViewGroup ssContainer, int ssPosition) {

ImageView ssImageView = new ImageView(ssContext);

ssImageView.setScaleType(ImageView.ScaleType.FIT_XY);

ssImageView.setImageResource(ssImages[ssPosition]);

((ViewPager) ssContainer).addView(ssImageView, 0);

ssImageView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

onClickSplashScreen();

}

});

return ssImageView;

}

@Override

public void destroyItem(ViewGroup ssContainer, int ssPosition,

Object ssObject) {

((ViewPager) ssContainer).removeView((ImageView) ssObject);

}

public void onClickSplashScreen(){

}

So, onClickSplashScreen, how do I add a Fragment there?

网友答案:
FragmentManager fragmentManager = this._activity.getSupportFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

will this help you?

网友答案:

You should user FragmentPagerAdapter instead of PagerAdapter,

Examples can be found here http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

分享给朋友:
您可能感兴趣的文章:
随机阅读: