content: /*
   * Created by LuaView.
   * Copyright (c) 2017, Alibaba Group. All rights reserved.
   *
   * This source code is licensed under the MIT.
   * For the full copyright and license information,please view the LICENSE file in the root directory of this source tree.
   */
  
  package com.taobao.luaview.view.recyclerview;
  
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  
  /**
   * RecyclerView 相关操作
   *
   * @author song
   * @date 16/1/13
   */
  public class RecyclerViewHelper {
  
      /**
       * get first visible position of recycler view
       *
       * @param rv
       * @return
       */
      public static int getFirstVisiblePosition(RecyclerView rv) {
          if (rv != null) {
              final RecyclerView.LayoutManager layoutManager = rv.getLayoutManager();
              if (layoutManager instanceof LinearLayoutManager) {
                  return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
              }
          }
          return 0;
      }
  
      /**
       * get last visible position of recycler view
       *
       * @param rv
       * @return
       */
      public static int getLastVisiblePosition(RecyclerView rv) {
          if (rv != null) {
              final RecyclerView.LayoutManager layoutManager = rv.getLayoutManager();
              if (layoutManager instanceof LinearLayoutManager) {
                  return ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
              }
          }
          return 0;
      }
  
      /**
       * get visible item count of recycler view;
       *
       * @param rv
       * @return
       */
      public static int getVisibleItemCount(RecyclerView rv) {
          final int firstVisiblePos = getFirstVisiblePosition(rv);
          final int lastVisiblePos = getLastVisiblePosition(rv);
          return Math.max(0, lastVisiblePos - firstVisiblePos + 1);
      }
  }
  