我有一个 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
我有一个方法可以将占位符视图插入到 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);
}
}
占位符只是一个不可见的视图,它模拟两个现有视图之间的空间开口:
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)。谢谢!