Android

2018-06-04-GridView

2018-06-05  本文已影响0人  天才蒙蒙亮

GridView

GridView常用属性及自定义适配器

带有图片的GridView

  1. xml布局文件
 <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:numColumns="auto_fit"          设置总列数
        android:columnWidth="90dp"            
        android:verticalSpacing="10dp"         设置垂直间隔
        android:horizontalSpacing="10dp"        设置水平空格
        android:stretchMode="columnWidth"       设置增长模式
        android:gravity="center"
        android:layout_alignParentStart="true"
        android:layout_marginTop="41dp" />

  1. Activity
/**
 * GridView组件:
 * 1.自定义适配器
 * (1)创建一个类
 * (2)实现四个方法
 *      getCount:获取要显示的总数
 *      getItem:获取每一个选项
 *      getItemId:选项ID
 *      getView:返回一个视图,为每一个选项生成视图(ImageView),该方法会被多次调用
 *
 */
public class MainActivity extends AppCompatActivity {
    private GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //获取到GridView,通过适配器加载资源
        gridView = (GridView) findViewById(R.id.gridView);
        gridView.setAdapter(new MyAdapter(this));
        
    }
    
    
    
    //自定义适配器,继承BaseAdapter
    static class MyAdapter extends BaseAdapter{
        private Context context = null;
        
        //图片资源int类型
        private int[] images = {R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,
                R.mipmap.misic,
                R.mipmap.misic,
                R.mipmap.misic,
                R.mipmap.misic,
                R.mipmap.misic,
                R.mipmap.misic,
                R.mipmap.misic,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar
        };

        public MyAdapter(Context context) {
            this.context = context;
        }


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

        @Override
        public Object getItem(int position) {
            return images[position];
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //ImageView的创建需要Context,所以通过构造函数,将原先调用者的context传过来
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(images[position]);
            return imageView;
        }
    }
}

GridView图文排列

带有图片和文字的GridView,和上一个一样,也是需要通过适配器绑定资源。因为带有图文的GridView由图片和文字两部分组成,所以在getView的时候,可以通过自定义的Layout(Custom Layout)来作为View的单个布局。

  1. xml布局文件
<GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gridView2"
        android:numColumns="4"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth"
        android:columnWidth="60dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

  1. 新建Layout文件,作为GridView的单个布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView3"
        android:src="@drawable/music"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView3"
        android:layout_gravity="center_vertical" />


</LinearLayout>

  1. Activity获取
public class Main22Activity extends AppCompatActivity {

    private GridView gridView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main22);


        gridView = (GridView) findViewById(R.id.gridView2);
        gridView.setAdapter(new MyAdapter(this));
        
    }

    static class MyAdapter extends BaseAdapter{
        private Context context;
        private String[] names = {"转账","查询","金融","基金","国债","信用卡","商城","充值","红包"};
        private int[] images = {
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar,
                R.mipmap.avatar
        };

        public MyAdapter(Context context){
            this.context = context;
        }

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

        @Override
        public Object getItem(int position) {
            return images[position];
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        
            //通过LayoutInflater.from(context)来获取layout布局文件资源
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.itemlayout, null);
            
            //找到布局文件
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView3);
            TextView textView = (TextView) view.findViewById(R.id.textView3);

            //将图片和文字资源匹配到布局文件
            imageView.setImageResource(images[position]);
            textView.setText(names[position]);

            return view;
        }
    }
}

  1. 可以设置事件监听

activity中添加以下代码

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(Main22Activity.this,"gridView",Toast.LENGTH_SHORT).show();
            }
        });

上一篇下一篇

猜你喜欢

热点阅读