标签:int tle android cap 页面 http obj inflate 赋值
GridView是一个以表格形式显示多张图片等组件。它是按照行列的方式来显示内容的,比如实现九宫格图,用GridView是首选。
看过我上一篇博客的同学应该知道,一步一步全部步骤写出来是很费时间的,大概流程就那样,所以这次网格视图就直接上代码块了,步骤差不多。
<Button
android:id="@+id/btn_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GridView"
android:textAllCaps="false"/>
private Button mBtnGridView;
mBtnGridView=findViewById(R.id.btn_gridview);
mBtnGridView.setOnClickListener(onClick);
case R.id.btn_gridview:
//跳转到GridView演示页面
intent = new Intent(MainActivity.this, GridViewActivity.class);
break;
<?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>
private GridView mGv;
mGv=findViewById(R.id.gv);
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;
}
}
<?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>
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;
}
}
mGv.setAdapter(new MyGridViewAdapter(GridViewActivity.this));
设置点击和长按事件之前的ListView中介绍过了,就不介绍了。
标签:int tle android cap 页面 http obj inflate 赋值
原文地址:https://www.cnblogs.com/ycmqaq/p/10035716.html