GridView 的简单应用

gridView 是android一个控件主要是显示列似与九宫格这样的效果。废话不多说直接上代码。

首先是需要一个适配器来确定每一个里面的布局,在里面我自定义了一个点击事件,当点击图片布局的时候触发,实现在activity中去实现他,当然也可以使用OnItemClickLisntner来直接使用每一个item的点击。

GridViewAdapter.java
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;


/**
 * Created by zd on 2018/12/29.
 */
public class GridViewAdapter extends BaseAdapter {

    private List<String> data;//数据

    private Context context;//上下文

    private OnImgClickListener itemClickListener;

    public GridViewAdapter(List<String> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(final int position,final View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            //加载子布局
            view = LayoutInflater.from(context).inflate(R.layout.item_grid, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) view.findViewById(R.id.img);
            viewHolder.textView = (TextView) view.findViewById(R.id.txt);
            viewHolder.ll = (LinearLayout) view.findViewById(R.id.ll);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        String plan = data.get(position);

        if (plan != null) {
            viewHolder.imageView.setImageResource(R.mipmap.ic_launcher);
            viewHolder.textView.setText(plan);

            viewHolder.ll.setTag(position);
            viewHolder.ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LinearLayout btn = (LinearLayout) v;
                    int pos = (Integer) btn.getTag();
                    Log.e("sys"," position2 :" + pos);
                    Log.e("sys"," data2 :" + data.get(pos).toString());
                    if(itemClickListener!=null){
                        itemClickListener.onImgClick((Integer) v.getTag());
                    }
                }
            });
        }
        return view;
    }

    private class ViewHolder {
        ImageView imageView;
        TextView textView;
        LinearLayout ll;
    }

    //自定义点击接口
    public  interface OnImgClickListener {
        void onImgClick(int tag);
    }

    //接口设置
    public void setImgClickListener(OnImgClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }
}

  

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

    <LinearLayout
        android:id="@+id/ll"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_width="80dp"
        android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img"
        android:layout_width="80dp"
        android:layout_height="100dp"/>

    <TextView
        android:maxLines="1"
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    </LinearLayout>
</LinearLayout>

 

MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    GridViewAdapter gridViewAdapter;
    List<String> data;

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


        data = new ArrayList<>();
        data.add("A1");
        data.add("A2");
        data.add("A3");
        data.add("A4");
        data.add("A5");
        data.add("B1");
        data.add("B2");
        data.add("B3");
        data.add("B4");
        data.add("B5");
        for (int i=0;i<30;i++){
            data.add("C"+i);
        }
        gridView = (GridView) findViewById(R.id.girdView);

        //实例化适配器
        gridViewAdapter = new GridViewAdapter(data, this);
        gridViewAdapter.setImgClickListener(new GridViewAdapter.OnImgClickListener() {
            @Override
            public void onImgClick(int tag) {
                //
                Log.e("sys","tag"+tag);
                Log.e("sys"," data2 :" + data.get(tag).toString());
            }
        });
        //设置适配器
        gridView.setAdapter(gridViewAdapter);

    }
}

  activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kkrs.grid.MainActivity">

    <GridView
        android:columnWidth="100dp"
        android:numColumns="4"
        android:id="@+id/girdView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></GridView>

</LinearLayout>

  android:numColumns="4" 表示一行有多少个

posted @ 2018-12-29 15:46  坎坷人生0  阅读(272)  评论(0编辑  收藏  举报