2016-07-11 4 views
1

채팅 응용 프로그램을 만들고 있습니다. LinearLayoutManager를 확장 한 레이아웃 관리자가 있는데, smoothScrollToPosition()을 호출 할 때 스크롤 속도를 변경할 수 있도록 어댑터에 추가 된 뷰의 높이를 계산하고 싶습니다.linearlayout 관리자에서 새로 추가 된보기에 액세스하는 방법

높이를 높이려면 MILLISECONDS_PER_INCH를 더 작은 값으로 만들고 높이를 낮추면 크기가 커집니다.

하지만 새로 추가 된보기의 높이를 계산할 수 없습니다. 새로 추가 된 뷰를 액세스하는 목적은 일정 smoothscroll 속도를 확인하는 것입니다 경우

public class SmoothLinearLayoutManager extends LinearLayoutManager { 

private ChatAdapter mChatAdapter; 
private Context mContext; 
private final float MILLISECONDS_PER_INCH = 250f; 
public SmoothLinearLayoutManager(Context context,ChatAdapter adapter) { 
    super(context); 
    mContext = context; 
    mChatAdapter = adapter; 
} 

public SmoothLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { 
    super(context, orientation, reverseLayout); 
    mContext = context; 
} 

public SmoothLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    mContext = context; 
} 

@Override 
public void smoothScrollToPosition(final RecyclerView recyclerView, RecyclerView.State state, final int position) { 

    Log.d("Scroll",mChatAdapter.getChildHeight()+""); 

    LinearSmoothScroller smoothScroller = new LinearSmoothScroller(mContext) { 
     @Override 
     public PointF computeScrollVectorForPosition(int targetPosition) { 
      return SmoothLinearLayoutManager.this.computeScrollVectorForPosition(position); 
     } 

     @Override 
     protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { 
      return (MILLISECONDS_PER_INCH)/displayMetrics.densityDpi; 
     } 
    }; 
    smoothScroller.setTargetPosition(position); 
    startSmoothScroll(smoothScroller); 
} 

}

답변

0

,이 라이브러리는 당신을 위해 유용 할 수 있습니다. 이 같은
https://github.com/nshmura/SnappySmoothScroller

:이 바닥에 있기 때문에이 모든 일정 속도를해야합니다

layoutManager = new SnappyLinearLayoutManager(context); 
layoutManager.setSnapDuration(500); 
layoutManager.setSeekDuration(1000); 
+0

는 새로 추가 된보기는 처음에 보이지 않는 그것은 이후 지금 그 위치로 스크롤해야 보이지 않습니다, 그것은 onBindView에, 그래서 내 질문은 우리가 볼 수없는 새로 추가 된보기의 높이를 측정 할 수 있는지 여부? – Ezio

+0

새로 추가 된 뷰의 높이는 View # onMeasure 메서드에서 결정됩니다. 보기 onMeasure 메서드는 RecyclerView.Adapter # onBindView 메서드 호출 후 호출됩니다. 그래서 RecyclerView.Adapter # onBindView 메소드 호출 전에 뷰의 높이를 측정 할 방법이 없다고 생각합니다. – nshmura

관련 문제