I am making a chat application . I have a Layout manager which extends LinearLayoutManager and i want to compute the height of the view added to the adapter so that when i call smoothScrollToPosition() then i could change the scroll speed.
For more height i will make MILLISECONDS_PER_INCH to a smaller value and for less height i will make it large.
But i am not able to calculate the height of the newly added view.
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);
}
}