Java Code Examples for android.support.v7.widget.GridLayoutManager#findFirstVisibleItemPosition()

The following examples show how to use android.support.v7.widget.GridLayoutManager#findFirstVisibleItemPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlbumMediaAdapter.java    From AlbumCameraRecorder with MIT License 6 votes vote down vote up
/**
 * 刷新所能看到的选择
 */
public void refreshSelection() {
    GridLayoutManager layoutManager = (GridLayoutManager) mRecyclerView.getLayoutManager();
    // 获取当前能看到的第一个,和最后一个
    int first = layoutManager.findFirstVisibleItemPosition();
    int last = layoutManager.findLastVisibleItemPosition();
    if (first == -1 || last == -1) {
        // 如果是-1就直接返回
        return;
    }
    // 获取数据源
    Cursor cursor = getCursor();
    for (int i = first; i <= last; i++) {
        RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(first);
        if (holder instanceof MediaViewHolder) {
            if (cursor.moveToPosition(i)) {
                setCheckStatus(MultiMedia.valueOf(cursor), ((MediaViewHolder) holder).mMediaGrid);
            }
        }
    }
}
 
Example 2
Source File: RecyclerViewTouchHelper.java    From YImagePicker with Apache License 2.0 6 votes vote down vote up
/**
 * 获取recyclerView滑动距离
 */
private int getScrollYDistance() {
    if (!(recyclerView.getLayoutManager() instanceof GridLayoutManager)) {
        return 0;
    }
    GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
    int position = gridLayoutManager.findFirstVisibleItemPosition();
    if (position < 0) {
        position = 0;
    }
    View firstVisibleChildView = gridLayoutManager.findViewByPosition(position);
    if (firstVisibleChildView == null) {
        return 0;
    }

    int itemHeight = firstVisibleChildView.getHeight() + PViewSizeUtils.dp(recyclerView.getContext(), 2);
    return (position / getSpanCount()) * itemHeight - firstVisibleChildView.getTop();
}
 
Example 3
Source File: AlbumMediaAdapter.java    From Matisse with Apache License 2.0 6 votes vote down vote up
public void refreshSelection() {
    GridLayoutManager layoutManager = (GridLayoutManager) mRecyclerView.getLayoutManager();
    int first = layoutManager.findFirstVisibleItemPosition();
    int last = layoutManager.findLastVisibleItemPosition();
    if (first == -1 || last == -1) {
        return;
    }
    Cursor cursor = getCursor();
    for (int i = first; i <= last; i++) {
        RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(first);
        if (holder instanceof MediaViewHolder) {
            if (cursor.moveToPosition(i)) {
                setCheckStatus(Item.valueOf(cursor), ((MediaViewHolder) holder).mMediaGrid);
            }
        }
    }
}
 
Example 4
Source File: BasePageIndicator.java    From GridPagerSnapHelper with Apache License 2.0 6 votes vote down vote up
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);

    onPageScrollStateChanged(newState);

    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

        int position = 0;
        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            position = gridLayoutManager.findFirstVisibleItemPosition();
            int row = gridLayoutManager.getSpanCount();
            position = position / (row * mPageColumn);
        } else if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
            position = linearLayoutManager.findFirstVisibleItemPosition();
        }

        onPageSelected(position);
    }
}
 
Example 5
Source File: RecyclerViewTouchHelper.java    From YImagePicker with Apache License 2.0 5 votes vote down vote up
private int getItemHeight() {
    if (!(recyclerView.getLayoutManager() instanceof GridLayoutManager)) {
        return 0;
    }
    GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
    int position = gridLayoutManager.findFirstVisibleItemPosition();
    if (position < 0) {
        position = 0;
    }
    View firstVisibleChildView = gridLayoutManager.findViewByPosition(position);
    if (firstVisibleChildView == null) {
        return 0;
    }
    return firstVisibleChildView.getHeight();
}
 
