How to create custom gridview (Like matrix structure ) in Android

筅森魡賤 提交于 2019-12-04 12:09:21

try this:

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

public class GridViewActivity extends 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);

}

}

output :

This can be achieved by GridLayout and dynamic Cell creation

/* * * Copyright 2012 Jess Anders * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */

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

    }

 }

This is not the straight solution but... i think i need to customize the GridView which i do`nt know currently

hi this is one code that i've created to do one matrix of buttons "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;
    }
}

the 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>

Good luck !!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!