ListView的用法

article/2025/9/26 11:50:07

一、 ListView的使用

<ListView>:用于展示大量数据的一种列表视图,通过上下滑动的方式将屏幕外的数据滚动到屏幕内。
数据无法直接传递给ListView,需要适配器
Adapter:作用是将各种数据以合适的形式展示到View上

实例:

在这里插入图片描述
Food.java:

public class Food {private String name;private String describe;private int imageId;//图片idpublic Food(String name, String describe, int imageId) {this.name = name;this.describe = describe;this.imageId = imageId;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescribe() {return describe;}public void setDescribe(String describe) {this.describe = describe;}public int getImageId() {return imageId;}public void setImageId(int imageId) {this.imageId = imageId;}
}

activity_main

<?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"><ListViewandroid:id="@+id/list_view"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>

food_item.xml:ListView中每一项的布局

<?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"><ImageViewandroid:id="@+id/food_image"android:layout_width="100dp"android:layout_height="100dp"/><TextViewandroid:id="@+id/food_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/food_image"android:layout_marginLeft="20dp"android:layout_marginTop="20dp"android:textSize="20sp"android:textColor="#000000"/><TextViewandroid:id="@+id/describe_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/food_name"android:layout_alignLeft="@+id/food_name"android:layout_marginTop="20dp"android:textSize="15sp"android:textColor="#000000"/>
</RelativeLayout>

FoodAdapter:自定义适配器,通过适配器将要适配的数据传递给ListView

//自定义Adapter继承自BaseAdapter
public class FoodAdapter extends BaseAdapter {private Context context;private List<Food> foodList;public FoodAdapter(Context context, List<Food> foodList){this.context = context;this.foodList = foodList;}@Override//填充的item的个数public int getCount() {return foodList.size();}@Override//指定索引对应的item的数据项public Object getItem(int position) {return null;}@Override//指定索引对应的item的id值public long getItemId(int position) {return position;}@Override//填充每个item的内容public View getView(int position, View convertView, ViewGroup viewGroup) {View view = null;ViewHolder viewHolder = null;if(convertView == null){//加载布局文件,将布局文件转换成View对象view = LayoutInflater.from(context).inflate(R.layout.food_item,null);//创建ViewHolder对象viewHolder = new ViewHolder();//实例化ViewHolderviewHolder.foodImage = view.findViewById(R.id.food_image);viewHolder.foodName = view.findViewById(R.id.food_name);viewHolder.describe = view.findViewById(R.id.describe_text);//将viewHolder的对象存储到View中view.setTag(viewHolder);}else{view = convertView;//取出ViewHolderviewHolder = (ViewHolder)view.getTag();}//给item中各控件赋值viewHolder.foodImage.setImageResource(foodList.get(position).getImageId());viewHolder.foodName.setText(foodList.get(position).getName());viewHolder.describe.setText(foodList.get(position).getDescribe());return view;}
}
//存放item中的所有控件
class ViewHolder{ImageView foodImage;TextView foodName;TextView describe;
}

原理
每个子项被滚动到屏幕内会调用getView()
通过convertView只需加载一次布局
当convertView为null时,动态加载布局。
当convertView不为null时,复用此布局。这样就不需要给滚动到屏幕中的每个item加载一次布局。
通过ViewHolder只需获取一次控件的实例
将所有控件实例都放在ViewHolder里。
当convertView为null时,实例化ViewHolder,调用View的setTag()方法,将ViewHolder对象存储在View中。
当convertView不为null时,调用View的getTag()方法,将ViewHolder取出。这样所有控件实例都缓存到ViewHolder中。这样就不需要每调用一个getView就调用findViewById()方法获取控件了。
只需给ViewHolder中的控件赋值即可

方法详解
public FoodAdapter(Context context, List foodList):适配器构造函数

  • 第一个参数:上下文
  • 第二个参数:要适配的数据

getCount():获得ListView中item的个数
getItem(int position) :指定索引对应的item的数据项
getItemId(int position):指定索引对应的item的id值
getView(int position, View convertView, ViewGroup viewGroup):用于填充每个item的内容

  • 第一个参数:表示进行操作的是哪一个item
  • 第二个参数:用于将之前加载的布局缓存,以便之后进行重用。也是待返回的View信息

setTag(Object tag): 用于给View设置一个标签,标签可以是任何对象。
getTag():取出View里设置的标签
LayoutInflater.from(Context context):创建LayoutInflater对象
inflate(int resource, @Nullable ViewGroup root)::动态加载布局文件

  • 第一个参数:要加载的布局文件的id
  • 第二个参数:给加载好的布局文件添一个父布局

