1.常用属性
2.Adapter接口
3.Demo演示
今天观看了GridView的相关视频,并且根据案例,进行了代码的编写和实例
新建GridViewActivity.java继承AppCompatActivity.java:
package com.example.revrse.gridview; import android.os.Bundle; import android.os.PersistableBundle; import android.widget.GridView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.example.revrse.R; public class GridViewActivity extends AppCompatActivity { private GridView mGv; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gridview); mGv=(GridView)findViewById(R.id.gv); } }
新建MyGrideViewActivity.java继承BaseAdapter:
package com.example.revrse.gridview; 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 com.example.revrse.R; public class MyGridViewActivity extends BaseAdapter { private Context mContext; private LayoutInflater mLayoutInflater; public MyGridViewActivity(Context context){ this.mContext=context; mLayoutInflater =LayoutInflater.from(context); } @Override public int getCount() { return 10; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } static class ViewHolder{ public ImageView imageView; public TextView textView; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder=null; if(convertView==null) { convertView = mLayoutInflater.inflate(R.layout.activity_gridview_item,null); holder=new ViewHolder(); holder.imageView=convertView.findViewById(R.id.iv_grid); holder.textView=convertView.findViewById(R.id.tv_title); convertView.setTag(holder); } else{ holder=(ViewHolder)convertView.getTag(); } holder.textView.setText("加油"); return convertView; } }
activity_gridview.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: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>
activity_gridview_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="#899000"/> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Come on!" android:gravity="center" android:textColor="#89178619"/> </LinearLayout>
学习这方面的代码时,其中知道了Glide的相关知识,用Glide加载网络图片,根据网上查阅资料得知,有的android不支持使用Glide。
原文地址:https://www.cnblogs.com/hhjing/p/12273763.html
时间: 2024-10-12 17:06:54