Java Code Examples for android.support.v7.widget.RecyclerView#getAdapter()

The following examples show how to use android.support.v7.widget.RecyclerView#getAdapter() . 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: DefaultDragChecker.java    From DragFooterView with Apache License 2.0 6 votes vote down vote up
private boolean recyclerBottom(RecyclerView recyclerView) {
    RecyclerView.Adapter adapter = recyclerView.getAdapter();
    if (adapter == null || adapter.getItemCount() == 0) {
        return false;
    }

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    int lastCompletelyVisibleItemPosition;
    if (layoutManager instanceof GridLayoutManager || layoutManager instanceof LinearLayoutManager) {
        lastCompletelyVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int[] into = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];
        ((StaggeredGridLayoutManager) layoutManager).findLastCompletelyVisibleItemPositions(into);
        int max = into[0];
        for (int value : into) {
            if (value > max) {
                max = value;
            }
        }
        lastCompletelyVisibleItemPosition = max;
    } else {
        throw new IllegalArgumentException("still not support other LayoutManager");
    }

    return lastCompletelyVisibleItemPosition == adapter.getItemCount() - 1;
}
 
Example 2
Source File: DividerItemDecoration.java    From MvpRoute with Apache License 2.0 6 votes vote down vote up
public void drawHorizontal(Canvas c, RecyclerView parent) {
	final int top = parent.getPaddingTop();
	final int bottom = parent.getHeight() - parent.getPaddingBottom();
	final int childCount = parent.getChildCount();
	BaseAdapter adapter = null;
	if (parent.getAdapter() instanceof BaseAdapter) {
		adapter = (BaseAdapter) parent.getAdapter();
	}
	for (int i = 0; i < childCount; i++) {
		final View child = parent.getChildAt(i);
		int position = parent.getChildAdapterPosition(child);
		if (adapter != null && !adapter.isDrawHeaderFooterLine() && (adapter.isHeaderPos(position) || adapter.isFooterPos(position))) {
			continue;
		}
		final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
		final int left = child.getRight() + params.rightMargin;
		final int right = left + mDividerHeight;
		drawcommonDivider(c, left, right, top, bottom);
	}
}
 
Example 3
Source File: RecyclerViewStateUtils.java    From HeaderAndFooterRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * 设置当前RecyclerView.FooterView的状态
 *
 * @param recyclerView
 * @param state
 */
public static void setFooterViewState(RecyclerView recyclerView, LoadingFooter.State state) {
    RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();
    if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
        if (((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterViewsCount() > 0) {
            LoadingFooter footerView = (LoadingFooter) ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterView();
            footerView.setState(state);
        }
    }
}
 
Example 4
Source File: BounceTouchListener.java    From Bounce with Apache License 2.0 5 votes vote down vote up
private boolean hasHitTop() {
    if (mMainView instanceof ScrollView) {
        ScrollView scrollView = (ScrollView) mMainView;
        return scrollView.getScrollY() == 0;
    } else if (mMainView instanceof ListView) {
        ListView listView = (ListView) mMainView;
        if (listView.getAdapter() != null) {
            if (listView.getAdapter().getCount() > 0) {
                return listView.getFirstVisiblePosition() == 0 &&
                        listView.getChildAt(0).getTop() >= 0;
            }
        }
    } else if (mMainView instanceof RecyclerView) {
        RecyclerView recyclerView = (RecyclerView) mMainView;
        if (recyclerView.getAdapter() != null && recyclerView.getLayoutManager() != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter.getItemCount() > 0) {
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                    return linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0;
                } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                    int[] checks = staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(null);
                    for (int check : checks) {
                        if (check == 0)
                            return true;
                    }
                }
            }
        }
    }

    return false;
}
 
Example 5
Source File: PinnedHeaderDecoration.java    From YPWGame with Apache License 2.0 5 votes vote down vote up
private void updatePinnedHeader(RecyclerView parent) {
    RecyclerView.Adapter adapter = parent.getAdapter();
    if (mAdapter != adapter || mIsAdapterDataChanged) {
        resetPinnedHeader();
        if (mAdapter != null) {
            mAdapter.unregisterAdapterDataObserver(mAdapterDataObserver);
        }

        mAdapter = adapter;
        if (mAdapter != null) {
            mAdapter.registerAdapterDataObserver(mAdapterDataObserver);
        }
    }
}
 
Example 6
Source File: DividerDecoration.java    From fangzhuishushenqi with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view);
    int orientation = 0;
    int headerCount = 0,footerCount = 0;
    if (parent.getAdapter() instanceof RecyclerArrayAdapter){
        headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
        footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
    }

    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof StaggeredGridLayoutManager){
        orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
    }else if (layoutManager instanceof GridLayoutManager){
        orientation = ((GridLayoutManager) layoutManager).getOrientation();
    }else if (layoutManager instanceof LinearLayoutManager){
        orientation = ((LinearLayoutManager) layoutManager).getOrientation();
    }

    if (position>=headerCount&&position<parent.getAdapter().getItemCount()-footerCount||mDrawHeaderFooter){
        if (orientation == OrientationHelper.VERTICAL){
            outRect.bottom = mHeight;
        }else {
            outRect.right = mHeight;
        }
    }
}
 
