1

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);
}

}

Ezio
  • 723
  • 6
  • 14

1 Answers1

0

If your purpose of accessing newly added view is to make constant smoothscroll speed, this library may be useful for you.
https://github.com/nshmura/SnappySmoothScroller

like this:

layoutManager = new SnappyLinearLayoutManager(context);
layoutManager.setSnapDuration(500);
layoutManager.setSeekDuration(1000);
nshmura
  • 5,940
  • 3
  • 27
  • 46
  • This will have constant speed for all, the newly added view is not visible initially because it is in the bottom and I have to scroll to that position, now since it's is not visible,it is not in the onBindView , so my question was whether we can measure the height of the newly added view which is not visible ? – Ezio Jul 14 '16 at 03:53
  • The height of newly added view is determined in View#onMeasure method. View#onMeasure method is called after RecyclerView.Adapter#onBindView method call. So I think there is no way to measure the height of view before RecyclerView.Adapter#onBindView method call. – nshmura Jul 14 '16 at 07:48