Example 6
Source File: ChannelFragmentAdapter.java    From letv with Apache License 2.0 5 votes vote down vote up
private void doClickOtherToNavigation(ViewGroup parent, int position, MyViewHolder myHolder) {
    RecyclerView recyclerView = (RecyclerView) parent;
    LayoutManager manager = recyclerView.getLayoutManager();
    View currentView = manager.findViewByPosition(myHolder.getAdapterPosition());
    View preTargetView = manager.findViewByPosition(this.mTopSize);
    GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
    int spanCount = gridLayoutManager.getSpanCount();
    int targetPosition = this.mTopSize + 1;
    if (recyclerView.indexOfChild(preTargetView) >= 0) {
        int targetX = preTargetView.getLeft();
        int targetY = preTargetView.getTop();
        if ((targetPosition - 1) % spanCount == 0) {
            View targetView = manager.findViewByPosition(targetPosition);
            targetX = targetView.getLeft();
            targetY = targetView.getTop();
        } else {
            targetX += preTargetView.getWidth() + UIsUtils.dipToPx(TitleBar.SHAREBTN_RIGHT_MARGIN);
            if (gridLayoutManager.findLastVisibleItemPosition() == getItemCount() - 1 && (((getItemCount() - 1) - this.mTopSize) - 2) % spanCount == 0) {
                if (gridLayoutManager.findFirstVisibleItemPosition() != 0) {
                    targetY += preTargetView.getHeight() + UIsUtils.dipToPx(TitleBar.SHAREBTN_RIGHT_MARGIN);
                } else if (gridLayoutManager.findFirstCompletelyVisibleItemPosition() != 0) {
                    targetY += (-recyclerView.getChildAt(0).getTop()) - recyclerView.getPaddingTop();
                }
            }
        }
        moveOtherToMy(myHolder);
        startAnimation(recyclerView, currentView, (float) targetX, (float) targetY);
        return;
    }
    moveOtherToMy(myHolder);
    int index = (this.mTopSize % spanCount) - 1;
    if (index < 0) {
        index = spanCount - 1;
    }
    startAnimation(recyclerView, currentView, (float) ((currentView.getWidth() * index) + (index == 0 ? 0 : (index - 1) * UIsUtils.dipToPx(TitleBar.SHAREBTN_RIGHT_MARGIN))), 0.0f);
}
 
Example 7
Source File: CoordinatorRecyclerView.java    From InstagramPhotoPicker with MIT License 5 votes vote down vote up
private boolean isScrollTop(MotionEvent ev) {
    LayoutManager layoutManager = getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        if (gridLayoutManager.findFirstVisibleItemPosition() == 0 &&
                gridLayoutManager.findViewByPosition(0).getTop() == gridLayoutManager.getTopDecorationHeight(gridLayoutManager.findViewByPosition(0))) {
            if (!scrollTop) {
                dragDistanceY = (int) (downPositionY - ev.getRawY());
                scrollTop = true;
            }
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: ListReviewFragment.java    From Movie-Check with Apache License 2.0 5 votes vote down vote up
@Override
public void showReviews() {
    linearLayoutAnyFounded.setVisibility(View.GONE);
    linearLayoutLoadFailed.setVisibility(View.GONE);
    recyclerViewReviews.setVisibility(View.VISIBLE);
    final GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 1, GridLayoutManager.VERTICAL, false);
    recyclerViewReviews.setLayoutManager(layoutManager);
    recyclerViewReviews.setItemAnimator(new DefaultItemAnimator());
    listViewAdapter = new ListViewAdapterWithPagination(
            new ReviewListAdapter(this.reviewList, new OnItemClickListener<Review>() {
                @Override
                public void onClick(Review review, View view) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(review.getUrl()));
                    startActivity(browserIntent);
                }

                @Override
                public void onLongClick(Review review, View view) {

                }
            }),
            new OnShowMoreListener() {
                @Override
                public void showMore() {
                    scrollToItem = layoutManager.findFirstVisibleItemPosition();
                    presenter.loadReviews(movie, ++page);
                }
            },
            itensPerPage);
    recyclerViewReviews.setAdapter(listViewAdapter);
    recyclerViewReviews.scrollToPosition(scrollToItem);
}
 
