Scrolling images (without layouts) using viewpager

What my code does:

Here is my code, which is for the viewpager that looks between the xml layouts (named left.xml, right.xml and center.xml).

What i want to do

I want to navigate between images (stored in a folder with transfer option). When I replace R.layout.xml with R.drawable.image, my application crashes. Can someone help me figure this out?

public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyPagerAdapter adapter = new MyPagerAdapter(); ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager); myPager.setAdapter(adapter); myPager.setCurrentItem(0); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } class MyPagerAdapter extends PagerAdapter { public int getCount() { return 3; } public Object instantiateItem(View collection, int position) { LayoutInflater inflater = (LayoutInflater) collection.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); int resId = 0; switch (position) { case 0: resId = R.layout.left; break; case 1: resId = R.layout.center; break; case 2: resId = R.layout.right; break; } View view = inflater.inflate(resId, null); ((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; } } 

}

+3
android android-fragments android-viewpager
Jul 24 2018-12-24T00:
source share
2 answers

If you use the same implementation and just add R.drawable.XXX instead of R.layout.YYY then the problem is there. You use LayoutInflater to inflate ImageView s, an inflatable layout, as it inflates the entire layout in one View .

Instead, try creating ImageView objects through code, and then return the newly created ImageView to return . Some example code would be:

 public Object instantiateItem(View collection, int position) { ImageView img = new ImageView(context); //this is a variable that stores the context of the activity //set properties for the image like width, height, gravity etc... int resId = 0; switch (position) { case 0: resId = R.drawable.img1; break; case 1: resId = R.drawable.img2; break; case 2: resId = R.drawable.img3; break; } img.setImageResource(resId); //setting the source of the image return img; } 

If you use only a certain number of images or pages, you should add them to the xml that contains the ViewPager , and not create the ViewPager .

+5
Jul 24 2018-12-24T00:
source share

I think one option would be to insert each image into its own layout using an ImageView, and then inflate the layout that contains the image, not the image.

0
Jul 24 2018-12-12T00:
source share



All Articles