本帖最后由 liu 于 2018-12-14 11:07 编辑
GridView
GridView以网格状显示,用法和ListView差不多(http://www.sufeinet.com/thread-24068-1-1.html)
常用属性:
android:columnWidth 每列的宽度
android:numColumns 列数
android:verticalSpacing 行距
android:horizontalSpacing 列距
举例:
主布局
activity_main.xml:
[XML] 纯文本查看 复制代码 <?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="15dp"
android:numColumns="3"
android:verticalSpacing="15dp" />
</LinearLayout>
item布局
item_weapon.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/tv"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
实体类
WeaponBean:[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
/**
* Created by Administrator on 2018/12/13.
*/
public class WeaponBean {
public int img; //图片
public String name; //名字
public WeaponBean(int img, String name) {
this.img = img;
this.name = name;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
适配器
MyAdapter:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.content.Context;
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.List;
/**
* Created by Administrator on 2018/12/13.
*/
public class MyAdapter extends BaseAdapter {
private Context context; //上下文对象
private List<WeaponBean> list; //数据
/**
* @param context 上下文对象
* @param list 要绑定的数据
*/
public MyAdapter(Context context, List<WeaponBean> list) {
this.context = context;
this.list = list;
}
//返回数据的总数
@Override
public int getCount() {
return list.size();
}
//返回 在list中指定位置的数据内容
@Override
public Object getItem(int position) {
return list.get(position);
}
//返回数据在list中的位置
@Override
public long getItemId(int position) {
return position;
}
//设置ListView每一行的布局和数据绑定
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder;
//判断convertView,为空则创建,并设置tag,不为空则通过tag取出ViewHolder,减少findViewById次数,达到优化的目的
if (convertView == null) {
holder = new ViewHolder();
//将布局文件转化为View对象
convertView = LayoutInflater.from(context).inflate(R.layout.item_weapon, null);
//初始化控件
holder.iv = (ImageView) convertView.findViewById(R.id.iv);
holder.tv = (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//将数据绑定到指定控件
holder.iv.setImageResource(list.get(position).img);
holder.tv.setText(list.get(position).name);
return convertView;
}
//用于缓存控件
public class ViewHolder {
private ImageView iv;
private TextView tv;
}
}
主页面
MainActivity:[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
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 {
private GridView mGridView;
private List<WeaponBean> list;
private String[] data = {"苏飞论坛", "HttpHelper万能框架", "分布式系统框架", "HttpHelper爬虫类", "Android", "IOS", "前端", "C#"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addData();
initView();
}
//创建数据
private void addData() {
list = new ArrayList<>();
for (int i = 0; i < data.length; i++) {
WeaponBean weaponBean = new WeaponBean(R.drawable.sflogo, data[i]);
list.add(weaponBean);
}
}
private void initView() {
mGridView = (GridView) findViewById(R.id.gv);
MyAdapter adapter = new MyAdapter(this, list);
//GridView设置适配器
mGridView.setAdapter(adapter);
//监听事件 点击
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "你点击了---" + list.get(i).name, Toast.LENGTH_SHORT).show();
}
});
//监听事件 长按
mGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "你长按了---" + list.get(i).name, Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
效果图:
|