Example 9
Source File: ListNowPlayingMoviesActivity.java    From Movie-Check with Apache License 2.0 5 votes vote down vote up
@Override
public void showMovies(final List<Movie> newMovieList) {
    if (movieList == null) {
        movieList = newMovieList;
    } else {
        movieList.addAll(newMovieList);
    }
    linearLayoutAnyFounded.setVisibility(View.GONE);
    linearLayoutLoadFailed.setVisibility(View.GONE);
    recyclerViewMovies.setVisibility(View.VISIBLE);
    final GridLayoutManager layoutManager = new GridLayoutManager(this, columns, GridLayoutManager.VERTICAL, false);
    recyclerViewMovies.setLayoutManager(layoutManager);
    recyclerViewMovies.setItemAnimator(new DefaultItemAnimator());
    listViewAdapter = new ListViewAdapterWithPagination(
            new NowPlayingMovieListAdapter(movieList, new OnItemClickListener<Movie>() {
                @Override
                public void onClick(Movie movie, View view) {
                    startActivity(MovieProfileActivity.newIntent(ListNowPlayingMoviesActivity.this, movie), ActivityOptionsCompat.makeSceneTransitionAnimation(ListNowPlayingMoviesActivity.this, view.findViewById(R.id.imageview_backdrop), "movieBackdrop").toBundle());
                }

                @Override
                public void onLongClick(Movie movie, View view) {

                }
            }),
            new OnShowMoreListener() {
                @Override
                public void showMore() {
                    scrollToItem = layoutManager.findFirstVisibleItemPosition();
                    presenter.loadMovies(++page);
                }
            },
            itensPerPage);
    recyclerViewMovies.setAdapter(listViewAdapter);
    recyclerViewMovies.scrollToPosition(scrollToItem);
}
 
Example 10
Source File: PullToZoomRecyclerViewEx.java    From PullZoomView with Apache License 2.0 5 votes vote down vote up
private boolean isFirstItemVisible() {
    if (mRootView != null) {
        final RecyclerView.Adapter adapter = mRootView.getAdapter();
        final GridLayoutManager mLayoutmanager = (GridLayoutManager) mRootView.getLayoutManager();


        if (null == adapter || adapter.getItemCount() == 0) {
            return true;
        } else {
            /**
             * This check should really just be:
             * mRootView.getFirstVisiblePosition() == 0, but PtRListView
             * internally use a HeaderView which messes the positions up. For
             * now we'll just add one to account for it and rely on the inner
             * condition which checks getTop().
             */

            int[] into = {0,0};
            if (mLayoutmanager != null)
                into[0] = mLayoutmanager.findFirstVisibleItemPosition();
            if (into.length > 0 && into.length > 0 && into[0] <= 1) {
                final View firstVisibleChild = mRootView.getChildAt(0);
                if (firstVisibleChild != null) {
                    return firstVisibleChild.getTop() >= mRootView.getTop();
                }
            }
        }
    }

    return false;
}
 
Example 11
Source File: SearchMovieActivity.java    From Movie-Check with Apache License 2.0 4 votes vote down vote up
@Override
public void showMovies(final List<Movie> newMovieList) {
    if (movieList == null) {
        movieList = newMovieList;
    } else {
        movieList.addAll(newMovieList);
    }
    linearLayoutAnyFounded.setVisibility(View.GONE);
    linearLayoutLoadFailed.setVisibility(View.GONE);
    recyclerViewMovies.setVisibility(View.VISIBLE);
    final GridLayoutManager layoutManager = new GridLayoutManager(this, columns, GridLayoutManager.VERTICAL, false);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return position >= movieList.size() ? columns : 1;
        }
    });
    recyclerViewMovies.setLayoutManager(layoutManager);
    recyclerViewMovies.setItemAnimator(new DefaultItemAnimator());
    listViewAdapter = new ListViewAdapterWithPagination(
            new MovieListAdapter(movieList, new OnItemClickListener<Movie>() {
                @Override
                public void onClick(Movie movie, View view) {
                    startActivity(MovieProfileActivity.newIntent(SearchMovieActivity.this, movie), ActivityOptionsCompat.makeSceneTransitionAnimation(SearchMovieActivity.this, view.findViewById(R.id.imageview_poster), "moviePoster").toBundle());
                }

                @Override
                public void onLongClick(Movie movie, View view) {

                }
            }
            ),
            new OnShowMoreListener() {
                @Override
                public void showMore() {
                    scrollToItem = layoutManager.findFirstVisibleItemPosition();
                    presenter.search(query, ++page);
                }
            },
            itensPerPage);
    recyclerViewMovies.setAdapter(listViewAdapter);
    recyclerViewMovies.scrollToPosition(scrollToItem);
}
 
