Android之ListView

1:简单列表(ArrayAdapter)

1:运行的结果:

2:首先在MyListView里面创建一个按钮,点击的时候进行跳转。

这里让我吃惊的是,Button里面可以直接设置onClick = .java里面的方法。

也即是点击这个按钮之后就会去调用这个方法。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MyListView"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginRight="10dp"android:orientation="vertical"android:paddingLeft="10dp"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="Array列表"android:onClick="toArrayListTest"android:textAllCaps="false"/></LinearLayout>
</ScrollView>

3:在MyListView.java里面编写跳转代码

package com.findyou.mylistview;import android.content.Intent;
import android.os.Bundle;
import android.view.View;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;public class MyListView extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_my_list_view);}public void toArrayListTest(View view) {Intent intent = new Intent(this, ArrayListActivity.class);startActivity(intent);}
}

这里用的也是Intent, 第一个写的这packageContext, 第二个表示你要跳转的目的。然后就开启跳转startActivity(传入intent)。

4:在ArrayListActivity里面先准备好 ListView

<?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"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ArrayListActivity"android:orientation="vertical"><ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>

 5:在ArrayListActivity.java里面编写核心代码

首先去拿到.xml界面里面的ListView,然后创建一个列表进行填充数据。再需要一个适配器,适配器里面需要的参数有,你这个填充的需要什么样的格式?(用的是官方的),以及填充的数据是什么?然后把获得是ListView进行setAdapter,启动。

对ListView里面的Item进行点击和长按监听。里面重写的参数是包括position的。

package com.findyou.mylistview;import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;
import java.util.List;public class ArrayListActivity extends AppCompatActivity {private ListView listView;// 下面这个去提供数据private List<String> mStringList;private ArrayAdapter<String> mArrayAdapter; // 需要一个适配器@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_array_list);listView = findViewById(R.id.lv);// 数据的初始化mStringList = new ArrayList<>();for(int i = 1; i <= 50;  ++ i ) {mStringList.add("这是条目: " + i);}// 第二个参数表示的是 这个的布局是什么?mArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mStringList);listView.setAdapter(mArrayAdapter); // 填充数据// 设置点击的监听listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(ArrayListActivity.this, "你点击了" + position, Toast.LENGTH_SHORT).show();}});// 设置长按的监听listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(ArrayListActivity.this, "你长按了" + position, Toast.LENGTH_SHORT).show();return true;}});}
}

2:simpleList列表(图文)

 1:运行的结果:

2:首先也是写一个activity_simple_list.xml, 写一个大的ListView先撑大

<?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"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".SimpleListActivity"android:orientation="vertical"><ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>

3:在MyListView.java里面补充跳转代码

    public void toSimplArrayListTest(View view) {Intent intent = new Intent(this, SimpleListActivity.class);startActivity(intent);}

4:在SimpleListActivity.java里面配置适配器(adapter)等 

package com.findyou.mylistview;import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
;
import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class SimpleListActivity extends AppCompatActivity {private ListView listView;private SimpleAdapter simpleAdapter;private List<Map<String, Object> >list; // 存放数据的private int[] imags =  {R.drawable.test1,R.drawable.test2,R.drawable.test3,R.drawable.test4,R.drawable.test5,R.drawable.test6,R.drawable.test7,R.drawable.test8,R.drawable.test9,};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_simple_list);listView = findViewById(R.id.lv);list = new ArrayList<>();for(int i = 1; i <= 50; i ++ ) {Map<String, Object> mp = new HashMap<>();mp.put("img", imags[i % 9]);mp.put("title", "标题" + i);mp.put("content", "内容" + i);list.add(mp); // 存放全部的数据}simpleAdapter = new SimpleAdapter(this, list,R.layout.list_item_layout,new String[] {"img", "title", "content"},new int[] {R.id.iv_img, R.id.tv_title, R.id.tv_content});// 上面的倒数两个参数 表示是 从哪到哪 from to, to 表示的是xml里面的idlistView.setAdapter(simpleAdapter);}
}

这里适配器simpleAdapter,里面的参数

this: 表示的是当前

list:  存放数据的(data), 但是这个里面的要求是List<Map<String, *>> 的形式。

R.layout.list_item_layout: 这个是每一个item的布局是什么样的。

 new String[] {"img", "title", "content"}: 表示你data里面的数据注意顺序(from)。

