4.ListView

article/2025/9/26 8:34:34

ListView

文章目录

  • ListView
    • 一、什么是ListView
    • 二、ListView入门
      • 1.ListView核心类
      • 2.代码编写步骤
    • 三、ListView优化
    • 四、把复杂界面(通过xml文件实现)显示到ListView上
      • 1.View的静态方法
      • 2.获取LayoutInflater对象
    • 五、SimpleAdapter & ArrayAdapter的使用
      • 1.ArrayAdapter
      • 2.SimpleAdapter
    • 六、ListView显示数据原理(MVC)

一、什么是ListView

在众多移动应用中,能看到各式各样的表格数据在这里插入图片描述
在这里插入图片描述

在Android中,要实现表格数据展示,最常用的做法就是使用ListView

ListView-支持垂直滚动,而且性能极佳

二、ListView入门

1.ListView核心类

(1)ListView

​ setAdapter 设置一个适配器

​(2)BaseAdapter

​ getcount

​ getItem

​ getId

​ getView

2.代码编写步骤

(1)布局中声明listview节点==(注意:listview的高度不要使用包裹内容)==

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.listiview.MainActivity" ><ListView android:id="@+id/lv_list"android:layout_width="match_parent"android:layout_height="match_parent"android:fastScrollEnabled="true"></ListView></RelativeLayout>

(2)在Activity的oncreate方法中findviewbyid 找到 listView 控件