Example 7
Source File: RecyclerViewUtils.java    From HeaderAndFooterRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * 移除FooterView
 *
 * @param recyclerView
 */
public static void removeFooterView(RecyclerView recyclerView) {

    RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

    if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {

        int footerViewCounter = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterViewsCount();
        if (footerViewCounter > 0) {
            View footerView = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterView();
            ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).removeFooterView(footerView);
        }
    }
}
 
Example 8
Source File: GridItemDecoration.java    From LRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * @param parent     RecyclerView
 * @param pos        当前item的位置
 * @param spanCount  每行显示的item个数
 * @param childCount child个数
 */
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
    LayoutManager layoutManager = parent.getLayoutManager();
    LRecyclerViewAdapter adapter = (LRecyclerViewAdapter) parent.getAdapter();
    if (layoutManager instanceof GridLayoutManager) {
        int leftCount = childCount - childCount % spanCount;//3
        //leftCount:若childCount能被span整除为childCount否则为去掉最后一行的item总数
        if ((pos - adapter.getHeaderViews().size() + 1) > leftCount) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: RecyclerViewStateUtils.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 设置当前RecyclerView.FooterView的状态
 *
 * @param recyclerView
 * @param state
 */
public static void setFooterViewState(RecyclerView recyclerView, LoadingFooter.State state) {
    RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();
    if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
        if (((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterViewsCount() > 0) {
            LoadingFooter footerView = (LoadingFooter) ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterView();
            footerView.setState(state);
        }
    }
}
 
Example 10
Source File: RecyclerViewUtils.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 设置HeaderView
 *
 * @param recyclerView
 * @param view
 */
public static void setHeaderView(RecyclerView recyclerView, View view) {
    RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

    if (outerAdapter == null || !(outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter)) {
        return;
    }

    HeaderAndFooterRecyclerViewAdapter headerAndFooterAdapter = (HeaderAndFooterRecyclerViewAdapter) outerAdapter;
    if (headerAndFooterAdapter.getHeaderViewsCount() == 0) {
        headerAndFooterAdapter.addHeaderView(view);
    }
}
 
Example 11
Source File: LoadMoreView.java    From PullRefreshLoadRecyclerView with MIT License 5 votes vote down vote up
private void computeOffsetWith(RecyclerView recyclerView) {
    if (recyclerView.getAdapter() == null) {
        setTranslationY(getMeasuredHeight());
        return;
    }
    int lastPosition = recyclerView.getAdapter().getItemCount() - 1;
    RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(lastPosition);
    if (viewHolder != null && viewHolder.itemView != null && viewHolder.itemView.isShown()) {
        int scrollRange = recyclerView.computeVerticalScrollRange();
        int currentScroll = recyclerView.computeVerticalScrollOffset();
        int rHeight = recyclerView.getHeight();
        int rPadTop = recyclerView.getPaddingTop();
        int offset = scrollRange - currentScroll - rHeight + getHeight() + rPadTop;

        checkAutoLoad();
        setTranslationY(offset);
        if (offset < 0) {
            dispatchOverScroll(offset, recyclerView.getScrollState());
        }
    } else if (recyclerView.getAdapter().getItemCount() == 0) {//no item
        setTranslationY(-recyclerView.getHeight() / 2 + getMeasuredHeight() / 2);
        checkAutoLoad();

    } else {
        setTranslationY(getMeasuredHeight());
    }
}
 
Example 12
Source File: MoviesListBinding.java    From android-mvvm-sample with MIT License 5 votes vote down vote up
@BindingAdapter("bind:movies")
public static void setMovies(RecyclerView recyclerView, List<Movie> movies) {
    RecyclerView.Adapter adapter = recyclerView.getAdapter();

    if (adapter != null && adapter instanceof MovieAdapter) {
        ((MovieAdapter) adapter).setMovies(movies);
    } else {
        throw new IllegalStateException("RecyclerView either has no adapter set or the "
                + "adapter isn't of type MovieAdapter");
    }
}
 
Example 13
Source File: LuRecyclerViewUtils.java    From LRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * 移除HeaderView
 *
 * @param recyclerView
 */
public static void removeHeaderView(RecyclerView recyclerView) {

    RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

    if (outerAdapter != null && outerAdapter instanceof LuRecyclerViewAdapter) {

        int headerViewCounter = ((LuRecyclerViewAdapter) outerAdapter).getHeaderViewsCount();
        if (headerViewCounter > 0) {
            View headerView = ((LuRecyclerViewAdapter) outerAdapter).getHeaderView();
            ((LuRecyclerViewAdapter) outerAdapter).removeHeaderView(headerView);
        }
    }
}
 
Example 14
Source File: EspressoTools.java    From android-step-by-step with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion hasItemsCount(final int count) {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException e) {
            if (!(view instanceof RecyclerView)) {
                throw e;
            }
            RecyclerView rv = (RecyclerView) view;
            if (rv.getAdapter() == null && count == 0) {
                return;
            }
            Truth.assertThat(rv.getAdapter().getItemCount()).isEqualTo(count);
        }
    };
}
 