new int[] 表示的R.id,你要插入到的模板里面的哪一个id(to)。

 R.layout.list_item_layout里面的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="10dp"android:paddingTop="5dp"android:paddingRight="10dp"android:paddingBottom="5dp"><ImageViewandroid:id="@+id/iv_img"android:layout_width="100dp"android:layout_height="100dp"android:scaleType="centerCrop"android:src="@drawable/ic_launcher_background"/><TextViewandroid:id="@+id/tv_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/iv_img"android:ellipsize="end"android:maxLines="1"android:text="雨中漫步"android:textSize="20sp"android:textStyle="bold"/><TextViewandroid:id="@+id/tv_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/tv_title"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/iv_img"android:ellipsize="end"android:maxLines="3"android:text="人生就像一场旅行,不必在乎目的地,在乎的是沿途的风景以及看风景的心情,让心灵去旅行"android:textSize="16sp"/></RelativeLayout>

 

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

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

相关文章

Python(十四)

1.type函数和init_subclass_ init_subclass_ 2.元类 类就是用来创建对象的模版&#xff0c;类是由type创造而来的&#xff0c;元类就是创建类的模版&#xff0c;type可以用来创造类&#xff0c;因为type本身就是一个元类&#xff0c;使用元类来创造类&#xff0c;元类之间也有…

当前用户的Git全局配置情况:git config --global --list

通过config命令可以查询当前用户的全局配置情况。这些配置项定义了 Git 在全局范围内的行为&#xff0c;包括如何处理大文件、SSL 证书验证以及提交时的用户信息。 git config --global --list http.sslVerifyfalse 这个配置项禁用了 SSL 证书验证。这在与自签名证书的 Git 服…

负载均衡群集---Haproxy

目录 一、HAproxy 一、概念 二、核心作用 三、主要功能特性 四、应用场景 五、优势与特点 二、 案例分析 1. 案例概述 2. 案例前置知识点 &#xff08;1&#xff09;HTTP 请求 &#xff08;2&#xff09;负载均衡常用调度算法 &#xff08;3&#xff09;常见的 web …

html5视频播放器和微信小程序如何实现视频的自动播放功能

在HTML5中实现视频自动播放需设置autoplay和muted属性&#xff08;浏览器策略要求静音才能自动播放&#xff09;&#xff0c;并可添加loop循环播放、playsinline同层播放等优化属性。微信小程序通过<video>组件的autoplay属性实现自动播放&#xff0c;同时支持全屏按钮、…

OpenHarmony定制系统组合按键(一)

一、开发环境 系统版本&#xff1a;OpenHarmony 4.0.10.13 设备平台&#xff1a;rk3568 SDK版本&#xff1a;fullSDK 4.0.10.13 DevEco Studio版本&#xff1a;4.1.0.400 二、需求背景 定制OpenHarmony 系统组合按键功能&#xff0c;例如仿Android Power VOL_Up组合键实现截…

相机定屏问题分析四:【cameraserver 最大request buffer超标】后置视频模式预览定屏闪退至桌面

【关注我,后续持续新增专题博文,谢谢!!!】 上一篇我们讲了:相机定屏问题分析三:【配流ConfigStream失败】外屏打开相机视频照片人像来回切换后,相机页面卡死,点击没反应9055522 这一篇我们开始讲: 相机定屏问题分析四:【cameraserver 最大request buffer超…

从 PyTorch 到 TensorFlow Lite:模型训练与推理

一、方案介绍 研发阶段&#xff1a;利用 PyTorch 的动态图特性进行快速原型验证&#xff0c;快速迭代模型设计。 灵活性与易用性&#xff1a;PyTorch 是一个非常灵活且易于使用的深度学习框架&#xff0c;特别适合研究和实验。其动态计算图特性使得模型的构建和调试变得更加直…

4.2.5 Spark SQL 分区自动推断

在本节实战中&#xff0c;我们学习了Spark SQL的分区自动推断功能&#xff0c;这是一种提升查询性能的有效手段。通过创建具有不同分区的目录结构&#xff0c;并在这些目录中放置JSON文件&#xff0c;我们模拟了一个分区表的环境。使用Spark SQL读取这些数据时&#xff0c;Spar…

数据结构:导论

目录 什么是“第一性原理”&#xff1f; 什么是“数据结构”&#xff1f; 数据结构解决的根本问题是什么&#xff1f; 数据结构的两大分类 数据结构的基本操作 数据结构与算法的关系 学习数据结构的底层目标 什么是“第一性原理”&#xff1f; 在正式进入数据结构之前&…

