Simply make Image Gallery using ViewPager.When you slide Finger over image and swipe left to right used ViewPager.Many other controller available but i think it is very easy to maintain and create gallery using ViewPager.
Start new project and give name as ViewPagerDemo and begin your development of your android Gallery. Put your all Gallery images into drawable folder and as of now put four images and give name as a,b,c,d.
Now need to create one helper class so it is helpful to in creation of Gallery give name as ImageSwipeAdapter.java and extends PagerAdapter and modified as below
package com.idroid.viewpager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImageSwipeAdapter extends PagerAdapter {
Context context;
private int[] Images = new int[] {
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d
};
ImageSwipeAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return Images.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 pad = context.getResources().getDimensionPixelSize(R.dimen.m_padding);
imageView.setPadding(pad, pad, pad, pad);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(Images[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Now start programming for Swipe feature for that modify your main.xml file as below code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Now need to modify your MainActivity.java file as per below so your Image ViewPager is redy to run
package com.idroid.viewpager;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageSwipeAdapter adapter = new ImageSwipeAdapter(this);
viewPager.setAdapter(adapter);
}
}
No comments:
Post a Comment