コード例 #1
0
  private View findCenterView(LinearLayoutManager lm) {

    int minDistance = 0;
    View view = null;
    View returnView = null;
    boolean notFound = true;

    for (int i = lm.findFirstVisibleItemPosition();
        i <= lm.findLastVisibleItemPosition() && notFound;
        i++) {

      view = lm.findViewByPosition(i);

      int center =
          lm.getOrientation() == LinearLayoutManager.HORIZONTAL
              ? (view.getLeft() + view.getRight()) / 2
              : (view.getTop() + view.getBottom()) / 2;
      int leastDifference = Math.abs(mCenterPivot - center);

      if (leastDifference <= minDistance || i == lm.findFirstVisibleItemPosition()) {
        minDistance = leastDifference;
        returnView = view;
        setCenterView(returnView);
      } else {
        notFound = false;
      }
    }
    return returnView;
  }
コード例 #2
0
    /**
     * This method takes layout manager and finds the right center element of it
     *
     * @param layoutManager
     * @return View
     */
    private View findCenterView(LinearLayoutManager layoutManager) {

      int minDistance = 0;
      View view, returnView = null;
      boolean notFound = true;

      for (int i = layoutManager.findFirstVisibleItemPosition();
          i <= layoutManager.findLastVisibleItemPosition() && notFound;
          i++) {

        view = layoutManager.findViewByPosition(i);

        int center = (view.getLeft() + view.getRight()) / 2;

        int leastDifference = Math.abs(mCenterPivot - center);

        if (leastDifference <= minDistance || i == layoutManager.findFirstVisibleItemPosition()) {
          minDistance = leastDifference;
          returnView = view;
        } else {
          notFound = false;
        }
      }
      return returnView;
    }
コード例 #3
0
  private void FadingOutOtherItems(PopupMenu popupMenu) {
    // Dim out all the other list items if they exist
    LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
    int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
    int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
    for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
      if (i == mHolder.getLayoutPosition()) continue;
      RecyclerViewHolder viewHolder =
          (RecyclerViewHolder) mRecyclerView.findViewHolderForLayoutPosition(i);
      View itemView = viewHolder.getItemView();
      itemView.clearAnimation();
      itemView.startAnimation(mFadeOut);
    }

    // Make sure to bring them back to normal after the menu is gone
    popupMenu.setOnDismissListener(
        new PopupMenu.OnDismissListener() {
          @Override
          public void onDismiss(PopupMenu popupMenu) {
            LinearLayoutManager layoutManager =
                (LinearLayoutManager) mRecyclerView.getLayoutManager();
            int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
            int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
            for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
              if (i == mHolder.getLayoutPosition()) continue;
              RecyclerViewHolder viewHolder =
                  (RecyclerViewHolder) mRecyclerView.findViewHolderForLayoutPosition(i);
              View itemView = viewHolder.getItemView();
              viewHolder.getLayoutPosition();
              itemView.clearAnimation();
              itemView.startAnimation(mFadeIn);
            }
          }
        });
  }
コード例 #4
0
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
          if (dy == 0 || recyclerView.getChildCount() <= 0) return;

          LinearLayoutManager linearLayoutManager =
              (LinearLayoutManager) recyclerView.getLayoutManager();
          int firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
          int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
          int visibleItemCount = lastVisibleItem - firstVisibleItem;
          int totalItemCount = recyclerView.getAdapter().getItemCount();

          NewsListRecyclerAdapter adapter = (NewsListRecyclerAdapter) recyclerView.getAdapter();

          // Set the item at top to read
          ViewHolder vh =
              (ViewHolder) recyclerView.findViewHolderForLayoutPosition(firstVisibleItem);
          if (vh != null && !vh.shouldStayUnread()) {
            adapter.ChangeReadStateOfItem(vh, true);
          }

          // Check if Listview is scrolled to bottom
          if (lastVisibleItem == (totalItemCount - 1)
              && recyclerView.getChildAt(visibleItemCount).getBottom()
                  <= recyclerView.getHeight()) {
            for (int i = firstVisibleItem + 1; i <= lastVisibleItem; i++) {
              vh = (ViewHolder) recyclerView.findViewHolderForLayoutPosition(i);
              if (vh != null && !vh.shouldStayUnread()) {
                adapter.ChangeReadStateOfItem(vh, true);
              }
            }
          }
        }
  @Override
  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mLinearLayoutManager.getItemCount();
    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

    if (loading) {
      if (totalItemCount > previousTotal) {
        loading = false;
        previousTotal = totalItemCount;
      }
    }
    if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
      // End has been reached

      // Do something
      current_page++;

      onLoadMore(current_page);

      loading = true;
    }
  }
コード例 #6
0
  /**
   * Open item
   *
   * @param position Position of list
   */
  protected void openAnimate(int position) {
    final View child =
        swipeRecyclerView
            .getChildAt(position - mLayoutManager.findFirstVisibleItemPosition())
            .findViewById(swipeFrontView);

    if (child != null) {
      openAnimate(child, position);
    }
  }
