Swipe image with view adaptor class

307 views Asked by At

I try to swipe the images along with its text and tts. I have used ViewPageAdapter. Code is

public class ViewPagerAdapter extends PagerAdapter{

Activity activity;
int imageArray[];
String[] arrToDisplay;

public ViewPagerAdapter(Activity act, int[] imgArra, String[] arrayObject) {
    imageArray = imgArra;
    activity = act;
    arrToDisplay = arrayObject;

  }

public int getCount() {
    return imageArray.length;
}



public Object instantiateItem(View collection, int position) {
    ImageView view = new ImageView(activity);

    view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    view.setScaleType(ScaleType.FIT_XY);
    view.setBackgroundResource(imageArray[position]);
    ((ViewPager) collection).addView(view, 0);
    return view;
  }


 @Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}


}

xml:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:orientation="vertical" >

    <!--ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/a_elephant" /-->

    <android.support.v4.view.ViewPager
        android:id="@+id/myfivepanelpager"
        android:layout_width="match_parent"
        android:layout_height="494dp" />

</LinearLayout>

Main.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

           ViewPagerAdapter adapter = new ViewPagerAdapter(this, arrAnimals1, arrAnimals);
    ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);


}

The above code swiping images correctly. Problem is i want to show the text as well along with tts. i have tried this code for displaying the text in adaptor class but it does'nt work:

public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService
             (Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.animals, null);

    ImageView im=(ImageView) layout.findViewById(R.id.imageView1);
    im.setBackgroundResource(imageArray[position]);
    TextView dsc=(TextView) layout.findViewById(R.id.txtObjName);
    dsc.setText("asdfasdf");
    ((ViewPager) collection).addView(layout, 0);
    return layout; 
}

Please help.

Thanks, itin

1

There are 1 answers

0
Emmanuel On

I would suggest you use either FragmentStatePagerAdapter or FragmentPagerAdapter. These adapters have a getItem() that returns a Fragment; you can create an xml for the Fragment that contains an ImageView and a TextView