Android GridView的使用页面按钮
GridView(网格视图),用来显示网格,用来显示一些按钮比较好用。
上代码吧。
主页面:activity_main.xml 添加一个GridView用来显示按钮的列表
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:padding="5dp" tools:context=".MainActivity"> <!--一行显示4个按钮--> <GridView android:id="@+id/grid_button_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="4" /> </LinearLayout >
按钮显示页面:button_menu_item.xml ,注意这里用相对布局RelativeLayout,
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" android:orientation="vertical"> <ImageView android:id="@+id/img_icon" android:layout_width="64dp" android:layout_height="64dp" android:layout_centerInParent="true" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/txt_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/img_icon" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:text="" android:textSize="18sp" /> </RelativeLayout >
代码模型:Icon.java
package action.sun.com.gridviewtest; public class Icon { private int iId; private String iName; public Icon() { } public Icon(int iId, String iName) { this.iId = iId; this.iName = iName; } public int getiId() { return iId; } public String getiName() { return iName; } public void setiId(int iId) { this.iId = iId; } public void setiName(String iName) { this.iName = iName; } }
适配器:MyAdapter.java
package action.sun.com.gridviewtest; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; /** * Created by Jay on 2015/9/22 0022. */ public abstract class MyAdapter<T> extends BaseAdapter { private ArrayList<T> mData; private int mLayoutRes; //布局id public MyAdapter() { } public MyAdapter(ArrayList<T> mData, int mLayoutRes) { this.mData = mData; this.mLayoutRes = mLayoutRes; } @Override public int getCount() { return mData != null ? mData.size() : 0; } @Override public T getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = ViewHolder.bind(parent.getContext(), convertView, parent, mLayoutRes , position); bindView(holder, getItem(position)); return holder.getItemView(); } public abstract void bindView(ViewHolder holder, T obj); //添加一个元素 public void add(T data) { if (mData == null) { mData = new ArrayList<>(); } mData.add(data); notifyDataSetChanged(); } //往特定位置,添加一个元素 public void add(int position, T data) { if (mData == null) { mData = new ArrayList<>(); } mData.add(position, data); notifyDataSetChanged(); } public void remove(T data) { if (mData != null) { mData.remove(data); } notifyDataSetChanged(); } public void remove(int position) { if (mData != null) { mData.remove(position); } notifyDataSetChanged(); } public void clear() { if (mData != null) { mData.clear(); } notifyDataSetChanged(); } public static class ViewHolder { private SparseArray<View> mViews; //存储ListView 的 item中的View private View item; //存放convertView private int position; //游标 private Context context; //Context上下文 //构造方法,完成相关初始化 private ViewHolder(Context context, ViewGroup parent, int layoutRes) { mViews = new SparseArray<>(); this.context = context; View convertView = LayoutInflater.from(context).inflate(layoutRes, parent, false); convertView.setTag(this); item = convertView; } //绑定ViewHolder与item public static ViewHolder bind(Context context, View convertView, ViewGroup parent, int layoutRes, int position) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(context, parent, layoutRes); } else { holder = (ViewHolder) convertView.getTag(); holder.item = convertView; } holder.position = position; return holder; } @SuppressWarnings("unchecked") public <T extends View> T getView(int id) { T t = (T) mViews.get(id); if (t == null) { t = (T) item.findViewById(id); mViews.put(id, t); } return t; } /** * 获取当前条目 */ public View getItemView() { return item; } /** * 获取条目位置 */ public int getItemPosition() { return position; } /** * 设置文字 */ public ViewHolder setText(int id, CharSequence text) { View view = getView(id); if (view instanceof TextView) { ((TextView) view).setText(text); } return this; } /** * 设置图片 */ public ViewHolder setImageResource(int id, int drawableRes) { View view = getView(id); if (view instanceof ImageView) { ((ImageView) view).setImageResource(drawableRes); } else { view.setBackgroundResource(drawableRes); } return this; } /** * 设置点击监听 */ public ViewHolder setOnClickListener(int id, View.OnClickListener listener) { getView(id).setOnClickListener(listener); return this; } /** * 设置可见 */ public ViewHolder setVisibility(int id, int visible) { getView(id).setVisibility(visible); return this; } /** * 设置标签 */ public ViewHolder setTag(int id, Object obj) { getView(id).setTag(obj); return this; } //其他方法可自行扩展 } }
主页面代码:MainActivity.java
package action.sun.com.gridviewtest; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity { private Context mContext; private GridView grid_photo; private BaseAdapter mAdapter = null; private ArrayList<Icon> mData = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; grid_photo = (GridView) findViewById(R.id.grid_button_menu); mData = new ArrayList<Icon>(); mData.add(new Icon(R.mipmap.ic_launcher, "图标1")); mData.add(new Icon(R.mipmap.ic_launcher, "图标2")); mData.add(new Icon(R.mipmap.ic_launcher, "图标3")); mData.add(new Icon(R.mipmap.ic_launcher, "图标4")); mData.add(new Icon(R.mipmap.ic_launcher, "图标5")); mData.add(new Icon(R.mipmap.ic_launcher, "图标6")); mData.add(new Icon(R.mipmap.ic_launcher, "图标7")); mAdapter = new MyAdapter<Icon>(mData, R.layout.button_menu_item) { @Override public void bindView(ViewHolder holder, Icon obj) { holder.setImageResource(R.id.img_icon, obj.getiId()); holder.setText(R.id.txt_icon, obj.getiName()); } }; grid_photo.setAdapter(mAdapter); grid_photo.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(mContext, "你点击了~" + position + "~项", Toast.LENGTH_SHORT).show(); } }); } }
完毕,运行就是上面的效果了。