Example 12
Source File: ListUpcomingMoviesActivity.java    From Movie-Check with Apache License 2.0 4 votes vote down vote up
@Override
public void showMovies(final List<Movie> newMovieList) {
    if (movieList == null) {
        movieList = newMovieList;
    } else {
        movieList.addAll(newMovieList);
    }
    linearLayoutAnyFounded.setVisibility(View.GONE);
    linearLayoutLoadFailed.setVisibility(View.GONE);
    recyclerViewMovies.setVisibility(View.VISIBLE);
    final GridLayoutManager layoutManager = new GridLayoutManager(this, columns, GridLayoutManager.VERTICAL, false);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return position >= movieList.size() ? columns : 1;
        }
    });
    recyclerViewMovies.setLayoutManager(layoutManager);
    recyclerViewMovies.setItemAnimator(new DefaultItemAnimator());
    listViewAdapter = new ListViewAdapterWithPagination(
            new UpcomingMovieListAdapter(movieList, new OnItemClickListener<Movie>() {

                @Override
                public void onClick(Movie movie, View view) {
                    startActivity(MovieProfileActivity.newIntent(ListUpcomingMoviesActivity.this, movie), ActivityOptionsCompat.makeSceneTransitionAnimation(ListUpcomingMoviesActivity.this, view.findViewById(R.id.imageview_poster), "moviePoster").toBundle());
                }

                @Override
                public void onLongClick(Movie movie, View view) {

                }
            }),
            new OnShowMoreListener() {
                @Override
                public void showMore() {
                    scrollToItem = layoutManager.findFirstVisibleItemPosition();
                    presenter.loadMovies(++page);
                }
            },
            itensPerPage);
    recyclerViewMovies.setAdapter(listViewAdapter);
    recyclerViewMovies.scrollToPosition(scrollToItem);
}
 
Example 13
Source File: ListTopRatedMoviesActivity.java    From Movie-Check with Apache License 2.0 4 votes vote down vote up
@Override
public void showMovies(final List<Movie> newMovieList) {
    if (movieList == null) {
        movieList = newMovieList;
    } else {
        movieList.addAll(newMovieList);
    }
    linearLayoutAnyFounded.setVisibility(View.GONE);
    linearLayoutLoadFailed.setVisibility(View.GONE);
    recyclerViewMovies.setVisibility(View.VISIBLE);
    final GridLayoutManager layoutManager = new GridLayoutManager(this, columns, GridLayoutManager.VERTICAL, false);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return position >= movieList.size() ? columns : 1;
        }
    });
    recyclerViewMovies.setLayoutManager(layoutManager);
    recyclerViewMovies.setItemAnimator(new DefaultItemAnimator());
    listViewAdapter = new ListViewAdapterWithPagination(
            new TopRatedMovieListAdapter(movieList, new OnItemClickListener<Movie>() {
                @Override
                public void onClick(Movie movie, View view) {
                    startActivity(MovieProfileActivity.newIntent(ListTopRatedMoviesActivity.this, movie), ActivityOptionsCompat.makeSceneTransitionAnimation(ListTopRatedMoviesActivity.this, view.findViewById(R.id.imageview_poster), "moviePoster").toBundle());
                }

                @Override
                public void onLongClick(Movie movie, View view) {

                }
            }
            ),
            new OnShowMoreListener() {
                @Override
                public void showMore() {
                    scrollToItem = layoutManager.findFirstVisibleItemPosition();
                    presenter.loadMovies(++page);
                }
            },
            itensPerPage);
    recyclerViewMovies.setAdapter(listViewAdapter);
    recyclerViewMovies.scrollToPosition(scrollToItem);
}
 
