Android Studio的代码笔记--Adapter+GridView学习

news2024/7/6 18:42:07

Adapter+GridView学习

  • Adapter+GridView
    • SimpleAdapter+GridView
      • activity_main.xml
      • appicon.xml
      • MainActivity
    • 自定义BaseAdapter+GridView+已下载应用PackageInfo
      • AppAdapter
      • MainActivity2
  • 其他
    • 获取已下载应用信息函数
    • 获取所有应用信息函数
    • ImageView产生圆角的方法
    • 背景设置很渐变
    • 设置选择效果,按压效果,未选择效果

Adapter+GridView

SimpleAdapter+GridView

SimpleAdapter+GridView 简单显示一组图片和名称

activity_main.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:background="#91F3F0">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        android:layout_marginRight="30dp"
        android:orientation="vertical">

        <GridView
            android:id="@+id/gv_app"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:horizontalSpacing="30dp"
            android:numColumns="6"
            android:verticalSpacing="30dp" />
    </LinearLayout>
</LinearLayout>

android:horizontalSpacing="30dp"横向间隔距离
android:numColumns="6"设置一行中有6列
android:verticalSpacing="30dp"纵向间隔距离

效果
在这里插入图片描述

appicon.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <androidx.cardview.widget.CardView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:layout_marginTop="20dp"
                android:gravity="center"
                app:cardCornerRadius="10dp"
                app:cardElevation="0dp">

                <ImageView
                    android:id="@+id/iv_icon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitCenter" />
            </androidx.cardview.widget.CardView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_icon"
                android:layout_width="wrap_content"
                android:layout_height="23dp"
                android:layout_below="@id/iv_icon"
                android:layout_gravity="center"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp"
                android:gravity="center"
                android:text="name"
                android:textColor="@color/white"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

其中androidx.cardview.widget.CardView使ImageView产生圆角效果

效果

在这里插入图片描述

MainActivity

package com.lxh.dialogtest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity lxh";
    private Context mContext;
    private GridView gv_app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;

        gv_app = (GridView) findViewById(R.id.gv_app);

        int[] appicons = {R.drawable.ic_launcher_background, R.drawable.ic_launcher_background,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_background,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_background,
                R.drawable.ic_launcher_background, R.drawable.ic_launcher_background};
        String[] appnames = {"图标1", "图标2", "图标3", "图标4", "图标5", "图标6", "图标7", "图标8"};

        List<Map<String, Object>> listitems = new ArrayList<>();
        for (int i = 0; i < appnames.length; i++) {
            Map<String, Object> listitem = new HashMap<>();
            listitem.put("icon", appicons[i]);
            listitem.put("name", appnames[i]);
            listitems.add(listitem);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listitems, R.layout.appicon, new String[]{"icon", "name"}, new int[]{R.id.iv_icon, R.id.tv_icon});

        gv_app.setAdapter(simpleAdapter);
        gv_app.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mContext, "点击了第 " + position + " 项", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

效果如图
在这里插入图片描述

自定义BaseAdapter+GridView+已下载应用PackageInfo

activity_main.xml和appicon.xml不变

AppAdapter

package com.lxh.dialogtest;

import android.content.Context;
import android.content.pm.PackageInfo;
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 java.util.List;

/**
 * create by lxh on 2023/12/12 Time:9:42
 * tip:
 */
public class AppAdapter extends BaseAdapter {

    private List<PackageInfo> appList;
    Context context;

    public AppAdapter(List<PackageInfo> appList, Context mcontext) {
        this.appList = appList;
        this.context = mcontext;
    }

    @Override
    public int getCount() {
        return appList.size();
    }

    @Override
    public Object getItem(int position) {
        return appList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.appicon, null);
            holder.appIcon = convertView.findViewById(R.id.iv_icon);
            holder.appName = convertView.findViewById(R.id.tv_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        
        PackageInfo pi = (PackageInfo) getItem(position);
        holder.appIcon.setImageDrawable(pi.applicationInfo.loadIcon(context.getPackageManager()));
        holder.appName.setText(pi.applicationInfo.loadLabel(context.getPackageManager()).toString());
        
        return convertView;
    }

    static class ViewHolder {
        public ImageView appIcon;
        public TextView appName;
    }
}

MainActivity2

package com.lxh.dialogtest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {
    private static final String TAG = "MainActivity2 lxh";
    private Context mContext;
    private GridView gv_app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity2.this;

        gv_app = (GridView) findViewById(R.id.gv_app);

        Appinfo = get_InstallApp(mContext);
        if (Appinfo != null) {
            gv_app.setAdapter(new AppAdapter(Appinfo, mContext));
            gv_app.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(mContext, "你点击了~" + position + "~项", Toast.LENGTH_SHORT).show();
                }
            });
        }

    }

    List<PackageInfo> Appinfo = null;

    public List<PackageInfo> get_InstallApp(Context context) {
        List<PackageInfo> installApp = new ArrayList<>();
        List<PackageInfo> packageInfos = null;
        packageInfos = context.getPackageManager().getInstalledPackages(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);

        for (PackageInfo packageInfo : packageInfos) {
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                installApp.add(packageInfo);
            }
        }

        return installApp;
    }
}

