问题
I want to create a matrix like gridview say 10x20 matrix
we want to specify the number of rows and column of the View ie 10x20
if the screen is low it should scroll to horizontally and vertically for example the image below describe the Matrix Gridview Each cell represent (0,0) (0,1) etc... (1,0) (1,1) etc..
How can generate this type of view? Advance Thanks ....!!!
回答1:
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 :
回答2:
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
回答3:
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 !!
来源:https://stackoverflow.com/questions/26501752/how-to-create-custom-gridview-like-matrix-structure-in-android