如何在Android中创建自定义gridview(如矩阵结构)

我想创建一个像gridview说10×20矩阵的矩阵

我们想要指定View的行数和列数,即10×20

如果屏幕很低,它应该水平和垂直滚动
例如,下图描述了Matrix Gridview
每个细胞代表
(0,0)(0,1)等……
(1,0)(1,1)等..

如何生成这种类型的视图?
提前谢谢…. !!!

解决方法:

试试这个:

GridViewCustomAdapter

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

public class GridViewCustomAdapter extends BaseAdapter {

    ArrayList<String> items;

    static Activity mActivity;

    private static LayoutInflater inflater = null;

    public GridViewCustomAdapter(Activity activity, ArrayList<String> tempTitle) {
        mActivity = activity;
        items = tempTitle;

        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public final int getCount() {

        return items.size();

    }

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

    @Override
    public final long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View v = null;

        v = inflater.inflate(R.layout.item, null);

        Button tv = (Button) v.findViewById(R.id.button);
        tv.setText(items.get(position));

        return v;
    }

}

gridview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="20dip"
        android:gravity="center"
        android:horizontalSpacing="2dp"
         android:verticalSpacing="2dp"
        android:numColumns="20"
        android:stretchMode="columnWidth" >
    </GridView>

</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button"
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:textSize="10sp"
    android:background="@android:color/holo_blue_light"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

GridViewActivity

公共类GridViewActivity扩展Activity {

private GridView list;
ArrayList<String> data = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview);
    for (int i = 0; i < 10; i++) {
        for(int j=0;j<20;j++)
        data.add(i+"-"+j);
    }
    GridViewCustomAdapter adapter = new GridViewCustomAdapter(this, data);

    list = (GridView) findViewById(R.id.grid_view);
    list.setAdapter(adapter);

}

}

输出:

上一篇:Android – 隐藏视图


下一篇:NestedScrollView中嵌套recyclerView的焦点问题