Gallery实现横向滑动图片

Gallery可以横向显示一组图片,并且可以横向滑动。下面展示如何使用Gallery去显示图片。

其用法用ListView几乎一致。

大致为:

1.在布局文件中加入Gallery控件

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.gallertdemo.MainActivity" >

    <Gallery  
        android:id="@+id/gallery"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" /> 

</RelativeLayout>

2.给Gallery创建适配器(适配器可以继承BaseAdapter或者其子类)

GalleryAdapter.java

public class GalleryAdapter extends BaseAdapter{

	private Context mContext;
	private Integer[] imageIDs;
	private String[] str;
	public GalleryAdapter(Context mContexts,Integer[] imageIDs,String[] str){
		this.imageIDs = imageIDs;
		this.str = str;
		this.mContext = mContexts;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return imageIDs.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.imgandtext, null);
			holder.pic = (ImageView) convertView.findViewById(R.id.image);
			holder.text = (TextView) convertView.findViewById(R.id.text);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.pic.setImageResource(imageIDs[position]);
		holder.text.setText(str[position]);
		return convertView;
	}
	
	class ViewHolder {
		private ImageView pic;
		private TextView text;
	}
}

3.条目imgandtext.xml的布局文件

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:textSize="12dp" />

</LinearLayout>

4.在MainActivity中使用Gallery

public class MainActivity extends ActionBarActivity {
	Integer[] imageIDs = { R.drawable.nanhuaijin, R.drawable.nanhuaijin_biguan,
			R.drawable.nanhuaijin_faxiang, R.drawable.nanhuaijin_miss,
			R.drawable.nanhuaijin_school, R.drawable.nanhuaijin_zhuangyan };
	private String[] str = { "jay1", "jay2", "jay3", "jay4", "jay5", "jay6", };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setAdapter(new GalleryAdapter(this, imageIDs, str));
		gallery.setSelection(0);// 默认显示第一张图片
		gallery.setSpacing(20);// 图片之间距离
		gallery.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getBaseContext(),
						"pic" + (position + 1) + " selected",
						Toast.LENGTH_SHORT).show();
			}
		});
	}
}


效果图:

Gallery实现横向滑动图片_第1张图片


Gallery效果默认第一张图片是居中显示的,看到有人说想要靠左边显示可以设置setSelection(1),觉得这种方法不好,有其他实现方法的朋友可以一起 讨论。


你可能感兴趣的:(Gallery实现横向滑动图片)