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

时间:2014-10-22 06:47:04

标签: android gridview viewgroup

我想创建一个类似gridview的矩阵说10x20矩阵

我们要指定View的行数和列数,即10x20

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

如何生成此类视图? 提前谢谢...... !!!

enter image description here

3 个答案:

答案 0 :(得分:5)

试试这个:

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);

}

}

输出

enter image description here

答案 1 :(得分:2)

这可以通过GridLayout和动态Cell创建来实现

/ *  *  *版权所有2012 Jess Anders  *  *根据Apache许可证2.0版(&#34;许可证&#34;)获得许可;  *除非符合许可,否则您不得使用此文件。  *您可以在以下位置获取许可证副本  *  * http://www.apache.org/licenses/LICENSE-2.0  *  *除非适用法律要求或书面同意,否则软件  *根据许可证分发,发布在&#34; AS IS&#34;基础,  *不附带任何明示或暗示的保证或条件。  *有关管理权限的特定语言,请参阅许可证  *许可证下的限制。  * /

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    GridLayout gl;
    TextView[] text;
    int item;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        gl = new GridLayout(MainActivity.this);
        gl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        gl.setOrientation(0);
        gl.setColumnCount(11);
        gl.setRowCount(3);

        text = new TextView[100];
        ScrollView sv = new ScrollView(this);
        sv.setScrollbarFadingEnabled(false);
        HorizontalScrollView scrolview = new HorizontalScrollView(this);
        scrolview.setScrollbarFadingEnabled(false);
        LinearLayout linearLayout = new LinearLayout(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(params);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.addView(sv);
        sv.addView(scrolview);
        scrolview.setHorizontalScrollBarEnabled(true);
        setContentView(linearLayout);
        for (int i = 0; i < 100; i++) {
            for (int j = 0; i < 10; j++) {
                text[i] = new TextView(MainActivity.this);
                text[i].setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                text[i].setText(String.valueOf(i) + "," + String.valueOf(j));
                text[i].setTextSize(25);
                text[i].setPadding(50, 25, 10, 25);
                text[i].setOnClickListener(new View.OnClickListener() {

                    int pos = item;

                    public void onClick(View v) {

                        Toast.makeText(getBaseContext(), pos + " Clicked",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                gl.addView(text[i]);
            }
        }

        scrolview.addView(gl);

    }

 }

这不是直接的解决方案,但是...我想我需要自定义我目前不知道的GridView

答案 2 :(得分:0)

嗨,这是我创建的用于执行按钮“ x”的一个矩阵的代码

package com.example.andre.aplicacaocalibracaov2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
    GridView gridView;
    List<String> lstSource = new ArrayList<>();

    String[] array_caracteres={
      "x"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpList();
        GridView gridView = (GridView) findViewById(R.id.gridView);
        //é a classe gridadapter
        GridAdapter adapter = new GridAdapter(lstSource,MainActivity.this);
        gridView.setAdapter(adapter);


    }
        public void setUpList() {
            for (String item : array_caracteres)
                for (int i = 0; i < 10; i++) {
                    for (int j = 0; j < 10; j++)
                        lstSource.add(item);
                }
        }
}


GridAdapter.java

package com.example.andre.aplicacaocalibracaov2;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

public class GridAdapter extends BaseAdapter {

    private Context context;
    List<String> lstSource;

    public GridAdapter(List<String>lstSource, Context context){
        this.lstSource = lstSource;
        this.context=context;


    }
    @Override
    public int getCount() {

        return lstSource.size();
    }

    @Override
    public Object getItem(int position) {

        return lstSource.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Button button;
        if(convertView ==null){
            button = new Button(context);
            button.setLayoutParams(new GridView.LayoutParams(85,85));
            button.setPadding(8,8,8,8);
            button.setText(lstSource.get(position));
            button.setBackgroundColor(Color.YELLOW);
            button.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v){
                    Toast.makeText(context,button.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            });


        }
        else
            button=(Button)convertView;
        return button;
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity"

    >

    <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:columnWidth="30dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="4dp"
        android:horizontalSpacing="4dp"
        android:gravity="center"
        android:layout_centerInParent="true"
 />


</RelativeLayout>

祝你好运!