GridView实现桌面图标显示案例
作者:砖厂打工仔 时间:2021-12-24 03:23:08
GridView实现桌面图标显示案例,供大家参考,具体内容如下
用法与ListView类似,需要以下几步:
1、定义实体类
2、自定义适配器继承BaseAdapter
3、定义GridView内部布局
效果图:
代码:
实体类:Icon.java
package com.example.a16gridviewtest.entity;
public class Icon {
? ? private int iconId;
? ? private String name;
? ? public Icon(int iconId, String name) {
? ? ? ? this.iconId = iconId;
? ? ? ? this.name = name;
? ? }
? ? public int getIconId() {
? ? ? ? return iconId;
? ? }
? ? public void setIconId(int iconId) {
? ? ? ? this.iconId = iconId;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public void setName(String name) {
? ? ? ? this.name = name;
? ? }
}
自定义适配器:GridViewAdapter.java
package com.example.a16gridviewtest.adpater;
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.a16gridviewtest.R;
import com.example.a16gridviewtest.entity.Icon;
import java.util.List;
public class GridViewAdapter extends BaseAdapter {
? ? private List<Icon> mData;
? ? private Context mContext;
? ? public GridViewAdapter(List<Icon> data, Context context) {
? ? ? ? this.mData = data;
? ? ? ? this.mContext = context;
? ? }
? ? @Override
? ? public int getCount() {
? ? ? ? return mData != null ? mData.size() : 0;
? ? }
? ? @Override
? ? public Object getItem(int position) {
? ? ? ? return mData.get(position);
? ? }
? ? @Override
? ? public long getItemId(int position) {
? ? ? ? return position;
? ? }
? ? @Override
? ? public View getView(int position, View convertView, ViewGroup parent) {
? ? ? ? ViewHolder holder = null;
? ? ? ? if (convertView == null) {
? ? ? ? ? ? holder = new ViewHolder();
? ? ? ? ? ? convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grid_icon, parent, false);
? ? ? ? ? ? holder.img_icon = convertView.findViewById(R.id.img_icon);
? ? ? ? ? ? holder.name = convertView.findViewById(R.id.txt_icon);
? ? ? ? ? ? convertView.setTag(holder);
? ? ? ? } else {
? ? ? ? ? ? holder = (ViewHolder) convertView.getTag();
? ? ? ? }
? ? ? ? holder.img_icon.setImageResource(mData.get(position).getIconId());
? ? ? ? holder.name.setText(mData.get(position).getName());
? ? ? ? return convertView;
? ? }
? ? class ViewHolder {
? ? ? ? private ImageView img_icon;
? ? ? ? private TextView name;
? ? }
}
MainAcitivity.java
public class MainActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.gridview_layout);
? ? ? ? //视图层View
? ? ? ? GridView gridView = findViewById(R.id.gridView);
? ? ? ? //数据源Model
? ? ? ? ArrayList<Icon> list = new ArrayList<>();
? ? ? ? list.add(new Icon(R.mipmap.icon1,"QQ"));
? ? ? ? list.add(new Icon(R.mipmap.icon2,"微信"));
? ? ? ? list.add(new Icon(R.mipmap.icon3,"电话"));
? ? ? ? list.add(new Icon(R.mipmap.icon4,"照片"));
? ? ? ? list.add(new Icon(R.mipmap.icon5,"音乐"));
? ? ? ? list.add(new Icon(R.mipmap.icon6,"Chrome"));
? ? ? ? list.add(new Icon(R.mipmap.icon7,"百度"));
? ? ? ? //控制层Controller
? ? ? ? GridViewAdapter adapter = new GridViewAdapter(list,this);
? ? ? ? gridView.setAdapter(adapter);
? ? ? ? //绑定点击事件
? ? ? ? gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "你点击了第" + (position+1) + "项", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
主布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:id="@+id/gridView"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:horizontalSpacing="10dp"
? ? android:verticalSpacing="10dp"
? ? android:numColumns="3" >
</GridView>
单个GridView布局:
<?xml version="1.0" encoding="utf-8"?>
<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="@mipmap/icon1" />
? ? <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="QQ"
? ? ? ? android:textSize="18sp" />
? ??
</RelativeLayout>
id/img_icon"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:layout_marginTop="30dp"
? ? ? ? android:text="QQ"
? ? ? ? android:textSize="18sp" />
? ??
</RelativeLayout>
来源:https://blog.csdn.net/weixin_42456748/article/details/124899590
标签:GridView,图标
0
投稿
猜你喜欢
springboot结合maven配置不同环境的profile方式
2022-05-28 12:00:16
C#特性-迭代器(上)及一些研究过程中的副产品
2023-12-05 18:26:49
java编码IDEA主题推荐
2021-10-21 03:54:18
springboot @Valid注解对嵌套类型的校验功能
2023-07-01 13:02:18
SpringBoot + Spring Security 基本使用及个性化登录配置详解
2022-03-05 04:43:41
经典排序算法之冒泡排序(Bubble sort)代码
2021-06-08 06:06:18
C# 语言入门基础介绍
2022-03-02 13:32:33
基于java.lang.IllegalArgumentException异常报错问题及解决
2023-09-14 16:03:34
adb通过wifi连接android设备流程解析
2021-12-27 08:39:37
C#微信公众平台开发之高级群发接口
2021-08-19 15:44:28
详解Java实践之建造者模式
2023-01-14 23:03:13
使用spring注入枚举类型作为参数
2023-11-23 13:25:57
Java实现酒店客房管理系统
2023-11-21 06:58:38
MyBatis的逆向工程详解
2022-12-03 11:06:10
springBoot集成Elasticsearch 报错 Health check failed的解决
2022-12-07 05:18:16
Android:利用SharedPreferences实现自动登录
2023-05-24 07:53:33
Springboot 全局时间格式化操作
2022-08-17 03:23:51
Springboot过滤器禁止ip频繁访问功能实现
2022-08-29 11:20:59
Java switch使用原理及实例解析
2023-10-11 20:44:20
Java 数据结构与算法系列精讲之环形链表
2023-04-27 22:37:07