问题
I have Image Gallery . In start, I have to swap right to left to see more Images . I want to Images Gallery that start left to right swamping by default
public View getView(int index, View view, ViewGroup viewGroup)
{
ImageView img = new ImageView(mContext);
ShowImgsActivity.pagNumber.setText(index+"");
img.setImageResource(ShowImgsActivity.typedImgs.getResourceId((ShowImgsActivity.typedImgs.length()-1)-index, -1));
img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
img.setScaleType(ImageView.ScaleType.FIT_XY);
回答1:
You can use ViewPager as an Image Gallery.It lets you swipe left and right to see previos and next images.
Here's is Java class,layout file and Adapter.
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);
}
}
Layout file:
<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>
Adapter:
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);
}
}
You can ask if you have any further queries.View flipper is another option.
Happy coding :)
Reference :here
来源:https://stackoverflow.com/questions/21623847/how-to-do-gallery-images-swap-left-to-right-bydefault