效果如图
在这里插入图片描述

其他

获取已下载应用信息函数

    public List<PackageInfo> get_InstallApp(Context context) {
        List<PackageInfo> installApp = new ArrayList<>();
        List<PackageInfo> packageInfos = null;
        packageInfos = context.getPackageManager().getInstalledPackages(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);

        for (PackageInfo packageInfo : packageInfos) {
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                installApp.add(packageInfo);
            }
        }

        return installApp;
    }

获取所有应用信息函数

    public List<PackageInfo> get_allApp(Context context) {
        List<PackageInfo> installApp = new ArrayList<>();
        List<PackageInfo> packageInfos = null;
        packageInfos = context.getPackageManager().getInstalledPackages(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);

        for (PackageInfo packageInfo : packageInfos) {
            installApp.add(packageInfo);
        }

        return installApp;
    }

ImageView产生圆角的方法

            <androidx.cardview.widget.CardView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:layout_marginTop="20dp"
                android:gravity="center"
                app:cardCornerRadius="10dp"
                app:cardElevation="0dp">

                <ImageView
                    android:id="@+id/iv_icon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitCenter" />
            </androidx.cardview.widget.CardView>

效果
在这里插入图片描述

背景设置很渐变

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="false" android:state_pressed="false">
        <shape>
            <gradient android:angle="270" android:endColor="#9D3DE1" android:startColor="#4379E4" />
        </shape>
    </item>
</selector>

效果
在这里插入图片描述

设置选择效果,按压效果,未选择效果

            <GridView
            android:id="@+id/gv_app"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:horizontalSpacing="30dp"
            android:numColumns="6"
            android:verticalSpacing="30dp"
            android:listSelector="@drawable/cg_white"
            />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <shape>
            <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
            <stroke android:width="2dp" android:color="@color/white" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
            <stroke android:width="2dp" android:color="@color/white" />
        </shape>
    </item>
    <item android:state_focused="false">
        <shape>
            <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
        </shape>
    </item>
</selector>

效果
在这里插入图片描述

未完待续。。。

与君共勉!待续
欢迎指错,一起学习

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1308489.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

二、远程控制树莓派(在用一个Wifi下)

VNC是一个图形桌面共享系统&#xff0c;利用一台计算机或移动设备&#xff08;运行VNC查看器&#xff09;远程控制另一台计算机&#xff08;运行VNC服务器&#xff09;的桌面。 Step1&#xff1a;树莓派&#xff1a;安装VNC服务器&#xff08;树莓派自带&#xff09; 打开方式…

从 Android 手机恢复删除的数据的10个有效工具

您是否曾经在 Android 手机上遇到过数据丢失的情况&#xff0c;即您拍摄的瞬间或其他数据意外丢失&#xff1f; 就我而言&#xff0c;我多次遇到过此类数据丢失的情况&#xff0c;相信我&#xff0c;没有什么比从手机中丢失所有重要数据更严重的了。这就像一场噩梦&#xff0c…

102基于matlab的PCA+ELM和PCA+PSO-ELM的费用估计

基于matlab的PCAELM和PCAPSO-ELM的费用估计&#xff0c;输出输出两者的预测误差并进行比较&#xff0c;输出优化后的迭代曲线。数据可更换自己的&#xff0c;程序已调通&#xff0c;可直接运行。 102matlab PCAPSOELM (xiaohongshu.com)

数据标注公司如何确保数据安全?景联文科技多维度提供保障

数据标注公司通常拥有大量的AI数据和用户数据&#xff0c;保护数据安全是数据标注公司的重要任务。 数据标注公司确保标注数据的安全可以从制度、人员、工具等多个方面入手&#xff0c;建立完善的安全管理体系和审计机制&#xff0c;加强应急预案和备份机制的建立&#xff0c;以…

Docker容器:docker推送镜像至Harbor

目录 1、Harbor创建项目 2、进入test项目&#xff0c;查看推送命令 3、在docker服务器上准备一个镜像 4、修改docker客户端配置 5、重启docker服务 6、docker登录Harbor 7、docker镜像推送到Harbor 1、Harbor创建项目 2、进入test项目&#xff0c;查看推送命令 3、在dock…

路由器静态路由的配置

路由器静态路由的配置步骤如下&#xff1a; 进入系统视图。输入命令sys进入系统视图。配置路由器的接口IP地址。命令格式为int g0/0/0&#xff0c;其中g0/0/0表示路由器的接口&#xff0c;可以根据实际情况进行修改。然后使用命令ip add配置接口的IP地址。配置下一跳地址。在静…

Node.js黑马时钟案例

