GridView布局及适配器优化

Posted L先森

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GridView布局及适配器优化相关的知识,希望对你有一定的参考价值。

1.布局样式

<GridView
        android:id="@+id/gridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#DCDCDC"
        android:gravity="center"
        android:horizontalSpacing="1dp"
        android:listSelector="@null"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dp" />

2.适配器对象

import android.view.View;

/**
 * Author:JsonLu
 * DateTime:2016/6/3 14:47
 * Email:[email protected]
 * Desc:
 **/
public class GridViewItem {
    private int resId;
    private String name;
    private View.OnClickListener clickListener;

    public int getResId() {
        return resId;
    }

    public void setResId(int resId) {
        this.resId = resId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public View.OnClickListener getClickListener() {
        return clickListener;
    }

    public void setClickListener(View.OnClickListener clickListener) {
        this.clickListener = clickListener;
    }
}

3.填充子节点布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/grid_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/grid_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

4.适配器

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

import java.util.List;

/**
 * Author:JsonLu
 * DateTime:2016/6/3 14:33
 * Email:[email protected]
 * Desc:
 **/
public abstract class GridAdapter extends BaseAdapter {

    public abstract List<GridViewItem> getData();
    private List<GridViewItem> list;
    private GridViewItem item;
    private LayoutInflater layoutInflater;

    public GridAdapter(Context context) {
        list = getData();
        layoutInflater = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.girdview_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.grid_icon);
            TextView textView = (TextView) view.findViewById(R.id.grid_name);
            item = list.get(position);
            imageView.setImageDrawable(view.getContext().getResources().getDrawable(item.getResId()));
            textView.setText(item.getName());
            view.setOnClickListener(item.getClickListener());

        }
        return view;
    }
}

5.使用

GridView gridView = (GridView) findViewById(R.id.gridView);
        gridView.setAdapter(new GridAdapter(this) {
            @Override
            public List<GridViewItem> getData() {
                List<GridViewItem> list = new ArrayList<GridViewItem>();
                for (int i = 0; i < 9; i++) {
                    GridViewItem item = new GridViewItem();
                    final int index = i;
                    item.setName("Title"+i);
                    item.setResId(R.drawable.ic_launcher);
                    item.setClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(getBaseContext(), "点击"+index, Toast.LENGTH_SHORT).show();
                        }
                    });
                    list.add(item);
                }
                return list;
            }
        });

 

以上是关于GridView布局及适配器优化的主要内容,如果未能解决你的问题,请参考以下文章

如何在选项卡片段内调用gridView适配器

Android实现简单日历

从适配器中的片段调用元素

Android学习之GridView图片布局适配经验

错误:E/RecyclerView:未连接适配器;跳过片段上的布局

Android实习生 —— 屏幕适配及布局优化