コード例 #7
0
 private void closeOtherOpenedItems() {
   if (opened != null && downPosition != SwipeRecyclerView.NO_POSITION) {
     int start = mLayoutManager.findFirstVisibleItemPosition();
     int end = mLayoutManager.findLastVisibleItemPosition();
     for (int i = start; i <= end; i++) {
       if (opened.get(i) && i != downPosition) {
         closeAnimate(swipeRecyclerView.getChildAt(i - start).findViewById(swipeFrontView), i);
       }
     }
   }
 }
コード例 #8
0
 /** Close all opened items */
 void closeOpenedItems() {
   if (opened != null) {
     int start = mLayoutManager.findFirstVisibleItemPosition();
     int end = mLayoutManager.findLastVisibleItemPosition();
     for (int i = start; i <= end; i++) {
       if (opened.get(i)) {
         closeAnimate(swipeRecyclerView.getChildAt(i - start).findViewById(swipeFrontView), i);
       }
     }
   }
 }
コード例 #9
0
  public static int getScrollY(RecyclerView rv) {
    View c = rv.getChildAt(0);
    if (c == null) {
      return 0;
    }

    LinearLayoutManager layoutManager = (LinearLayoutManager) rv.getLayoutManager();
    int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
    int scrollY = -(c.getTop());

    sRecyclerViewItemHeights.put(layoutManager.findFirstVisibleItemPosition(), c.getHeight());

    if (scrollY < 0) scrollY = 0;

    for (int i = 0; i < firstVisiblePosition; ++i) {
      if (sRecyclerViewItemHeights.get(i) != null) // (this is a sanity check)
      scrollY += sRecyclerViewItemHeights.get(i); // add all heights of the views that are gone
    }

    return scrollY;
  }
コード例 #10
0
 /** Unselected choice state in item */
 protected void unselectedChoiceStates() {
   int start = mLayoutManager.findFirstVisibleItemPosition();
   int end = mLayoutManager.findLastVisibleItemPosition();
   for (int i = 0; i < checked.size(); i++) {
     if (checked.get(i) && i >= start && i <= end) {
       reloadChoiceStateInView(
           swipeRecyclerView.getChildAt(i - start).findViewById(swipeFrontView), i);
     }
     checked.set(i, false);
   }
   swipeRecyclerView.onChoiceEnded();
   returnOldActions();
 }
コード例 #11
0
  @Override
  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    if (mLinearLayoutManager == null) {
      mLinearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    }

    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mLinearLayoutManager.getItemCount();

    if (infiniteScrollingEnabled) {
      if (loading) {
        if (totalItemCount > previousTotal) {
          loading = false;
          previousTotal = totalItemCount;
        }
      }

      if (!loading && (totalItemCount - visibleItemCount <= firstVisibleItem + visibleThreshold)) {
        // End has been reached
        // do something
        onLoadMore();
        loading = true;
      }
    }

    if (firstVisibleItem == 0) {
      if (!controlsVisible) {
        onScrollUp();
        controlsVisible = true;
      }

      return;
    }

    if (scrolledDistance > HIDE_THRESHOLD && controlsVisible) {
      onScrollDown();
      controlsVisible = false;
      scrolledDistance = 0;
    } else if (scrolledDistance < -HIDE_THRESHOLD && !controlsVisible) {
      onScrollUp();
      controlsVisible = true;
      scrolledDistance = 0;
    }

    if ((controlsVisible && dy > 0) || (!controlsVisible && dy < 0)) {
      scrolledDistance += dy;
    }
  }
コード例 #12
0
  /**
   * Close item
   *
   * @param position Position of list
   */
  protected void closeAnimate(int position) {
    if (swipeRecyclerView != null) {
      int firstVisibleChildPosition = mLayoutManager.findFirstVisibleItemPosition();
      final View childContainer =
          swipeRecyclerView.getChildAt(position - firstVisibleChildPosition);
      if (childContainer != null) {
        final View child = childContainer.findViewById(swipeFrontView);

        if (child != null) {
          closeAnimate(child, position);
        }
      }
    }
  }
コード例 #13
0
 /**
  * Dismiss an item.
  *
  * @param position is the position of the item to delete.
  * @return 0 if the item is not visible. Otherwise return the height of the cell to dismiss.
  */
 protected int dismiss(int position) {
   opened.remove(position);
   checked.remove(position);
   int start = mLayoutManager.findFirstVisibleItemPosition();
   int end = mLayoutManager.findLastVisibleItemPosition();
   View view = swipeRecyclerView.getChildAt(position - start);
   ++dismissAnimationRefCount;
   if (position >= start && position <= end) {
     performDismiss(view, position, false);
     return view.getHeight();
   } else {
     pendingDismisses.add(new PendingDismissData(position, null));
     return 0;
   }
 }
コード例 #14
0
 private void changeActionBarTitle(int dy) {
   int position = mLayoutManager.findFirstVisibleItemPosition();
   if (lastTitlePos == position) {
     return;
   }
   DailyStoriesAdapter.Item item = mAdapter.getItem(position);
   int type = item.getType();
   if (type == DailyStoriesAdapter.Type.TYPE_HEADER) {
     mTitle = getString(R.string.title_activity_main);
   } else if (dy > 0 && type == DailyStoriesAdapter.Type.TYPE_DATE) {
     mTitle = DateViewHolder.getDate(item.getDate(), getActivity());
   } else if (dy < 0) {
     mTitle = DateViewHolder.getDate(mAdapter.getTitleBeforePosition(position), getActivity());
   }
   ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(mTitle);
   lastTitlePos = position;
 }
