RecyclerView decorator adding extra padding on refresh

后端 未结 8 2057
轻奢々
轻奢々 2021-02-01 17:42

So, I\'m facing a weird problem while implementing RecyclerView in my project. I have a custom decorator to implement a consistent top and bottom padding and a rather different

相关标签:
8条回答
  • 2021-02-01 17:48

    You have to remove the item decoration before reseting new data.

    rv.removeItemDecoration(yourDecorator)

    Let me know if it works!

    0 讨论(0)
  • 2021-02-01 17:48

    I had the same issue and I fixed that with the itemDecorationCount

    if (recyclerView.itemDecorationCount == 0) {
                val itemDecorator = DividerItemDecoration(activity, DividerItemDecoration.VERTICAL)
                itemDecorator.setDrawable(ContextCompat.getDrawable(activity, R.drawable.recycler_divider))
                recyclerView.addItemDecoration(itemDecorator)
            }
    
    0 讨论(0)
  • 2021-02-01 17:51

    You are adding Decoration each time recyclerview is loaded.

    Use one flag instead :

    private boolean flagDecoration = false;
    
    if(!flagDecoration)
    {
        rv.addItemDecoration(new RecyclerViewItemDecoration(30, item_count - 1));
        flagDecoration = true;    
    }
    
    0 讨论(0)
  • 2021-02-01 17:53

    Faced the same issue and resolved it by placing

    mLayoutManager = new LinearLayoutManager(current_activity);
    recyclerView.setLayoutManager(mLayoutManager);
    

    in onPreExecute() method.

    Just give it a try if it works

    0 讨论(0)
  • 2021-02-01 17:58

    I face the same issue. Fix it with if check

        if (recyclerView.getItemDecorationAt(0) == null) { // check decoration here
            FlexboxItemDecoration agentDividerItemDecoration = new FlexboxItemDecoration(rv.getContext());
            agentDividerItemDecoration.setDrawable(recyclerView.getContext().getResources().getDrawable(R.drawable.shape_divider_flex_normal));
            recyclerView.addItemDecoration(agentDividerItemDecoration);
        } else {
            Log.i(TAG, "initTagsView: ItemDecoration not null");
        }
    
    0 讨论(0)
  • 2021-02-01 18:06

    I was facing the same issue, the best solution is to make your item decoration while your are initializing your layout manager in the view holder , this resolves the problem you are facing example

    public class GridViewHolder extends ViewHolder {
    
        RecyclerView grid_list;
    
        public GridViewHolder(View v) {
            super(v);
            this.grid_list = (RecyclerView) v.findViewById(R.id.home_screen_item_grid_recycler_view);
            if (grid_list.getLayoutManager() == null) {
                RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 4);
                grid_list.setLayoutManager(mLayoutManager);
                grid_list.addItemDecoration(new GridSpacingItemDecoration(4, 3, false));
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题