package com.tencent.qcloud.tuikit.tuichat.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class CustomRecyclerView extends RecyclerView {

    public CustomRecyclerView(Context context) {
        super(context);
        addOnScrollListener();
    }

    public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        addOnScrollListener();
    }

    private void addOnScrollListener() {

        super.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                        GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
                        int visibleItemCount = gridLayoutManager.getChildCount();
                        int totalItemCount = gridLayoutManager.getItemCount();
                        int firstVisibleItem = gridLayoutManager.findFirstVisibleItemPosition();
                        int lastVisibleItem = firstVisibleItem + visibleItemCount;
                        if (lastVisibleItem == totalItemCount) {
                            requestDisallowInterceptTouchEvent(true);
                        } else {
                            requestDisallowInterceptTouchEvent(false);
                        }
                        Log.d("11111", visibleItemCount + ";" + totalItemCount + ";" + firstVisibleItem + ";" + lastVisibleItem);
                    }
                }
            }
        });
    }
}
