一、简介:
GridView是一个以表格形式显示多张图片等组件。它是按照行列的方式来显示内容的,比如实现九宫格图,用GridView是首选。
二、代码块:
看过我上一篇博客的同学应该知道,一步一步全部步骤写出来是很费时间的,大概流程就那样,所以这次网格视图就直接上代码块了,步骤差不多。
在activity_main.xml中添加一个按钮:
<Button
android:id="@+id/btn_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GridView"
android:textAllCaps="false"/>
MainActivity在原有基础上添加:
先声明控件:
private Button mBtnGridView;
在onCreate:
mBtnGridView=findViewById(R.id.btn_gridview);
在setListeners:
mBtnGridView.setOnClickListener(onClick);
在OnClick:
case R.id.btn_gridview:
//跳转到GridView演示页面
intent = new Intent(MainActivity.this, GridViewActivity.class);
break;
在activity_grid_view.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:padding="15dp">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"/>
</LinearLayout>
在GridViewActivity:
先声明控件:
private GridView mGv;
在onCreate:
mGv=findViewById(R.id.gv);
在之前新建MyGridViewAdapter:
public class MyGridViewAdapter extends BaseAdapter {
@Override
public int getCount() {
return 10;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
return null;
}
}
新建layout_grid_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/iv_grid"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:background="@color/colorPrimaryDark"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:textColor="@color/colorAccent"
android:gravity="center"
android:layout_marginTop="10dp"
/>
</LinearLayout>
在MyGridViewAdapter中修改:
public class MyGridViewAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
public MyGridViewAdapter(Context context){
this.mContext=context;
mLayoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return 10;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
static class ViewHolder{
public ImageView imageView;
public TextView textView;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder=null;
if(view==null){
view=mLayoutInflater.inflate(R.layout.layout_grid_item,null);
holder=new ViewHolder();
holder.imageView=view.findViewById(R.id.iv_grid);
holder.textView=view.findViewById(R.id.tv_title);
view.setTag(holder);
}else {
holder=(ViewHolder)view.getTag();
}
//赋值
holder.textView.setText("ycm");
Glide.with(mContext).load("http://i1.bvimg.com/670191/a72f2a8c0f289d48s.png").into(holder.imageView);
return view;
}
}
在GridViewActivity中添加:
mGv.setAdapter(new MyGridViewAdapter(GridViewActivity.this));
三、运行截图:
设置点击和长按事件之前的ListView中介绍过了,就不介绍了。
原文地址:https://www.cnblogs.com/ycmqaq/p/10035716.html
时间: 2024-10-12 15:37:31