Example 14
Source File: SearchPersonActivity.java    From Movie-Check with Apache License 2.0 4 votes vote down vote up
@Override
public void showPerson(final List<Person> personList) {
    if (this.personList == null) {
        this.personList = personList;
    } else {
        this.personList.addAll(personList);
    }
    linearLayoutAnyFounded.setVisibility(View.GONE);
    linearLayoutLoadFailed.setVisibility(View.GONE);
    recyclerViewMovies.setVisibility(View.VISIBLE);
    final GridLayoutManager layoutManager = new GridLayoutManager(this, columns, GridLayoutManager.VERTICAL, false);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return position >= SearchPersonActivity.this.personList.size() ? columns : 1;
        }
    });
    recyclerViewMovies.setLayoutManager(layoutManager);
    recyclerViewMovies.setItemAnimator(new DefaultItemAnimator());
    listViewAdapter = new ListViewAdapterWithPagination(
            new PersonListAdapter(this.personList, new OnItemClickListener<Person>() {
                @Override
                public void onClick(Person person, View view) {
                    startActivity(PersonProfileActivity.newIntent(SearchPersonActivity.this, person), ActivityOptionsCompat.makeSceneTransitionAnimation(SearchPersonActivity.this, view.findViewById(R.id.imageview_photo), "personPhoto").toBundle());
                }

                @Override
                public void onLongClick(Person person, View view) {

                }
            }
            ),
            new OnShowMoreListener() {
                @Override
                public void showMore() {
                    scrollToItem = layoutManager.findFirstVisibleItemPosition();
                    presenter.search(query, ++page);
                }
            },
            itensPerPage);
    recyclerViewMovies.setAdapter(listViewAdapter);
    recyclerViewMovies.scrollToPosition(scrollToItem);
}
 
Example 15
Source File: ListPopularMoviesActivity.java    From Movie-Check with Apache License 2.0 4 votes vote down vote up
@Override
public void showMovies(final List<Movie> newMovieList) {
    if (movieList == null) {
        movieList = newMovieList;
    } else {
        movieList.addAll(newMovieList);
    }
    linearLayoutAnyFounded.setVisibility(View.GONE);
    linearLayoutLoadFailed.setVisibility(View.GONE);
    recyclerViewMovies.setVisibility(View.VISIBLE);
    final GridLayoutManager layoutManager = new GridLayoutManager(this, columns, GridLayoutManager.VERTICAL, false);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return position >= movieList.size() ? columns : 1;
        }
    });
    recyclerViewMovies.setLayoutManager(layoutManager);
    recyclerViewMovies.setItemAnimator(new DefaultItemAnimator());
    listViewAdapter = new ListViewAdapterWithPagination(
            new MovieListAdapter(movieList, new OnItemClickListener<Movie>() {
                @Override
                public void onClick(Movie movie, View view) {
                    startActivity(MovieProfileActivity.newIntent(ListPopularMoviesActivity.this, movie), ActivityOptionsCompat.makeSceneTransitionAnimation(ListPopularMoviesActivity.this, view.findViewById(R.id.imageview_poster), "moviePoster").toBundle());
                }

                @Override
                public void onLongClick(Movie movie, View view) {

                }
            }
            ),
            new OnShowMoreListener() {
                @Override
                public void showMore() {
                    scrollToItem = layoutManager.findFirstVisibleItemPosition();
                    presenter.loadMovies(++page);
                }
            },
            itensPerPage);
    recyclerViewMovies.setAdapter(listViewAdapter);
    recyclerViewMovies.scrollToPosition(scrollToItem);
}