2016-02-29 56 views
1

我使用的是做工精細RecyclerView(FlowLayoutManager)的自定義佈局管理,這是用於鋪設了孩子們的功能:RecyclerView重繪上onLayoutChildren()內容

@Override 
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) 
{ 

    detachAndScrapAttachedViews(recycler); 

    final int count = this.getItemCount(); 
    views.clear(); 
    lines.clear(); 
    for (int i = 0; i < count; i++) { 
     View child = recycler.getViewForPosition(i); 
     addView(child); 
     measureChildWithMargins(child, 0, 0); 

     final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 

     ViewDefinition view = new ViewDefinition(this.config, child); 
     view.setWidth(child.getMeasuredWidth()); 
     view.setHeight(child.getMeasuredHeight()); 
     view.setNewLine(lp.isNewLine()); 
     view.setGravity(lp.getGravity()); 
     view.setWeight(lp.getWeight()); 
     view.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin); 
     views.add(view); 
    } 


    this.config.setMaxWidth(this.getWidth() - this.getPaddingRight() - this.getPaddingLeft()); 
    this.config.setMaxHeight(this.getHeight() - this.getPaddingTop() - this.getPaddingBottom()); 
    this.config.setWidthMode(View.MeasureSpec.EXACTLY); 
    this.config.setHeightMode(View.MeasureSpec.EXACTLY); 
    this.config.setCheckCanFit(true); 


    CommonLogic.fillLines(views, lines, config); 
    CommonLogic.calculateLinesAndChildPosition(lines); 


    int contentLength = 0; 
    final int linesCount = lines.size(); 
    for (int i = 0; i < linesCount; i++) { 
     LineDefinition l = lines.get(i); 
     contentLength = Math.max(contentLength, l.getLineLength()); 
    } 


    LineDefinition currentLine = lines.get(lines.size() - 1); 
    int contentThickness = currentLine.getLineStartThickness() + currentLine.getLineThickness(); 
    int realControlLength = CommonLogic.findSize(this.config.getLengthMode(), this.config.getMaxLength(), contentLength); 
    int realControlThickness = CommonLogic.findSize(this.config.getThicknessMode(), this.config.getMaxThickness(), contentThickness); 


    CommonLogic.applyGravityToLines(lines, realControlLength, realControlThickness, config); 


    for (int i = 0; i < linesCount; i++) { 
     LineDefinition line = lines.get(i); 
     applyPositionsToViews(line); 
    } 
} 

唯一的問題是,當我調用notifyDataSetChanges()或notifyItemChanges()時,它會再次調用onLayoutChildren(),並且整個recyclerview正在更改它的內容,並且它會立即回滾到頂端。

我試着看看LinearLayoutManager如何控制這個問題,但我不能理解我想要的。

如何讓RecyclerView保持當前狀態,但只能更改內容?或者我如何滾動到與我相同的位置?

回答

0

如果要更新特定項目或項目範圍,應使用notifyItemChanged(position)notifyItemRangeChanged(startpos, endpos)。 也有插入方法(notifyItemInserted(pos))和刪除(notifyItemRemoved(pos)