Gallery实现方法

在MainActivity中:

package com.example.text10_gallery;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;


public class MainActivity extends Activity {


protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
}
   GalleryAdapter galleryAdapter;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Gallery gallery = (Gallery) findViewById(R.id.gallery);
       galleryAdapter = new GalleryAdapter(MainActivity.this);
       gallery.setAdapter(galleryAdapter);
       //相应的点击事件
       gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               Toast.makeText(MainActivity.this, "您点击的是" + i, Toast.LENGTH_LONG).show();
           }
       });
   }

}

在创建一个GalleryAdapter


package com.example.text10_gallery;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;


/**
 * Created by ZWH on 2016/5/16.
 */
public class GalleryAdapter extends BaseAdapter {
    private Context mContext;
    //设置要展示的图片资源
    int[] images = {R.drawable.a123, R.drawable.a354, R.drawable.aa555, R.drawable.asdd};


    public GalleryAdapter(Context context) {
        this.mContext = context;
    }


    @Override
    public int getCount() {
        return images.length;
    }


    @Override
    public Object getItem(int i) {
        return i;
    }


    @Override
    public long getItemId(int i) {
        return i;
    }


@Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //在此最好判断一下view是否为空
        ImageView image = new ImageView(mContext);
        image.setImageResource(images[i]);
        image.setAdjustViewBounds(true);
        //设置调整视图边距
        //设置宽高
        image.setLayoutParams(new Gallery.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return image;
    }

}


布局

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.mytest.MainActivity">


    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/qq_41357725/article/details/80341953