Example 15
Source File: BaseFragment.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void createScreenCapture(final RecyclerView recyclerView) {
    if (recyclerView.getAdapter() == null || recyclerView.getAdapter().getItemCount() == 0) {
        ToastUtils.makeToast(R.string.empty_list_to_capture);
        return;
    }
    if (getActivity() == null) return;
    PermissionUtils.checkStoragePermission((BaseActivity) getActivity(), () -> doCapture(recyclerView));
}
 
Example 16
Source File: ScrollingUtil.java    From Pas with Apache License 2.0 5 votes vote down vote up
public static void scrollToBottom(RecyclerView recyclerView) {
    if (recyclerView != null) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (recyclerView.getAdapter() != null && recyclerView.getAdapter().getItemCount() > 0) {
            layoutManager.scrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
        }
    }
}
 
Example 17
Source File: FocusArchivist.java    From dpad-aware-recycler-view with Apache License 2.0 5 votes vote down vote up
/**
 * Find focusable view by item id.
 *
 * @param rv Recycler view.
 * @param id Item's id.
 * @return View with such id, if selection can be restored on it. Null otherwise.
 */
private View findLastFocusedViewById(@NonNull RecyclerView rv, long id) {
    RecyclerView.Adapter adapter = rv.getAdapter();
    if (adapter != null && adapter.hasStableIds() && mLastSelectedId != NO_ID) {
        RecyclerView.ViewHolder viewHolder = rv.findViewHolderForItemId(id);
        if (viewHolder != null) {
            viewHolder.itemView.requestFocus();
            return viewHolder.itemView;
        }
    }

    return null;
}
 
Example 18
Source File: ClickItemTouchListener.java    From MultiView with Apache License 2.0 4 votes vote down vote up
private boolean hasAdapter(RecyclerView hostView) {
    return (hostView.getAdapter() != null);
}
 
Example 19
Source File: ShieldDisplayNodeAdapter.java    From Shield with MIT License 4 votes vote down vote up
public static boolean needOffset(RecyclerView recyclerView) {
    return (recyclerView instanceof ShieldRecyclerViewInterface)
            || (recyclerView != null
            && recyclerView.getAdapter() != null
            && "HeaderViewRecyclerAdapter".equals(recyclerView.getAdapter().getClass().getSimpleName()));
}
 
Example 20
Source File: RecyclerViewStateUtils.java    From GankGirl with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 设置headerAndFooterAdapter的FooterView State
 *
 * @param instance      context
 * @param recyclerView  recyclerView
 * @param pageSize      分页展示时,recyclerView每一页的数量
 * @param state         FooterView State
 * @param errorListener FooterView处于Error状态时的点击事件
 */
public static void setFooterViewState(Activity instance, RecyclerView recyclerView, int pageSize, LoadingFooter.State state, View.OnClickListener errorListener) {

    if(instance==null || instance.isFinishing()) {
        return;
    }

    RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

    if (outerAdapter == null || !(outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter)) {
        return;
    }

    HeaderAndFooterRecyclerViewAdapter headerAndFooterAdapter = (HeaderAndFooterRecyclerViewAdapter) outerAdapter;

    //只有一页的时候,就别加什么FooterView了
    if (headerAndFooterAdapter.getInnerAdapter().getItemCount() < pageSize) {
        return;
    }

    LoadingFooter footerView;

    //已经有footerView了
    if (headerAndFooterAdapter.getFooterViewsCount() > 0) {
        footerView = (LoadingFooter) headerAndFooterAdapter.getFooterView();
        footerView.setState(state);

        if (state == LoadingFooter.State.NetWorkError) {
            footerView.setOnClickListener(errorListener);
        }
        recyclerView.scrollToPosition(headerAndFooterAdapter.getItemCount() - 1);
    } else {
        footerView = new LoadingFooter(instance);
        footerView.setState(state);

        if (state == LoadingFooter.State.NetWorkError) {
            footerView.setOnClickListener(errorListener);
        }

        headerAndFooterAdapter.addFooterView(footerView);
        recyclerView.scrollToPosition(headerAndFooterAdapter.getItemCount() - 1);
    }
}