setImageResource(int resId):设置显示的图片
setText(CharSequence text):设置显示的文字
ViewHolder:用来对控件的实例进行缓存,将item中的控件都放在这里

public class MainActivity extends AppCompatActivity{private List<Food> foodList;private Food food;private FoodAdapter adapter;private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();//创建适配器adapter = new FoodAdapter(MainActivity.this,foodList);//ListView绑定适配器listView.setAdapter(adapter);//执行点击事件listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {food = foodList.get(position);Toast.makeText(MainActivity.this,food.getName(),Toast.LENGTH_SHORT).show();}});}private void initView() {listView = (ListView)findViewById(R.id.list_view);//创建集合用来存放foodfoodList = new ArrayList<Food>();//初始化food并添加到集合中foodList.add(new Food("绿茶","绿色的茶",R.drawable.img1));foodList.add(new Food("汉堡","面包加肉",R.drawable.img2));foodList.add(new Food("米饭","中国主食",R.drawable.img3));foodList.add(new Food("寿司","日式料理",R.drawable.img4));foodList.add(new Food("牛排","这是牛排",R.drawable.img5));foodList.add(new Food("蛋糕","这是甜点",R.drawable.img6));foodList.add(new Food("奶茶","离不开的",R.drawable.img7));foodList.add(new Food("披萨","外国主食",R.drawable.img8));}
}

方法详解:
setAdapter(ListAdapter adapter):设置ListView的适配器,通过该方法将ListView与适配器绑定起来
setOnItemClickListener(AdapterView.OnItemClickListener listener):为ListView注册监听器,点击ListView中的每一个子项,都会回调onItemClick()方法。通过onItemClick()方法的position参数可判断出用户点击的是哪一个子项

二、ListView焦点问题

如果在ListView的Item中添加了Button,CheckBox,EditText等控件的话,当点击item会发现, ListView的item点击不了,触发不了onItemClick的方法,也触发不了onItemLongClick方法, 这个就是ListView的一个焦点问题了!就是ListView的焦点被其他控件抢了。
解决办法:
方法一:
布局文件中:为抢占了ListView Item焦点的控件设置android:focusable="false"
方法二:
在代码中:获得控件后调用:setFocusable(false)
方法三:
item根节点设置:android:descendantFocusability="blocksDescendants“
blocksDescendants表示viewgroup会覆盖子类控件而直接获得焦点


http://chatgpt.dhexx.cn/article/NyYrlKmX.shtml

相关文章

Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等

