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
Related
I have 5 SlidingTabs. I want to place ViewPager animation in one of them.
Fragment class:
public class AdFragment extends Fragment {
int[] mResources = {
R.drawable.six,
R.drawable.five,
R.drawable.four,
R.drawable.eight,
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.seven,
R.drawable.nine,
R.drawable.ten,
};
int currentPage;
public AdFragment() {
// Required empty public constructor
}
Timer swipeTimer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_ad, container, false);
CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(getActivity(),mResources);
final ViewPager mViewPager = (ViewPager) view.findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == mResources.length) {
currentPage = 0;
}
mViewPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 500, 3000);
return view;
}
}
Adapter class:
class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
int[] mResources;
private Resources resource;
public CustomPagerAdapter(Context context,int[] mResources) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mResources = mResources;
}
#Override
public int getCount() {
return mResources.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
#Override
public Object instantiateItem(View view, int position) {
ImageView myView = new ImageView(mContext);
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), mResources[position]);
myView.setImageBitmap(bitmap);
((ViewPager) view).addView(myView);
return myView;
}
}
At the moment I have 10 images in slideshow.When I open my Tab with Fragment described above, wait for some period, then try to go another Tab and back again to this - I get:
01-27 14:13:53.794 12652-12652/igor.com.totalsoulbeta E/AndroidRuntime: FATAL EXCEPTION: main
Process: igor.com.totalsoulbeta, PID: 12652
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2113)
at android.content.res.Resources.getDrawable(Resources.java:700)
at android.content.res.XResources.getDrawable(XResources.java:521)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:175)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:168)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:51)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
at igor.com.totalsoulbeta.CustomPagerAdapter.instantiateItem(CustomPagerAdapter.java:46)
The size of each image is not more that 150kb. My question is: Is it possible to perfome such animation with 25 images without OutOfMemoryError? If yes, what I need to change in my code? How I can clear heap and where I need to do that?
If it's no the best way of animating, please, advise me your variant
Thanks in advance!
A ViewPager should be able to show any number of pages. At any time it only has 3 pages in memory (unless you change the offscreen page limit). The probleme may lie with the Timer leaking something. The documentation says :
When a timer is no longer needed, users should call cancel(), which
releases the timer's thread and other resources. Timers not explicitly
cancelled may hold resources indefinitely.
Does it help if you add something like that in the Fragment ? :
#Override
public void onDestroyView() {
super.onDestroyView();
swipeTimer.cancel();
swipeTimer = null;
}
I created a "Slide Image" in first activity; which has five images. After I slide the last image(fifth), it should go to next activity,say-activity_next.xml.
Below is my code to slide Image i.e., SlideActivity.java
public class SlideActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
#Override
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 context = Illustrations.this;
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(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
}
}
I'm not getting where to pass Intent for next activity i.e., activity_next.xml
**** UPDATED CODE as per your suggestions ****
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position==viewPager.getAdapter().getCount()){
Intent reg = new Intent(SlideActivity.this,activity_next.class);
startActivity(reg);
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
#Override
public int getCount() {
return mImages.length+1;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Illustrations.this;
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);
if(position < getCount()-1)
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
****activity_next.java code is below ****
public class activity_next extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
}
}
**** activity_next.xml code is as below****
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Nowhere Else" />
</LinearLayout>
I'm not getting where to pass Intent for next activity i.e.,
activity_next.xml
To start Activity on reach of last item in ViewPager add OnPageChangeListener to ViewPager and in onPageSelected start next Activity:
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position)
{
if(position==viewPager.getAdapter().getCount()){
//start next Activity here with activity_next.xml layout
}
}
....
});
If you want to slide the last image to go to the next Activity, like a "dismiss" efect, you should try something like this:
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
#Override
public int getCount() {
return mImages.length+1;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Illustrations.this;
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);
if(position < getCount()-1)
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
and then:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position)
{
if(position==viewPager.getAdapter().getCount()-1){
//start next Activity
}
}
});
}
I got it clear as #Chol said I just made the count to -1 as below.
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
if(position==viewPager.getAdapter().getCount()-1){
Intent reg = new
Intent(Illustrations.this,Register_Page.class);
startActivity(reg);
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
}
Thank you all very much ... :)
The position is image's position that comes array like slide_image[position]. So, with using position, the Intent can be create at the SliderAdapter page just writing several code.
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
layoutInflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.layout, container, false);
LinearLayout layoutslide = view.findViewById(R.id.slider);
ImageView slideImageView =(ImageView)view.findViewById(R.id.slide_image);
TextView slideHeading=(TextView)view.findViewById(R.id.slide_heading);
TextView slideDescription=(TextView)view.findViewById(R.id.slide_desc);
slideImageView.setImageResource(slide_image[position]);
slideHeading.setText(slide_headings[position]);
slideDescription.setText(slide_description[position]);
slideImageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (position == 0) {
Intent myIntent = new Intent(context,EarthQuakeActivity.class);
context.startActivity(myIntent);
}
if (position == 1) {
Intent myIntent = new Intent(context,EarthQuakeActivity.class);
context.startActivity(myIntent);
}
if (position == 2) {
Intent myIntent = new Intent(context,EarthQuakeActivity.class);
context.startActivity(myIntent);
}
}
});
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) {
}
}
in My root Activity i have Slide show that displayed by the VIewPager .
in my case i want to open new activity or dialog box or something like this when i pressed image on Slide show .
Normally i used OnclickListener and assign picture id to it and don't get any Feedback .
RootActivity.java
public class RootActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_root);
// Image slide show
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
ImageAdapter.setTimer(viewPager, 3, 0);
// End Image slide show
ViewPager vp = (ViewPager)findViewById(R.drawable.ax1);
vp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.print("OK ...");
}
});
}
}
ImageAdapter.java
public class ImageAdapter extends PagerAdapter {
final static Handler handler = new Handler();
public static Timer swipeTimer ;
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.ax1,
R.drawable.ax2,
};
ImageAdapter(Context countDownTimer){
this.context=countDownTimer;
}
#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);
}
public static void setTimer(final ViewPager myPager, int time, final int curPage){
final Runnable Update = new Runnable() {
int NUM_PAGES =5;
int currentPage = curPage ;
public void run() {
if (currentPage == NUM_PAGES ) {
currentPage = 0;
}
myPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 1000, time*1000);
}
}
Okay, first things first...
1. This line, in RootActivity.onCreate() does not make a lot of sense:
ViewPager vp = (ViewPager)findViewById(R.drawable.ax1);
You're trying to find a ViewPager object in the layout using a drawable resource id. So delete this line and everything in onCreate() that comes after it.
2. You can only attach an OnClickListener to an existing view and since there is no OnItemClickListener for PagerAdapters, you need to attach the OnClickListener to the appropriate Views from within the Adapter's instantiateItem() method.
I have attached a modified version of your code representing these points.
RootActivity.java
public class RootActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_root);
// Image slide show
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
ImageAdapter.setTimer(viewPager, 3, 0);
// End Image slide show
}
}
ImageAdapter.java
Look for the // Begin Added by SpaceLord comments
public class ImageAdapter extends PagerAdapter {
final static Handler handler = new Handler();
public static Timer swipeTimer;
Context context;
private int[] GalImages = new int[]{
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.ax1,
R.drawable.ax2,
};
ImageAdapter(Context countDownTimer) {
this.context = countDownTimer;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == 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]);
// Begin Added by SpaceLord
imageView.setOnClickListener(itemClickListener);
imageView.setTag(position);
// End Added by SpaceLord
container.addView(imageView, 0);
return imageView;
}
// Begin Added by SpaceLord
private final View.OnClickListener itemClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final int tag = (Integer) v.getTag();
switch (tag) {
// Do whatever you need to for each slide click here
case 0:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on ONE");
break;
case 1:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on TWO");
break;
case 2:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on THREE");
break;
case 3:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on AX1");
break;
case 4:
Log.d(ImageAdapter.class.getSimpleName(), "Clicked on AX2");
break;
}
}
};
// End Added by SpaceLord
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ImageView) object);
}
public static void setTimer(final ViewPager myPager, int time, final int curPage) {
final Runnable Update = new Runnable() {
int NUM_PAGES = 5;
int currentPage = curPage;
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
myPager.setCurrentItem(currentPage++, true);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 1000, time * 1000);
}
}
Directly set onClickListener(...) to your ImageView in your ImageAdapter like:
#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]);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Do your job
}
});
((ViewPager) container).addView(imageView, 0);
return imageView;
}
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);
}
}