我想显示大小可变的小部件列表。
Flutter 的 Wrap 非常适合我的布局需求。
final List<dynamic> someItems;
return SingleChildScrollView(
child: Wrap(
children: someItems.map((item) => _createTile(context, item)).toList(),
),
scrollDirection: Axis.vertical,
);
Run Code Online (Sandbox Code Playgroud)
但是一个致命的问题是,Wrap 不能懒惰地创建小部件。例如,如果上图中显示的 Tile 小部件少于 100 个,但数据列表的数量更多,Wrap 甚至会创建不可见的小部件。
// The result of taking a log once for each item when configuring Wrap with a list of 1000 data.
I/flutter ( 4437): create Widget 0
I/flutter ( 4437): create Widget 1
I/flutter ( 4437): create Widget 2
I/flutter ( 4437): create Widget 3
I/flutter ( 4437): create Widget 4
I/flutter ( …
Run Code Online (Sandbox Code Playgroud) Failed to allocate a 24 byte allocation with 294656 free bytes and 287KB until OOM, target footprint 268435456, growth limit 268435456; failed due to fragmentation (largest possible contiguous allocation 114556928 bytes)
Run Code Online (Sandbox Code Playgroud)
在我服务的应用程序的“正常情况”中(我不使用位图,这是常见的 OOM 中的一个问题。相反,它使用 Http 请求正文,该正文可能非常大),
通过 Android Studio Profiler 检查内存时未检测到内存泄漏。
但对于客户来说,OOM 的情况就像上面的消息一样。(数字略有不同)
在澄清如何调试我的应用程序之前,我想知道该消息的含义。
Failed to allocate a 24 byte allocation
Run Code Online (Sandbox Code Playgroud)
判断尝试创建占用24字节内存的对象失败。
但我不太明白其他字母都在说什么。
Failed to allocate a 24 byte allocation with 294656 free bytes and 287KB until OOM
Run Code Online (Sandbox Code Playgroud)
failed due to fragmentation (largest possible contiguous allocation 114556928 bytes)
Run Code Online (Sandbox Code Playgroud)
有许多文件或文件夹应该包含在特定情况下的项目中,例如在构建时,但用户无法编辑。
在上图中,除了 src、res 文件夹以及应用程序级别的 gradle 文件和 proguard 之外的所有内容都很少被用户编辑。(即使你必须编辑它,它也会在之后再次隐藏)
我想隐藏这些项目,但我不想使用 Android Explorer,因为我想看到每个 f 文件夹的不同层。
有没有办法只查看我想要的文件和文件夹,而不删除实际文件,如下图所示?
从服务器收到的Json拥有此表格。
[
{
"id": 1103333,
"name": "James",
"tagA": [
"apple",
"orange",
"grape"
],
"tagB": [
"red",
"green",
"blue"
],
"tagC": null
},
{
"id": 1103336,
"name": "John",
"tagA": [
"apple",
"pinapple",
"melon"
],
"tagB": [
"black",
"white",
"blue"
],
"tagC": [
"London",
"New York"
]
}
]
Run Code Online (Sandbox Code Playgroud)
一个对象可以具有多个标签,并且一个标签可以与多个对象相关联。
在此列表中,我想找到一个对象,其tagA是苹果或葡萄,而tagB是黑色。
这是我以前写的第一张表。
create table response(id integer primary key, name text not null, tagA text,
tagB text, tagC text)
select * from response where (tagA like '%apple%' or tagA like '%grape%') and (tagB …
Run Code Online (Sandbox Code Playgroud) 上面的代码是RecyclerViewAdapter,仅当它是第一项时才更改颜色,如下所示。
class TestAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val textColor1 = Color.BLACK
private val textColor2 = Color.YELLOW
private val items = ArrayList<String>()
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val textColor = if(position==0) textColor1 else textColor2
holder.itemView.textView.setTextColor(textColor)
holder.itemView.textView.text = items[position]
}
fun move(from:Int,to:Int){
val item = items[from]
items.remove(item)
items.add(to,item)
notifyItemMoved(from,to)
}
}
Run Code Online (Sandbox Code Playgroud)
在这种状态下,我想使用移动功能将值3移动到第一个位置。我想要的结果如下所示。
但实际上,它显示出以下结果
使用notifyDataSetChanged时,看不到动画过渡效果,
使用findViewHolderForAdapterPosition手动运行onBindViewHolder会得到我想要的结果,但是它非常不稳定。(导致我未修复的错误的其他部分)
fun move(from:Int,to:Int){
val item = items[from]
val originTopHolder = recyclerView.findViewHolderForAdapterPosition(0)
val afterTopHolder = recyclerView.findViewHolderForAdapterPosition(from)
items.remove(item)
items.add(to,item)
notifyItemMoved(from,to)
if(to==0){
onBindViewHolder(originTopHolder,1)
onBindViewHolder(afterTopHolder,0)
}
}
Run Code Online (Sandbox Code Playgroud)
还有其他解决方法吗?