/*
 * Copyright 2015 Eric Liu
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.liuguangqiang.recyclerview.widget;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * A RecyclerView using LinearLayoutManager.
 * <p>
 * This RecycleView will listening itself whether scroll at the top or bottom.
 * </p>
 * Created by Eric on 15/7/7.
 */
public class LinearRecyclerView extends RecyclerView {

    private boolean isBottom;
    private boolean isTop;

    private int firstVisibleItem;
    private int visibleItemCount;
    private int totalItemCount;

    private LinearLayoutManager layoutManager;
    private OnScrollPositionListener onScrollPositionListener;

    public void setOnScrollPositionListener(OnScrollPositionListener listener) {
        onScrollPositionListener = listener;
    }

    public LinearRecyclerView(Context context) {
        super(context);
        init();
    }

    public LinearRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LinearRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        layoutManager = new LinearLayoutManager(getContext());
        setLayoutManager(layoutManager);
        addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE && onScrollPositionListener != null) {
                    if (isBottom) onScrollPositionListener.onScrollToBottom();
                    if (isTop) onScrollPositionListener.onScrollToTop();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (onScrollPositionListener != null) {
                    visibleItemCount = recyclerView.getChildCount();
                    totalItemCount = layoutManager.getItemCount();
                    firstVisibleItem = layoutManager.findFirstVisibleItemPosition();

                    isBottom = (firstVisibleItem + visibleItemCount) >= (totalItemCount - 1);
                    isTop = firstVisibleItem == 0;
                }
            }
        });
    }

    public interface OnScrollPositionListener {

        void onScrollToTop();

        void onScrollToBottom();

    }

}

@@ -1246,24 +1246,25 @@
 outManager;%0A
+%0A
     private 
@@ -1313,16 +1313,63 @@
 istener;
+%0A    private OnScrollListener onScrollListener;
 %0A%0A    pu
@@ -1487,32 +1487,141 @@
 istener;%0A    %7D%0A%0A
+    public void setOnScrollListener(OnScrollListener listener) %7B%0A        onScrollListener = listener;%0A    %7D%0A%0A
     public Linea
@@ -2539,32 +2539,182 @@
                %7D
+%0A%0A                if (onScrollListener != null) %7B%0A                    onScrollListener.onScrollStateChanged(recyclerView, newState);%0A                %7D
 %0A            %7D%0A%0A
@@ -3247,32 +3247,170 @@
                %7D
+%0A%0A                if (onScrollListener != null) %7B%0A                    onScrollListener.onScrolled(recyclerView, dx, dy);%0A                %7D
 %0A            %7D%0A 
@@ -3475,17 +3475,16 @@
 tener %7B%0A
-%0A
         
@@ -3535,22 +3535,213 @@
 ToBottom();%0A
+    %7D%0A%0A    public interface OnScrollListener %7B%0A        void onScrollStateChanged(RecyclerView recyclerView, int newState);%0A%0A        void onScrolled(RecyclerView recyclerView, int dx, int dy);
 %0A    %7D%0A%0A%7D%0A

Create OnScrollListener for listening the scroll events.