package com.kaixed.kchat.view.adapter;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

public class CustomLayoutManager extends LinearLayoutManager {

    public CustomLayoutManager(Context context) {
        super(context);
    }

    @Override
    public void scrollToPositionWithOffset(int position, int offset) {
        if (getChildCount() == 0 || getItemCount() == 0) {
            super.scrollToPositionWithOffset(position, offset);
            return;
        }

        if (position < getItemCount()) {
            View firstVisibleView = getChildAt(0);
            int firstItemPosition = getPosition(firstVisibleView);
            int lastItemPosition = getPosition(getChildAt(getChildCount() - 1));

            if (lastItemPosition - firstItemPosition + 1 < getChildCount()) {
                // 如果列表不满一屏，则将列表置于顶部
                super.scrollToPositionWithOffset(position, 0);
            } else {
                // 如果列表满一屏，则将最新的消息显示在底部
                super.scrollToPositionWithOffset(position, Integer.MAX_VALUE);
            }
        } else {
            super.scrollToPositionWithOffset(position, offset);
        }
    }
}
