效果图:
布局代码:
各item的布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="@+id/img_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerInParent="true"
android:src="@drawable/iv_icon_1" />
<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>
Activity代码:
package com.example.notification;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class BaseadapterActivity extends Activity {
private Context context;
private GridView gridphoto;
private BaseAdapter baseAdapter;
private List<IconActivity> mList = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baseadapteruse);
gridphoto = findViewById(R.id.baseadause);
mList = new ArrayList<IconActivity>();
mList.add(new IconActivity(R.drawable.iv_icon_1, "图标1"));
mList.add(new IconActivity(R.drawable.iv_icon_2, "图标2"));
mList.add(new IconActivity(R.drawable.iv_icon_3, "图标3"));
mList.add(new IconActivity(R.drawable.iv_icon_3, "图标4"));
mList.add(new IconActivity(R.drawable.iv_icon_5, "图标6"));
mList.add(new IconActivity(R.drawable.iv_icon_5, "图标5"));
mList.add(new IconActivity(R.drawable.iv_icon_7, "图标7"));
baseAdapter = new MyAdapter(this, (ArrayList<IconActivity>) mList);
gridphoto.setAdapter(baseAdapter);
}
class MyAdapter extends BaseAdapter {
private ArrayList<IconActivity> adapterList;
private Context context;
public MyAdapter(Context context, ArrayList<IconActivity> adapterList) {
this.context = context;
this.adapterList = adapterList;
}
@Override
public int getCount() {
return mList != null ? adapterList.size() : 0;
}
@Override
public Object getItem(int i) {
return adapterList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Viewholder viewholder = null;
if (view == null) {
viewholder=new Viewholder();
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
view = inflater.inflate(R.layout.basegridviewitem, viewGroup, false);
viewholder.img = (ImageView) view.findViewById(R.id.img_icon);
viewholder.tv = (TextView) view.findViewById(R.id.txt_icon);
view.setTag(viewholder);
} else {
viewholder = (Viewholder) view.getTag();
}
viewholder.img.setImageResource(adapterList.get(i).getiId());
viewholder.tv.setText(adapterList.get(i).getiName());
return view;
}
public class Viewholder {
private TextView tv;
private ImageView img;
}
}
}