package com.example.listview;import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.os.Build;public class MainActivity extends Activity {private ListView lv_list;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//[1]找到对应的空间lv_list = (ListView) findViewById(R.id.lv_list);//[2]显示数据 通过一个数据适配器lv_list.setAdapter(new myListAdapter());}//定义一个直接子类 public class myListAdapter extends BaseAdapter{//显示多少个条目@Overridepublic int getCount() {// TODO Auto-generated method stubreturn 10000;}//返回对应的条目内容@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}//返回 对应位置的id@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}//对应item的View@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub
//			TextView tv_text = new TextView(getApplicationContext());
//			tv_text.setText("显示的内容位置:"+position);
//			return tv_text;/*** 如果这么去写 程序会崩溃* 内存溢出 :原因是 垃圾回收机制 没有创建的快* */TextView tv_text;if (convertView == null) {tv_text = new TextView(getApplicationContext());tv_text.setText("convertView == null创建了一个新的对象,显示的内容位置:"+position);}else {tv_text = (TextView) convertView;tv_text.setText("convertView不为空 复用旧的view,复用:"+position);}return tv_text;}}}

三、ListView优化

思路:

重用convertView 判断convertView 是否为空 如果为空则需创建新的view对象 如果不为空则直接使用convertView

Listview的高度 一定是匹配父控件或者指定高度,绝对不允许将高度设置成包裹内容

四、把复杂界面(通过xml文件实现)显示到ListView上

把一个xml文件转化为View对象的几种写法

1.View的静态方法

View.inflate (这种方式比较方便)

2.获取LayoutInflater对象

通过LayoutInflater的inflate方法实现

​ 获取LayoutInflater对象方式一 LayoutInflater inflater = LayoutInflater.from(MainActivity.this);

​ 获取LayoutInflater对象方式二 LayoutInflater inflater2 = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

​ (谷歌源码中采用的这种方式)

		@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View v;if (convertView == null) {//把xml文件转化为 view对象的第一种方式 通过View的inflate方法//第一个参数 上下文//第二个参数 要转化成view对象 对应的布局id//第三个参数 viewGroup 是一个特殊的View对象 它可以加入子view 比如 LinearLayout  RelativeLayout都是ViewGroup//如果这个参数传了值 那么创建出来的view 就是这个view的子view 我们在getview方法中 主要目的是把xml文件转化成view对象 	显示在listview中//不需要加入到其他viewgroup中 所以这个参数传null就可以了//打气筒 将是XML 转换成View 对象 的三种方式v = View.inflate(getApplicationContext(), R.layout.items, null);//LayoutInflater 也有 inflate方法 
//				v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.items, null);//ArrayAdapter源码 采用的这种方式获取的打气筒 通过打气筒把xml布局文件转化为view对象
//				LayoutInflater systemService = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
//				 v=systemService.inflate(resource, root);}else {v = convertView;}return v;}}

五、SimpleAdapter & ArrayAdapter的使用

1.ArrayAdapter

  public class MainActivity2 extends Activity {private String datas[] = new String[]{"张小军","张欣","张丹丹"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView lv_list =  (ListView) findViewById(R.id.lv_list);//第二个参数 用来显示数据的布局文件ID//第三个参数 布局文件中用来展示数据的具体的textview的id//第三个参数 String数组  用来显示的数据源ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item,R.id.tv_test, datas);lv_list.setAdapter(adapter);}
}

2.SimpleAdapter

 public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView lv_list =  (ListView) findViewById(R.id.lv_list);//List<map> 用来填充数据的List<Map<String, String>> data = new ArrayList<Map<String,String>>();Map<String, String> map1 = new HashMap<String, String>();map1.put("title", "中国足球又输了");map1.put("content", "2016冲击失败");data.add(map1);Map<String, String> map2 = new HashMap<String, String>();map2.put("title", "沪android12期高薪就业");map2.put("content", "平均薪水20K");data.add(map2);String[] from ={"title","content"};int[] to = {R.id.tv_title,R.id.tv_content};//第二个参数 数据map的List//第三个参数 用来显示数据的布局文件//第四个参数 String数组 数据List中 每一条数据的 key//第五个参数 数据List中 的文字内容 对应的 要显示此内容的 textView的idSimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item2, from, to);lv_list.setAdapter(adapter);}
}

六、ListView显示数据原理(MVC)

在这里插入图片描述


http://chatgpt.dhexx.cn/article/5TEsXJd7.shtml

相关文章

ListView及ListAdapter详解

ListView及ListAdapter详解 一、AdapterView 1. 简介 An AdapterView is a view whose children are determined by an Adapter. 简单地说就是其子视图是由适配器决定的视图组件 2. 子类 ListViewGridViewSpinnerGallery3. 常用方法 //功能:获取list中指定位置item get…

qt listview

运行图 .h #ifndef WIDGET_H #define WIDGET_H#include <QWidget>#include <QStringListModel> #include <QModelIndex>namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent 0);~Widget();pri…

flutter 之 ListView的使用与详解 map for listview.builder 的使用

1.ListView 配合ListTile 实现新闻列表样式 ListView(children: <Widget>[ListTile(title: const Text(我是 title),subtitle: const Text(我是 sub_title),leading: Image.asset(images/c.png,fit: BoxFit.cover,),trailing: const Icon(Icons.chevron_right,size: 22,…

android ListView

android ListView几个比较特别的属性 由于这两天在做listView的东西&#xff0c;所以整理出来一些我个人认为比较特别的属性&#xff0c;通过设置这样的属性可以做出更加美观的列表 首先是stackFromBottom属性&#xff0c;这只该属性之后你做好的列表就会显示你列表的最下面&am…

ListView使用总结

ListView使用总结 虽然随着RecyclerView的不断普及&#xff0c;相应的资源也越来越多&#xff0c;许多的项目都在使用RecyclerView&#xff0c;但作为他的前辈ListView&#xff0c;加深对ListView的使用有助于我们更好的适应到RecyclerView的使用中。 首先看一下我们实现的效…

Android有关ListView嵌套ListView的一些问题

本人在做评论回复功能的时候&#xff0c;查阅到ListView结合Adapter适配器具有以列表的形式 展示具体数据内容&#xff0c;并且能够根据数据的长度自适应屏幕显示的功能&#xff0c;因此打算在ListView中嵌套ListView完成点击事件后弹出输入框再输入数据后在下方显示回复内容&a…

ListView详解0

ListView常用方法总结 1、listview拖动变黑解决方法 在Android中&#xff0c;ListView是最常用的一个控件&#xff0c;在做UI设计的时候&#xff0c;很多人希望能够改变一下它的背景&#xff0c;使他能够符合整体的UI设计&#xff0c;改变背景背很简单只需要准备一张图片然后指…

C# ListView 的用法

ListView 是一种多列的列表视图控件&#xff0c;可以用于展示多个数据项及其相关信息。ListView 控件提供了丰富的属性和事件&#xff0c;可以用于实现各种各样的表格视图&#xff0c;包括带有单元格编辑、排序和分组等功能。 下面我们通过几个示例来演示如何使用 ListView 控…

Qt ListView使用

概述 Qt中ListView加载数据一般有两种方式&#xff0c;一种是直接qml文件中model直接定义并且放置数据&#xff0c;一种是C代码中定义Model&#xff0c;再setContextProperty的方式暴露给qml域。 步骤 &#xff08;1&#xff09;qml界面 import QtQuick 2.0 import QtQui…

Android控件listview ListView的用法

在Android开发中&#xff0c;ListView是一个比较常用的控件&#xff0c;它以列表的形式展示数据内容&#xff0c;并且能够根据列表的高度自适应屏幕显示。ListView的样式是由属性决定的&#xff0c;它的常用属性如下所示 android:listSelector 点击后改变背景颜色 android:divi…

Android之ListView实现

ListView 用来显示多个可滑动项&#xff08;Item&#xff09;列表的ViewGroup。 需要使用Adapter&#xff08;适配器&#xff09;将集合数据和每一个Item所对应的布局动态适配到ListView中显示 显示列表&#xff1a;listView.setAdapter(adapter) Adapter ArrayAdapter&#xf…

ListView使用方法

ListView使用方法总结 - 直接使用ListView组件创建列表 - 通过Activity继承ListActivity创建 - 定制ListView界面 直接使用ListView组件创建列表 通过数组资源文件指定列表项 先在XML布局文件中添加ListView标志&#xff0c;设置好相关属性&#xff1b;在values下创建数组资…

Android—— ListView 的简单用法及定制ListView界面

一、ListView的简单用法 2. 训练目标 1) 掌握 ListView 控件的使用 2) 掌握 Adapter 桥梁的作用 实现步骤&#xff1a; 1&#xff09;首先新建一个项目&#xff0c; 并让ADT 自动帮我们创建好活动。然后修改activity_main.xml 中的代码&#xff0c;如下所示&#xff1a; &…

QT listView学习

文章目录 listViewdemo说明demo演示model定义委托 QStyledItemDelegate总结 listView listView 对比 tableView 、 treeView来说&#xff0c;最大的不同就是数据结构的不同。treeView是像树一样的层次结构&#xff0c;而listView则就是像链表一样的结构 跟之前的treeView&…

还在用ListView?

还在用Lisview&#xff1f;RecyclerView都已经出来一年多了&#xff01; 想必大家多或多或少的接触过或者了解过RecyclerView&#xff0c;为什么没有用起来&#xff0c;原因大概如下&#xff1f; ListView我用的挺好的&#xff0c;为什么要换RecyclerView&#xff1f;ListView…

ListView用法

ListView是用于显示数据的&#xff0c;先在窗体中拉一个lisview控件&#xff0c;还有一些新增、修改、删除、查询按钮和文本框&#xff0c;控件名称为listview,按钮为btnInsert,btnUpate,btnDeleteOne,btnDelete,btnSelect,文本框的名称为txtName,txtSex,txtPhone,txtAddress,设…

ListView的基础用法

最近学到ListView和RecyclerView&#xff0c;感觉有点难理解&#xff0c;于是自己找到了篇文章&#xff0c;感觉写的挺详细的&#xff08;文章链接在文末&#xff09;&#xff0c;然后自己再整理敲了跑了一遍&#xff0c;总结了一下&#xff0c;方便自己以后回头温习。 一个Li…

Android(14) ArrayAdapter(数组适配器)的三种方法

ArrayAdapter数组适配器用于绑定格式单一的数据&#xff0c;数据源可以是集合或者数组 列表视图(ListView)以垂直的形式列出需要显示的列表项。 实现过程&#xff1a;新建适配器->添加数据源到适配器->视图加载适配器 第一种&#xff1a;直接用ListView组件创建 列表…

Android——列表视图(ListView)

1、什么是ListView&#xff1f;它可以实现怎样的功能&#xff1f; 列表视图是android中最常用的一种视图组件&#xff0c;它以垂直列表的形式列出需要显示的列表项。手机屏幕空间有限&#xff0c;能显示的内容不多。可以借助ListView来显示更多、更丰富的内容。ListView允许用…

ListView详细介绍与使用

前言介绍&#xff1a; 关于 ListView 我们大家都应该是非常的熟悉了&#xff0c;在 Android 开发中是经常用到的&#xff0c;今天就再来回顾一下&#xff0c;ListView 的使用方法&#xff0c;和一些需要优化注意的地方&#xff0c;还有日常开发过程中的一些小技巧和经验。 Li…