I create a ViewPager dynamically :
private View createCarousel(Context context)
{
ViewPager pager = new ViewPager(context);
ArrayList<Drawable> drawables = new ArrayList<>();
String[] carouselImages = mDisplayedGame.getCarouselImages();
if (carouselImages != null) {
for (String s : carouselImages) {
drawables.add(ImageLoader.getInstance().getDrawable(s));
}
pager.setAdapter(new ImageAdapter(context, drawables));
pager.setCurrentItem(0);
}
return pager;
}
Note: carouselImages != null always and ImageLoader works correctly.
Adapter :
public class ImageAdapter extends PagerAdapter
{
Context context;
private ArrayList<Drawable> mDrawables;
public ImageAdapter(Context context, ArrayList<Drawable> drawables, float width)
{
this.context = context;
mDrawables = drawables;
}
#Override
public int getCount()
{
return mDrawables.size();
}
#Override
public float getPageWidth(int position)
{
return 1f;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageDrawable(mDrawables.get(position));
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((ImageView) object);
}
}
And add to container :
mRootView.addView(createCarousel(this), new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
And ViewPager not showed. What i'm doing wrong?
Confuse about this situation but magic happens in this line :
mRootView.addView(createCarousel(this), new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
when i change height to 100 :
mRootView.addView(createCarousel(this), new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100 ));
Content was appeared on screen.
So problem in setting layout params.
In my case i need to display fullscreen images so i just calculate dimens when app start and set manually them to constructor params.
Didn't you forget to set layout params for child ImageViews ? How about try something like:
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageDrawable(mDrawables.get(position));
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
((ViewPager) container).addView(imageView, 0);
return imageView;
Related
I am using viewpager to show images from drawable folder
The images named:
a1
a2
a3...
To a700
So i use this code to get all images in viewpager
mImages=new int[700];
for (int i = 0; i < 700; i++) {
mImages[i] = getResources()
.getIdentifier("a"+i, "drawable", getPackageName()); }
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
It work fine and show all images
But when i use this code there is no first image in viewpager
The first image is appeared after first viewpager swip
Why?
How to start from a1 in viewpager
This is the adapter
private class ImagePagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return mImages.length;
}
public int getImageId(int pos){return mImages[pos];}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = MainActivity.this;
final TouchImageView imageView = new TouchImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
And i use this cose in mainactivity
public class MainActivity extends Activity {
ViewPager viewPager;
public int[] mImages = new int[]{
R.drawable.startpic};
try
mImages=new int[700];
for (int i = 0; i < 700; i++) {
mImages[i] = getResources()
.getIdentifier("a"+ (i + 1), "drawable", getPackageName()); }
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
you do not have an image named a0, so make sure you are not starting at a0. With the logic you have currently, I think you will also be missing a700 at the end (your for loop only goes to when i is 699).
I have a viewpager showing the images, at the moment i have 3 images. It is going like 1->2->3 it stops on 3 but i want it to go to 1. Same as it should work from 1->3.
Here is my code:
public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
How can i set the loop for it. I hope i explained well. Thank-you.
The best way to do this is to use Timer in combination with Runnable. Start by referencing your ViewPager:
imagePager = (ViewPager) view.findViewById (R.id.your_view_pager);
Then you need to set adapter to your imagePager like this (let's suppose you have already instantiated your ImageAdapter):
imagePager.setAdapter(imageAdapter);
Continue:
private Timer swipeTimer = new Timer();
private Handler handler = new Handler();
imagePager.setCurrentItem(0); // this is the beginning
final Runnable changeCurrentImage = new Runnable() {
public void run() {
currentImage++;
if (currentImage >= imageAdapter.getCount()) {
currentImage = 0; // go back to the first image
}
imagePager.setCurrentItem(currentPage, true);
}
};
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(changeCurrentImage);
}
}, 5000, 5000); // here you can set time interval
i implemented a viewpager in my activity that on scroll, loads a set of data to an adapter and display them in a list view. everything is working fine, but i cant seem to find out how to make it more efficient because when scrolling, the view freezes but for example in the GMAIL app when scrolling between Emails the scrolling is so smooth.
Below is the code i am using:
Activity:
in oncreate
pageradapter adapter2 = new pageradapter(this, ArrayOfItems); //ArrayOfItems is an Array containing the data that will be displayed
ViewPager myPager = (ViewPager) findViewById(R.id.mypanelpager);
myPager.setAdapter(adapter2);
pagechangelistener pageListener = new pagechangelistener();
myPager.setOnPageChangeListener(pageListener);
pageradapter:
public pageradapter(Activity act, ArrayList <List<Item>>Arrayitems) {
this.Arrayitems = Arrayitems;
activity = act;
}
public int getCount() {
return Arrayitems.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
List<Item> item = Arrayitems.get(position);
ListView view = new ListView(activity);
taskdetailsarrayadaptertest adapter = new taskdetailsarrayadaptertest(activity, item);
view.setAdapter(adapter);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
((ViewPager) container).addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
pagechangelistener:
#Override
public void onPageSelected(int position)
{
currentPage = position;
int positionTemp = position + 1;
}
public final int getCurrentPage() {
return currentPage;
}
First you need to use holders like this.
in your ViewPager:
class Holder{
ListView listView;
Adapter adapter;
}
private ArrayList<Holder> mHolders;
//init holders
#Override
public Object instantiateItem(ViewGroup collection, int position) {
Holder holder = mHolders.get(position);
holder.listView.setAdapter(holder.adapter);
collection.addView(holder.listView);
return holder.listView;
}
Second try async loading data in listview
set myPager.setOffscreenPageLimit(1);
I am creating an application in which I have multiple images,sounds and texts.I want to show single image on a screen and for this I am using view pager and implementing on Page change Listener interface for keeping track of pages.But in my case images are scrolling as I want but I want to play sound according means each sound is different for every image in my case first 2 sounds are being played simultaneously and the text which is being set is in the array and it is getting second index position.I am unable to get the problem.Any suggestion would be highly appreciated.
Here is my code,
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setOnPageChangeListener(adapter);
viewPager.setAdapter(adapter);
private class ImagePagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
Context context;
MediaPlayer mp;
private int[] mImages = new int[]{R.drawable.airplane,R.drawable.ambulance,R.drawable.apple,
R.drawable.ball_img,R.drawable.bear_img,R.drawable.bird_img,
R.drawable.cake_img,R.drawable.cat,R.drawable.car,R.drawable.clock,
R.drawable.drum_img,R.drawable.dinasour,R.drawable.dog,R.drawable.dolphine,R.drawable.duck,
R.drawable.eagle,R.drawable.elephant,
R.drawable.fish,R.drawable.frog,
R.drawable.horse,
R.drawable.insect,
R.drawable.jagular,
R.drawable.kangroo ,
R.drawable.lion,
R.drawable.monkey,
R.drawable.nose,
R.drawable.owl ,
R.drawable.peacock ,
R.drawable.quill,
R.drawable.rabit ,
R.drawable.star ,
R.drawable.tiger,
R.drawable.umbrella ,
R.drawable.vulture,
R.drawable.wolf ,
R.drawable.xylophone ,
R.drawable.yoyo ,
R.drawable.zebra
};
private int [] soundIds = {R.raw.airplane,R.raw.ambulance,R.raw.airplane,R.raw.ambulance,R.raw.airplane,R.raw.ambulance,R.raw.airplane,R.raw.ambulance};
String description [] = {"Airplane","Ambulance","Apple","Ball","Bear","Bird","Cake","Cat","Car","Clock"
,"Drum","Dinasour","Dog","Dolphine","Duck","Eagle","Elephant","Fish","Frog","Horse","Insect"
,"Jagular","Kangroo","Lion","Monkey","Nose","Owl","Peacock","Quill","Rabbit","Star","Tiger"
,"Umbrella","Vulture","Wolf","Xylophone","Yoyo","Zebra"};
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container,int position) {
context = ZoomedLetters.this;
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mp = MediaPlayer.create(context,soundIds[position]);
mp.start();
descriptions.setText(description[position]);
ImageView imageView = new ImageView(context);
int padding = context.getResources().
getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
descriptions.setText(description[position]);
ImageView imageView = new ImageView(context);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;*/
}
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
i have jSON array content image url and caption , the list content over 100 object
this is example code :
[{"url":"http:\/\/web.com\/image\/1.jpg","cap":"image 1"},{"url":"http:\/\/web.com\/image\/2.jpg","cap":"image 2"},{"url":"http:\/\/web.com\/image\/3.jpg","cap":"image 3"}]
what i am looking for is this :
sure its must content left right pagination swipe picture and caption
is there any asapter can do that ? can any one lead me what i have to use pager or fragment or any thing else ?
please guys any advice ?
you can use this AndroidViewPagerGallery
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
}
}
and this adapter you can customize it to create layout and change the picture and captions
public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}