gpt4 book ai didi

Android RecyclerView 适配器 : notifyItemInserted and notifyItemMoved at index 0 not working

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:57 27 4
gpt4 key购买 nike

我有一个带有水平线性布局管理器的 RecyclerView,声明如下:

RecyclerView graph = (RecyclerView) findViewById(R.id.graph);

RecyclerView.LayoutManager classManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
graph.setLayoutManager(classManager);
graph.addItemDecoration(new ComponentDecorator(this)); //Just sets a margin around each item

我有一个方法可以像这样将占位符 View 插入到 RecyclerView 中:

private void insertPlaceholder(int index) {
int placeholderIndex = getIndexOfPlaceholder(); //returns index of existing placeholder, -1 if none

//No need to do anything
if(placeholderIndex == index)
return;

if(placeholderIndex == -1) {
ClassGraphItem placeholder = new ClassGraphItem();
placeholder.setType(ClassGraphItem.PLACEHOLDER);

mItems.add(index, placeholder);
Print.log("notify item inserted at index", index);
notifyItemInserted(index);
}
else {
ClassGraphItem placeholder = mItems.get(placeholderIndex);
mItems.remove(placeholderIndex);
mItems.add(index, placeholder);

notifyItemMoved(placeholderIndex, index);
}
}

占位符只是一个不可见的 View ,它模拟两个现有 View 之间的空间开口:

private class PlaceholderViewHolder extends RecyclerView.ViewHolder {

public PlaceholderViewHolder(View itemView) {
super(itemView);

ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(mComponentWidth, 1);
itemView.setLayoutParams(params);

itemView.setVisibility(View.INVISIBLE);
}

}

当插入的索引 > 0 时,它完美地工作。但是在索引 0 处,插入占位符或将现有占位符移动到 0 索引都不起作用,特别是 RecyclerView 不会动画显示在索引 0 处插入的新项目。如果我使用 notifyDataSetChanged() 它确实有效。然而,这并没有动画,也不是我正在寻找的效果。这对我来说似乎是一个错误,但我想确保没有其他原因导致此问题。

我使用的是最新版本的 recyclerview 支持库 (24.2.1)。谢谢!

最佳答案

我删除了 recycler.setHasFixedSize(true); 现在它可以工作了。我不知道为什么这会相关。

关于Android RecyclerView 适配器 : notifyItemInserted and notifyItemMoved at index 0 not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39683237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com