2016-02-29 3 views
1

Я использую пользовательский LayoutManager для 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))

Смежные вопросы