先上没有使用node.js之前的html部分代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title></title><style>* {margin: 0;padding: 0;}html,body {height: 100%;overflow: hidden;backgrou…

Appium 自动化测试 —— Appium的环境搭建与设置流程!

1、Appium 环境搭建&#xff1a; 学习 appium 最大的难处之一在于环境的安装&#xff0c;安装流程比较繁琐&#xff0c;安装的工具和步骤也较多&#xff0c;以下是基于 Windows 系统下的 Android 手机端的安装流程。就像我们在用 Selenium 进行 web 自动化测试的时候一样&…

RocketMQ源码 Broker-SubscriptionGroupManager 订阅组管理组件源码分析

前言 SubscriptionGroupManager 继承了ConfigManager配置管理组件&#xff0c;拥有将内存数据持久化到磁盘文件subscriptionGroup.json的能力。它主要负责维护所有消费组在内存中的订阅数据。 源码版本&#xff1a;4.9.3 源码架构图 核心数据结构 主要的数据结构比较简单&am…

Java - 异常(三)- 声明异常(throws)和手动抛出异常throw

目录 6.3 方式2&#xff1a;声明异常&#xff08;throws&#xff09; 6.4 手动抛出异常throw 6.4.1 概述 6.4.2 使用格式&#xff1a; 6.4.3 实例代码 6.4.4 为什么要手动抛出异常对象&#xff1f; 6.4.5 如何理解“自动”和“手动” 抛出异常对象 6.4.6 注意点 ❓面试…

@Scheduled任务调度/定时任务-非分布式

1、功能概述 任务调度就是在规定的时间内执行的任务或者按照固定的频率执行的任务。是非常常见的功能之一。常见的有JDK原生的Timer, ScheduledThreadPoolExecutor以及springboot提供的Schduled。分布式调度框架如QuartZ、Elasticjob、XXL-JOB、SchedulerX、PowerJob等。 本文…

GPT4All 本地部署教程

省流&#xff1a;偷懒的可以直接看第二章的GPT4All部署 一. GPT4All README 根据官方网站GPT4All的描述&#xff0c;它是一个开源大型语言模型&#xff0c;可在CPU和几乎任何GPU上本地运行 github source: https://github.com/nomic-ai/gpt4all GPT4All Website and Models…

目标检测检测精度

在一个数据集检测中&#xff0c;会产生四类检测结果&#xff1a;TP、TN 、FP 、FN&#xff1a; T ——true 表示正确 F——false 表示错误 P—— positive 表示积极的&#xff0c;看成正例 N——negative 表示消极的&#xff0c;看成负例 我的理解&#xff1a;后面为预测结…

Postman-脚本自动化及定时执行脚本(7)

一.postman脚本自动化&#xff08;从postman至Newman可以一键执行脚本并生成报告&#xff1a;&#xff09; Postman Newman 是一个 CLI&#xff08;命令行界面&#xff09;工具&#xff0c;可以使用它来运行 Postman 中的集合&#xff08;Collection&#xff09;和环境&#xf…

懒惰的数独——lodash的shuffle方法实现随机打乱的效果

1.效果 2.代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title></title><script src"https://cdn.jsdelivr.net/npm/vue2.5.17/dist/vue.js"></script><script src&q…

案例055:基于微信小程序的四六级词汇

文末获取源码 开发语言&#xff1a;Java 框架&#xff1a;SSM JDK版本&#xff1a;JDK1.8 数据库&#xff1a;mysql 5.7 开发软件&#xff1a;eclipse/myeclipse/idea Maven包&#xff1a;Maven3.5.4 小程序框架&#xff1a;uniapp 小程序开发软件&#xff1a;HBuilder X 小程序…

Matter分析与安全验证

本文作者&#xff1a;杉木涂鸦智能安全实验室 什么是matter Matter是一项智能家居的开源标准&#xff0c;由连接标准联盟制定、认证、推广&#xff0c;该标准基于互联网协议&#xff08;IP&#xff09;&#xff0c;遵循该标准的智能家居设备、移动应用程序和云服务能够进行互…

three.js(一)

文章目录 three.js环境搭建正文补充 示例效果知识点补充1:一个标准的html知识点补充2:原生的前端框架和Vue框架的区别原生的前端框架Vue框架声明式编程和响应式编程 three.js环境搭建 正文 搭建 Three.js 的环境通常包括以下几个步骤&#xff1a; 1.创建项目目录&#xff1a…

【LeetCode每日一题】1904. 你完成的完整对局数

给你两个字符串 startTime 和 finishTime &#xff0c;均符合 "HH:MM" 格式&#xff0c;分别表示你 进入 和 退出 游戏的确切时间&#xff0c;请计算在整个游戏会话期间&#xff0c;你完成的 完整对局的对局数 。 如果 finishTime 早于 startTime &#xff0c;这表示…

一个文件下png,jpg,jpeg,bmp,xml,json,txt文件名称排序命名

#utf-8 #authors 会飞的渔WZH #time:2023-12-13 import os# 要修改的图像所在的文件夹路径 root_path rD:\images\lines2\3 # 要修改的图像所在的文件夹路径filelist os.listdir(root_path) #遍历文件夹 print(len(filelist)) i0for item in filelist:if item.endswith(.…