コード例 #15
0
  @Override
  public float getScrollPercentage(RecyclerView recyclerView) {
    if (recyclerView == null) return 0;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (!(layoutManager instanceof LinearLayoutManager)) {
      throw new RuntimeException("LlScrollPercentCalculator needs a LinearLayoutManager.");
    }
    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;

    float lastFullyVisiblePosition = linearLayoutManager.findFirstVisibleItemPosition();
    float visibleItems =
        linearLayoutManager.findLastCompletelyVisibleItemPosition()
            - linearLayoutManager.findFirstCompletelyVisibleItemPosition();

    RecyclerView.Adapter adapter = recyclerView.getAdapter();
    if (adapter == null) return 0;
    float itemCount = adapter.getItemCount();
    return lastFullyVisiblePosition / (itemCount - visibleItems);
  }
コード例 #16
0
  private int calculateFirstVisiblePosition(LayoutManager layoutManager) {
    if (layoutManager instanceof LinearLayoutManager) {
      LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
      return linearLayoutManager.findFirstVisibleItemPosition();
    }

    if (layoutManager instanceof StaggeredGridLayoutManager) {
      StaggeredGridLayoutManager staggeredGridLayoutManager =
          (StaggeredGridLayoutManager) layoutManager;
      int[] firstVisibleItemPositions =
          staggeredGridLayoutManager.findFirstVisibleItemPositions(null);

      int min = 10;
      for (int i = 0; i < firstVisibleItemPositions.length; i++) {
        min = Math.min(min, firstVisibleItemPositions[i]);
        Log.e(TAG, "firstVisibleItemPositions" + i + firstVisibleItemPositions[i]);
      }

      return min;
    }

    return -1;
  }
コード例 #17
0
 private void reQueryLocal() {
   try {
     String tag = getArguments().getString(K.KEY_TAG.name());
     Cursor cursor =
         mActivity
             .getContentResolver()
             .query(
                 SchemaPosts.CONTENT_URI,
                 null,
                 SchemaPosts.COLUMN_SRC_SEARCH + "=?",
                 new String[] {tag},
                 BaseColumns._ID + " DESC");
     if (cursor.getCount() > 0) {
       int beforeIdx = mLayoutManager.findFirstVisibleItemPosition();
       mViewAdapter.changeCursor(cursor);
       mViewAdapter.notifyDataSetChanged();
     } else {
       cursor.close();
     }
   } catch (Exception e) {
     // muted;
   }
 }
コード例 #18
0
 public int getPosition() {
   return mLayoutManager.findFirstVisibleItemPosition();
 }
コード例 #19
0
 @Override
 public int getScrollPosition() {
   return viewListLayoutManager.findFirstVisibleItemPosition();
 }
コード例 #20
0
 @Override
 public boolean handleKeyboardShortcutRepeat(
     @NonNull final KeyboardShortcutsHandler handler,
     final int keyCode,
     final int repeatCount,
     @NonNull final KeyEvent event,
     int metaState) {
   final String action = handler.getKeyAction(CONTEXT_TAG_NAVIGATION, keyCode, event, metaState);
   if (action == null) return false;
   final int direction;
   switch (action) {
     case ACTION_NAVIGATION_PREVIOUS:
       {
         direction = -1;
         break;
       }
     case ACTION_NAVIGATION_NEXT:
       {
         direction = 1;
         break;
       }
     case ACTION_NAVIGATION_PAGE_DOWN:
       {
         RecyclerViewUtils.pageScroll(view, manager, 1);
         return true;
       }
     case ACTION_NAVIGATION_PAGE_UP:
       {
         RecyclerViewUtils.pageScroll(view, manager, -1);
         return true;
       }
     default:
       {
         return false;
       }
   }
   final View focusedChild =
       RecyclerViewUtils.findRecyclerViewChild(view, manager.getFocusedChild());
   final int position;
   final int firstVisibleItemPosition = manager.findFirstVisibleItemPosition();
   final int lastVisibleItemPosition = manager.findLastVisibleItemPosition();
   final int itemCount = adapter.getItemCount();
   final boolean backupOutsideRange =
       positionBackup > lastVisibleItemPosition || positionBackup < firstVisibleItemPosition;
   if (focusedChild != null) {
     position = view.getChildLayoutPosition(focusedChild);
   } else if (firstVisibleItemPosition == 0) {
     position = -1;
   } else if (lastVisibleItemPosition == itemCount - 1) {
     position = itemCount;
   } else if (direction > 0 && backupOutsideRange) {
     position = firstVisibleItemPosition;
   } else if (direction < 0 && backupOutsideRange) {
     position = lastVisibleItemPosition;
   } else {
     position = positionBackup;
   }
   positionBackup = position;
   RecyclerViewUtils.focusNavigate(view, manager, position, direction);
   return true;
 }