2

I try to implement Fragment into ViewPager to make my app more flexible. My app contains TextView and I have a few phrases that will appear one by one if you swap the page from left to right. For now it works in this way like I posted but I want to add a button or maybe some more function, that's why I want to make it like constructor from Fragments.. I try to change my code watching on this post but can't totally understand how to do it. I created two layouts: one for text view another one to put there fragment. Could any one show me how to do it clear?

public class SwipeAdapter extends PagerAdapter{

private int[] car = {R.string.car1, R.string.car2,
        R.string.car3, R.string.car4, R.string.car5};
private Context context;
private LayoutInflater layoutInflater;

public SwipeAdapter(Context context){
    this.context = context;
}

@Override
public int getCount() {
    return car.length;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = layoutInflater.inflate(R.layout.carSwipe, container, false);

     TextView textView = (TextView) itemView.findViewById(R.id.interTextView);
    textView.setText(car[position]);
    container.addView(itemView);
    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((RelativeLayout)object);
}
Community
  • 1
  • 1
JohnPix
  • 1,595
  • 21
  • 44
  • Are you trying to have a ViewPager in a Fragment or have the pages themselves be fragments, or perhaps both? – OneCricketeer Nov 21 '15 at 17:17
  • I have a TextView and I have a few phrases that will appear one by one if you swap the page from left to right. For now it works in this way like I posted but I want to add a button or maybe some more function, that's why I want to make it like constructor from Fragments. – JohnPix Nov 21 '15 at 18:08

1 Answers1

0

You don't need ViewPager with Fragments to Achieve of showing a TextView.

You can try:

  1. In your activity layout xml that you set in content View add a ViewPager with width and height of your preference.
  2. Then you can set the adapter that you have posted above for that ViewPager and it will work.

Alternatively if you still need to inflate Fragments then you need to extend FragmentStateAdapter of FragmentAdapter. That will give you the ability to have in the ViewPager Fragment,

z3n105
  • 1,935
  • 1
  • 15
  • 14