ListView
常用的属性
android:divider 分割线的颜色
数据适配器


三个
BaseAdapter implements ListAdapter, SpinnerAdapter
public class SimpleAdapter extends BaseAdapter
public class ArrayAdapter<T> extends BaseAdapter
SimpleAdapter主要是显示图片加文字的东西例如:
image

相反ArrayAdapter主要适配与TextView控件 顾名思义 显示文字

那咱们来看看两个简单的SimpleAdapter ArrayAdapter的使用

注意注意注意!!!
创建的两个layout文件
一个用于显示ListView 另一个显示里面的内容
image
setContentView(R.layout.activity_first_second_list);
这个加载的是ListView框架
后面SimpleAdapter中的参数才是
SimpleAdapter simpleAdapter = new SimpleAdapter(
FirstActivity.this, things,
R.layout.activity_first,
new String[]{"name", "img"},
new int[]{R.id.tv_name, R.id.img_first});
五个参数大概是:
1.- 上下文(this)
2. 集合
3. resource资源(layout文件)
4. key值
5. 对应的控件(R.id.)

image

ArrayAdapter没啥好说的 三个参数对比TextView控件使用 注意第二个参数就行
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(SecondActivity.this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, things);

有一个小注意点 就是你的图片像素很大话 要放在xxxhdpi中


ListView的优化

使用的是BaseAdapter适配器
主要就是写他的四个抽象方法

@Override
public int getCount() { 条目总数
return 0;
}
@Override
public Object getItem(int position) { 获取对象
return null;
}
@Override
public long getItemId(int position) { 获取id
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) { 获取视图
return null;
}

还正在研究呢 avd虚拟机启动又失败了
https://blog.csdn.net/daidai199505/article/details/108465038
推荐的是删除.lock文件 原因就是你的avd已经打开了 这就要正确关闭虚拟机
但还是打不开的话就删除原虚拟机设备 再来一个 换一个API

至于优化
public View getView(int position, View convertView, ViewGroup parent) { View view = View.inflate(ShopActivity.this,R.layout.activity_shop,null); TextView title = view.findViewById(R.id.title_shop); TextView price = view.findViewById(R.id.tv_price); ImageView imageView = view.findViewById(R.id.img_shop); title.setText(titles[position]); price.setText(prices[position]); imageView.setBackgroundResource(icons[position]); return view; }
就是将convertView(缓存的对象)重复使用来减少对象的创建
class ViewHolder { TextView title, price; ImageView imageView; }
将需要加载的控件变量放在这个类中
然后就是将view都改为convertView
title price imageView都是在ViewHolder中

posted on   不爱美女爱辣条  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?



点击右上角即可分享
微信分享提示