汽车制造场景下Profibus转Profinet网关核心功能与应用解析

在当今工业自动化的浪潮中&#xff0c;各种通讯协议层出不穷&#xff0c;而其中PROFIBUS与PROFINET作为两种主流的工业通信标准&#xff0c;它们之间的转换需求日益增长。特别是对于那些希望实现老旧设备与现代化网络无缝对接的企业来说&#xff0c;一个高效、稳定的网关产品显…

qt ubuntu 20.04 交叉编译

一、交叉编译环境搭建 1.下载交叉编译工具链&#xff1a;https://developer.arm.com/downloads/-/gnu-a 可以根据自己需要下载对应版本&#xff0c;当前最新版本是10.3, 笔者使用10.3编译后的glibc.so版本太高&#xff08;glibc_2.3.3, glibc_2.3.4, glibc_2.3.5&#xff09;…

在Babylon.js中创建3D文字:简单而强大的方法

引言 在3D场景中添加文字是许多WebGL项目的常见需求。Babylon.js提供了多种创建3D文字的方法&#xff0c;其中使用TextBlock结合平面网格是一种简单而高效的方式。本文将介绍如何使用Babylon.js的GUI系统在3D空间中创建美观的文字效果。 方法概述 Babylon.js的GUI系统允许我…

油桃TV v20250519 一款电视端应用网站聚合TV播放器 支持安卓4.1

油桃TV v20250519 一款电视端应用网站聚合TV播放器 支持安卓4.1 应用简介&#xff1a; 油桃TV是一款开源电视端应用网站聚合浏览器&#xff0c;它把大家常见需求的一些网站都整合到了这个应用上&#xff0c;并进行了电视端…

Perl单元测试实战指南:从Test::Class入门到精通的完整方案

阅读原文 前言:为什么Perl开发者需要重视单元测试? "这段代码昨天还能运行,今天就出问题了!"——这可能是每位Perl开发者都经历过的噩梦。在没有充分测试覆盖的情况下,即使是微小的改动也可能导致系统崩溃。单元测试正是解决这一痛点的最佳实践,它能帮助我们在…

OpenCv高阶(十三)——人脸检测

文章目录 前言一、人脸检测—haar特征二、人脸检测---级联分类器1、级联分类器2、如何训练级联分类器3、已存在的级联分类器 三、代码分析1、人脸检测的简单使用2、人脸微笑检测&#xff08;1&#xff09; 初始化视频源&#xff08;2&#xff09;主循环处理每一帧&#xff08;3…

无线通信模块简介

QuecPython 是运行在无线通信模块上的开发框架。对于首次接触物联网开发的用户而言&#xff0c;无线通信模块可能是一个相对陌生的概念。本文主要针对无线通信和蜂窝网络本身&#xff0c;以及模块的概念、特性和开发方式进行简要的介绍。 无线通信和蜂窝网络 物联网对无线通信…

Unity 中实现首尾无限循环的 ListView

之前已经实现过&#xff1a; Unity 中实现可复用的 ListView-CSDN博客文章浏览阅读5.6k次&#xff0c;点赞2次&#xff0c;收藏27次。源码已放入我的 github&#xff0c;地址&#xff1a;Unity-ListView前言实现一个列表组件&#xff0c;表现方面最核心的部分就是重写布局&…

【C++】 类和对象(上)

1.类的定义 1.1类的定义格式 • class为定义类的关键字&#xff0c;后跟一个类的名字&#xff0c;{}中为类的主体&#xff0c;注意类定义结束时后⾯分号不能省 略。类体中内容称为类的成员&#xff1a;类中的变量称为类的属性或成员变量;类中的函数称为类的⽅法或 者成员函数。…

Transformer架构详解:从Attention到ChatGPT

Transformer架构详解&#xff1a;从Attention到ChatGPT 系统化学习人工智能网站&#xff08;收藏&#xff09;&#xff1a;https://www.captainbed.cn/flu 文章目录 Transformer架构详解&#xff1a;从Attention到ChatGPT摘要引言一、Attention机制&#xff1a;Transformer的…

Rock9.x(Linux)安装Redis7

&#x1f49a;提醒&#xff1a;1&#xff09;注意权限问题 &#x1f49a; 查是否已经安装了gcc gcc 是C语言编译器&#xff0c;Redis是用C语言开发的&#xff0c;我们需要编译它。 gcc --version如果没有安装gcc&#xff0c;那么我们手动安装 安装GCC sudo dnf -y install…