Android RecyclerView Adapter: notifyItemInserted and notifyItemMoved at index 0 does not work

I have a RecyclerView with a horizontal linear layout manager declared as follows:

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

I have a method that inserts a Placeholder view into a RecyclerView as follows:

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);
    }
}

A placeholder is simply an invisible view that mimics a space between two existing views:

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);
    }

}

s > 0, . 0 , 0 , , RecyclerView , , 0. notifyDataSetChanged(), . , . , , , .

recyclerview (24.2.1). !

+6
2

recycler.setHasFixedSize(true);, . , , .

+14

- recycler.setHasStableIds(true) getItemId .

, , .

0

Source: https://habr.com/ru/post/1655721/


All Articles