1. 程式人生 > >Android學習-網格檢視GridView

Android學習-網格檢視GridView

一、簡介:

GridView是一個以表格形式顯示多張圖片等元件。它是按照行列的方式來顯示內容的,比如實現九宮格圖,用GridView是首選。

二、程式碼塊:

看過我上一篇部落格的同學應該知道,一步一步全部步驟寫出來是很費時間的,大概流程就那樣,所以這次網格檢視就直接上程式碼塊了,步驟差不多。

在activity_main.xml中新增一個按鈕:

<Button
        android:id="@+id/btn_gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="GridView"
        android:textAllCaps="false"/>

MainActivity在原有基礎上新增:

先宣告控制元件:

private Button mBtnGridView;

在onCreate:

mBtnGridView=findViewById(R.id.btn_gridview);

在setListeners:

mBtnGridView.setOnClickListener(onClick);

在OnClick:

		case R.id.btn_gridview:
        //跳轉到GridView演示頁面
            intent = new Intent(MainActivity.this, GridViewActivity.class);
            break;

在activity_grid_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">

    <GridView
        android:id="@+id/gv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"/>
</LinearLayout>

在GridViewActivity:

先宣告控制元件:

private GridView mGv;

在onCreate:

mGv=findViewById(R.id.gv);

在之前新建MyGridViewAdapter:

public class MyGridViewAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return 10;
    }

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return null;
    }
}

新建layout_grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <ImageView
        android:id="@+id/iv_grid"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:scaleType="fitCenter"
        android:background="@color/colorPrimaryDark"/>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textColor="@color/colorAccent"
        android:gravity="center"
        android:layout_marginTop="10dp"
        />
</LinearLayout>

在MyGridViewAdapter中修改:

public class MyGridViewAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public MyGridViewAdapter(Context context){
        this.mContext=context;
        mLayoutInflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return 10;
    }

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

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

    static class ViewHolder{
        public ImageView imageView;
        public TextView textView;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view=mLayoutInflater.inflate(R.layout.layout_grid_item,null);
            holder=new ViewHolder();
            holder.imageView=view.findViewById(R.id.iv_grid);
            holder.textView=view.findViewById(R.id.tv_title);
            view.setTag(holder);
        }else {
            holder=(ViewHolder)view.getTag();
        }
        //賦值
        holder.textView.setText("ycm");
        Glide.with(mContext).load("http://i1.bvimg.com/670191/a72f2a8c0f289d48s.png").into(holder.imageView);
        return view;
    }
}

在GridViewActivity中新增:

mGv.setAdapter(new MyGridViewAdapter(GridViewActivity.this));

三、執行截圖:

GridView
設定點選和長按事件之前的ListView中介紹過了,就不介紹了。