private void showAlerDialog() {AlertDialog dialog new AlertDialog.Builder(this).setTitle("AlerDialog").setMessage("这是一个AlertDialog").setPositiveButton("确定",null).setNegativeButton("取消",null).create();dialog.…

【android学习】Dialog对话框

1&#xff0c;Dialog 1&#xff09;onCreateDialog(int) 2&#xff09;showDialog(int) 第一次请求时&#xff0c;会从Activity中调用onCreateDialog。 3&#xff09;onPrepareDialog(int,Dialog) 在每次打开对话框时被调用。 4&#xff09;dismissDialog(int) 关闭对话…

Android Dialog使用详解

对话框是提示用户作出决定或输入额外信息的小窗口&#xff0c;通常不会填充整个屏幕&#xff0c;用于进行一些额外交互 Dialog 类是对话框的基类&#xff0c;但应该避免直接实例化 Dialog&#xff0c;而应使用其子类&#xff0c;比如 AlertDialog 此对话框可显示标题、提示信…

Android 修改AlertDialog原生setPositiveButton的字体颜色背景颜色大小边距位置

看效果图&#xff1a; public void lanyaClick(View v) {//点击确定之后转向登陆框LayoutInflater factory LayoutInflater.from(Beforestart.this);//得到自定义对话框final View DialogView factory.inflate(R.layout.item_alert_dialog, null);//创建对话框android.app.Al…

setPositiveButton和setNegativeButton的区别

setPositiveButton和setNegativeButton的区别和setNeutralButton的区别 三者都是AlertDialog弹出框的按钮&#xff0c;都是封装好的button&#xff0c;只是显示的位置不同&#xff0c;项目中可根据情况选择使用&#xff0c;setNegativeButton一般用于确认&#xff0c;setNegat…

GPS(rinex格式)数据解析详细解读

RINEX格式现如今已成为GPS测量应用中的标准数据格式&#xff0c;目前应用最为广泛、最普遍的是RINEX格式的第2个版本&#xff0c;该版本能够用于包括静态和动态GPS测量在内的不同观测模式数据。在该版本中定义了6种不同类型的数据文件&#xff0c;分别用于存放不同类型的数据&a…

2020/10/23 GPS的数据格式学习

GPS的数据格式学习 一、在使用GPS的通过串口向电脑发送数据的时候&#xff0c;要注意GPS数据线的连接&#xff1b; 1.1 VCC接VCC&#xff1b;&#xff08;VCC表示接电源正极&#xff09; 1.2 GND接GND&#xff1b;&#xff08;GND表示接地或接电源负极&#xff09; 1.3 TX接RX…

GPS数据包格式+数据解析

GPS数据包格式数据解析 一、全球时区的划分&#xff1a; 每个时区跨15经度。以0经线为界向东向西各划出7.5经度&#xff0c;作为0时区。即0时区的经度范围是7.5W——7.5E。从7.5E与7.5W分别向东、向西每15经度划分为一个时区&#xff0c;直到东11区和西11区。东11区最东部的经度…

GPS研究---GPS 数据格式

GPS 数据处理时所采用的观测数据是来自观测的 GPS 接收机。由于接收机的型号很多&#xff0c;厂商设计的数据格式各不相同&#xff0c;国际上为了能统一使用不同接收机的数据&#xff0c; 设计了一种与接收机无关的 RINEX(The Receiver Independent Exchange Format)格式&#…

GPS数据格式的分析

文章目录 前言一、数据格式解析1、GPGGA2、GPRMC3、GPCHC4、Kitti数据集oxts数据 二、驱动1、功能包1.1 解析GPGGA1.2 华测GPCHC 2、ROS相关消息类型2.1 sensor_msgs::NavSatFix2.2 gps_common::GPSFix2.3 sensor_msgs::Imu 3、驱动思路 三、时间1、UTC时间2、时间戳 前言 GPS…

GPS GLONASS数据文件类型解析

GPS & GLONASS数据文件类型解析 一、GPS数据格式 RINEX格式现如今已成为GPS测量应用中的标准数据格式&#xff0c;目前应用最为广泛、最普遍的是RINEX格式的第2个版本&#xff0c;该版本能够用于包括静态和动态GPS测量在内的不同观测模式数据。在该版本中定义了6种不同类…

GPS的数据格式介绍

GPRMC&#xff08;建议使用最小GPS数据格式&#xff09; $GPRMC,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11><CR><LF> 1) 标准定位时间&#xff08;UTC time&#xff09;格式&#xff1a…

Android ExpandableListView

ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目&#xff0c;这不同于列表视图&#xff08;ListView&#xff09;。ExpandableListView允许有两个层次&#xff1a;一级列表中有二级列表。 比如在手机设置中&#xff0c;对于分类&#xff0c;有很好的效果。手机…

ExpandableListView用法

先上个效果图&#xff1a; 1&#xff0c;我用的fragment import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.dami.student.ui.chatui.adapter.ContactsExpandableListAdapter; import com.dami.student.R; import android.conten…

android expandablelistview简单应用,android ExpandableListView简单例子

android中常常要用到ListView&#xff0c;有时也要用到ExpandableListView&#xff0c;如在手机设置中&#xff0c;对于分类有很好的效果&#xff0c;会用ListView的人一定会用ExpandableListView&#xff0c;因为 ExpandableListView extends ListView的&#xff0c;下面来看个…

android expandablelistview横向,完美实现ExpandableListView二级分栏效果

本文实例为大家分享了ExpandableListView二级分栏效果的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下 对ExpandableListView控件进行封装(未自定义)直接上代码&#xff1a; 通用ViewHolder类&#xff0c;仅在setImageResource中添加代码 package com.svp.haoyan.ex…

android expandablelistview横向,expandableListView 总结

实现效果图&#xff1a; expandableListView groupIndicator 图片默认是在左边&#xff0c;而且比较难看&#xff0c;而我要的是实现groupIndicator 在右边自定义图片&#xff0c; 换图片 最简单的就是直接copy 系统 android:drawable/expander_group ?android:attr/expandab…

Android学习之ExpandableListView

什么是ExpandableListView ExpandableListView是扩展的ListView&#xff0c;继承自ListView&#xff1b;ExpandableListView可以实现点击展开列表&#xff0c;再点击收缩回去的效果。 ExpandableListView的使用 首先需要在主布局文件中声明ExpandableListView&#xff1b; …

ExpandableListView详解

文章目录 效果图ExpandableListView的简介与使用去掉ExpandableListView的箭头以及自定义Indicator解决setOnChildClickListener失效问题解决collapseGroup(i)崩溃问题解决group_item.xml中包含CheckBox、EditText等&#xff0c;点击不能展开的问题 1.效果图 2.ExpandableLi…

values_list()

转载&#xff1a;https://www.cnblogs.com/chenchao1990/p/5311531.html?utm_sourcetuicool&utm_mediumreferral