安卓GridView简单


1.效果图


2.代码

package com.example.test;

import com.example.adapter.GridViewAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.GridView;
import android.widget.Toast;

/**
 * last-modified :下午4:23:21
 * 
 * @author jack chen
 */
public class GridViewActivity extends Activity implements OnItemClickListener {
	private String[] data = new String[] { "好一朵茉莉花", "梅花三弄", "爱如潮水","一直很安静","命中注定" };
	private GridView gridView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_gridview);
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setAdapter(new GridViewAdapter(GridViewActivity.this,data));
		gridView.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Toast.makeText(GridViewActivity.this, " position:" + position, 2000).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}



/**
 * 
 */
package com.example.adapter;

import com.example.test.R;

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

/**
 * last-modified :下午5:39:38
 * 
 * @author jack chen
 */
public class GridViewAdapter extends BaseAdapter {
	private String[] data;
	private LayoutInflater inflater;

	public GridViewAdapter(Context context,String[] data) {
		this.data = data;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return data.length;
	}

	@Override
	public Object getItem(int position) {
		return data[position];
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder; 
		if(convertView == null){
			convertView = inflater.inflate(R.layout.grid_view_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.title.setText(data[position]);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		return convertView;
	}
	
	private class ViewHolder{
		TextView title;
	}
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:listSelector="#00000000"
        android:numColumns="auto_fit" />

</RelativeLayout>

<?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:background="@drawable/bound"
    android:padding="5dip"
    android:layout_marginTop="10dip"
    android:layout_marginLeft="4dip"
    android:layout_marginRight="4dip"
    android:layout_marginBottom="5dip"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/title"
        android:layout_width="100dip"
        android:layout_height="60dip"
        android:gravity="center"
        android:text="this is a text"/>
   
</LinearLayout>


类似于ListView,扩展性非常好。样式什么的可以自行调节。 

安卓官方文档 


我是菜鸟,我在路上。

你可能感兴趣的:(android,GridView)