GridView的基本用法和属性

1、gridview中文意思是网格视图,gridlayout中文意思是网格布局。前者是用来显示一些图片用的,最适合用来显示九宫格图片展示了,后者是用来实现可拖拽布局。

2、gridview使用的adapter跟listview是一样的adapter,都是BaseAdapter。

class GridViewAdapter extends BaseAdapter{

    @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) {
    if (convertView == null){
        convertView = View.inflate(MainActivity.this,R.layout.item,null);
    }
        ImageView imageView = (ImageView) convertView.findViewById(R.id.iv);
        imageView.setImageResource(images[position]);
        return convertView;
    }

3、网格布局的xml======》

    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="4"
    android:horizontalSpacing="4dp"
    android:verticalSpacing="4dp">


4、gridview也可以用来显示横向的listview的效果,并且对于内存的开销也比recyclerview要小的多,下面就写一个横向的小demo,代码如下:

public class MainActivity extends AppCompatActivity {
    int[] images = new int[]{
            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.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.ic_launcher, R.mipmap.ic_launcher
    };
    private GridView gridView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView = (GridView) this.findViewById(R.id.gridview);
        GridViewAdapter adapter = new GridViewAdapter();
        gridView.setAdapter(adapter);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int allWidth = (int) (110 * images.length * density);//整个横向的gridview的宽度
        int itemWidth = (int) (100 * density);//条目的宽度
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                allWidth, LinearLayout.LayoutParams.FILL_PARENT);
        gridView.setLayoutParams(params);
        gridView.setColumnWidth(itemWidth);
        gridView.setHorizontalSpacing(10);
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setNumColumns(images.length);
    }

    final class GridViewAdapter extends BaseAdapter {

        @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) {
            if (convertView == null) {
                convertView = View.inflate(MainActivity.this, R.layout.item, null);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.iv);
            imageView.setImageResource(images[position]);
            return convertView;
        }

    }

xml===========如下:记得要在gridview外面套一个线性布局,因为scrollview只能有一个孩子

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

            android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">
                    android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >
        
    
RelativeLayout>


5、gridview有两个时间监听:

(1)选中监听:gridview.setOnItemSelectedListener

(2)单击监听:gridview.setOnItemClickListener

你可能感兴趣的:(gridview)