public void setAdapter(Adapter adapter) { if (recyclerView == null) initViews(); Adapter oldAdapter = recyclerView.getAdapter(); if (oldAdapter != null) { oldAdapter.unregisterAdapterDataObserver(emptyObserver); } recyclerView.setAdapter(adapter); if (adapter != null) { adapter.registerAdapterDataObserver(emptyObserver); if (adapter.getItemCount() > 0) { // only show data if adapter has data already // otherwise progress bar is shown emptyObserver.onChanged(); } } }
public void startPeriodicUpdate() { if (recyclerView == null || recyclerView.getAdapter() == null) { throw new IllegalStateException("Need to call setAdapter() first!"); } refresher = new Runnable() { @Override public void run() { LOG.info("Updating Content..."); Adapter adapter = recyclerView.getAdapter(); adapter.notifyItemRangeChanged(0, adapter.getItemCount()); handler.postDelayed(refresher, MIN_DATE_RESOLUTION); } }; LOG.info("Adding Handler Callback"); handler.postDelayed(refresher, MIN_DATE_RESOLUTION); }
/** * @Description: 判断第一个条目是否完全可见 * * @return boolean: * @version 1.0 * @date 2015-9-23 * @Author zhou.wenkai */ private boolean isFirstItemVisible() { final Adapter<?> adapter = getRefreshableView().getAdapter(); // 如果未设置Adapter或者Adapter没有数据可以下拉刷新 if (null == adapter || adapter.getItemCount() == 0) { if (DEBUG) { Log.d(LOG_TAG, "isFirstItemVisible. Empty View."); } return true; } else { // 第一个条目完全展示,可以刷新 if (getFirstVisiblePosition() == 0) { return mRefreshableView.getChildAt(0).getTop() >= mRefreshableView .getTop(); } } return false; }
/** * @Description: 判断最后一个条目是否完全可见 * * @return boolean: * @version 1.0 * @date 2015-9-23 * @Author zhou.wenkai */ private boolean isLastItemVisible() { final Adapter<?> adapter = getRefreshableView().getAdapter(); // 如果未设置Adapter或者Adapter没有数据可以上拉刷新 if (null == adapter || adapter.getItemCount() == 0) { if (DEBUG) { Log.d(LOG_TAG, "isLastItemVisible. Empty View."); } return true; } else { // 最后一个条目View完全展示,可以刷新 int lastVisiblePosition = getLastVisiblePosition(); if(lastVisiblePosition >= mRefreshableView.getAdapter().getItemCount()-1) { return mRefreshableView.getChildAt( mRefreshableView.getChildCount() - 1).getBottom() <= mRefreshableView .getBottom(); } } return false; }
/** * Defines the choice behavior for the List. By default, Lists do not have any choice behavior * ({@link ChoiceMode#NONE}). By setting the choiceMode to {@link ChoiceMode#SINGLE}, the * List allows up to one item to be in a chosen state. By setting the choiceMode to * {@link ChoiceMode#MULTIPLE}, the list allows any number of items to be chosen. * * @param choiceMode One of {@link ChoiceMode#NONE}, {@link ChoiceMode#SINGLE}, or * {@link ChoiceMode#MULTIPLE} */ public void setChoiceMode(ChoiceMode choiceMode) { if (mChoiceMode == choiceMode) { return; } mChoiceMode = choiceMode; if (mChoiceMode != ChoiceMode.NONE) { if (mCheckedStates == null) { mCheckedStates = new CheckedStates(); } final Adapter adapter = mRecyclerView.getAdapter(); if (mCheckedIdStates == null && adapter != null && adapter.hasStableIds()) { mCheckedIdStates = new CheckedIdStates(); } } }
@Override public boolean validAdapter(Adapter adapter) { if (adapter != null) { RecyclerView.ViewHolder viewHolder = adapter.createViewHolder(mTicklableRecyclerView, adapter.getItemViewType(0)); if (!(viewHolder instanceof ViewHolder)) { String msg = "adapter's ViewHolder should be instance of FocusableLinearLayoutManager.ViewHolder"; if (DesignConfig.DEBUG) { throw new IllegalArgumentException(msg); } else { Log.w(TAG, msg); return false; } } } return true; }
private void setupEmptyViewObserver(RecyclerView.Adapter paramAdapter) { if (paramAdapter != null) { if (this.mObserver != null) { paramAdapter.unregisterAdapterDataObserver(this.mObserver); this.mObserver = null; } this.mObserver = new RecyclerView.AdapterDataObserver() { public final void onChanged() { PlayRecyclerView.this.updateEmptyStatus(); } }; paramAdapter.registerAdapterDataObserver(this.mObserver); } }
/** * Initializes the RecyclerView. * * @param adapter The adapter that provides a binding from an app-specific data set to views * that are displayed within the RecyclerView. * @return The RecyclerView itself. */ public RecyclerView initializeRecyclerView(Adapter<RecyclerView.ViewHolder> adapter) { mAdapter = adapter; mAdapter.registerAdapterDataObserver(mAdapterObserver); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); mRecyclerView.setHasFixedSize(true); mRecyclerView.addOnScrollListener(new OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { setToolbarShadowVisibility(); } }); mItemAnimator = mRecyclerView.getItemAnimator(); return mRecyclerView; }
/** * Defines the choice behavior for the List. By default, Lists do not have any choice behavior * ({@link com.marshalchen.common.uimodule.twowayview.ItemSelectionSupport.ChoiceMode#NONE}). By setting the choiceMode to {@link com.marshalchen.common.uimodule.twowayview.ItemSelectionSupport.ChoiceMode#SINGLE}, the * List allows up to one item to be in a chosen state. By setting the choiceMode to * {@link com.marshalchen.common.uimodule.twowayview.ItemSelectionSupport.ChoiceMode#MULTIPLE}, the list allows any number of items to be chosen. * * @param choiceMode One of {@link com.marshalchen.common.uimodule.twowayview.ItemSelectionSupport.ChoiceMode#NONE}, {@link com.marshalchen.common.uimodule.twowayview.ItemSelectionSupport.ChoiceMode#SINGLE}, or * {@link com.marshalchen.common.uimodule.twowayview.ItemSelectionSupport.ChoiceMode#MULTIPLE} */ public void setChoiceMode(ChoiceMode choiceMode) { if (mChoiceMode == choiceMode) { return; } mChoiceMode = choiceMode; if (mChoiceMode != ChoiceMode.NONE) { if (mCheckedStates == null) { mCheckedStates = new CheckedStates(); } final Adapter adapter = mRecyclerView.getAdapter(); if (mCheckedIdStates == null && adapter != null && adapter.hasStableIds()) { mCheckedIdStates = new CheckedIdStates(); } } }
public void showData() { if (recyclerView == null) initViews(); Adapter adapter = recyclerView.getAdapter(); if (adapter != null) { if (adapter.getItemCount() == 0) { emptyView.setVisibility(VISIBLE); recyclerView.setVisibility(INVISIBLE); } else { // use GONE here so empty view doesn't use space on small lists emptyView.setVisibility(GONE); recyclerView.setVisibility(VISIBLE); } progressBar.setVisibility(GONE); } }
/** * Initializes the RecyclerView. * * @param adapter The adapter that provides a binding from an app-specific data set to views * that are displayed within the RecyclerView. */ public void initializeRecyclerView(Adapter<RecyclerView.ViewHolder> adapter) { mAdapter = adapter; mAdapter.registerAdapterDataObserver(mAdapterObserver); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); }
static void setSpanSizeLookup(final Adapter adapter, final GridLayoutManager layoutManager) { layoutManager.setSpanSizeLookup(new SpanSizeLookup() { public int getSpanSize(int position) { int type = adapter.getItemViewType(position); if (type == RcvAdapterWrapper.TYPE_HEADER || type == RcvAdapterWrapper.TYPE_FOOTER) { return layoutManager.getSpanCount(); } return 1; } }); }
@Override public void onAdapterChanged(RecyclerView.Adapter oldAdapter, RecyclerView.Adapter newAdapter) { super.onAdapterChanged(oldAdapter, newAdapter); final ItemSelectionSupport itemSelectionSupport = ItemSelectionSupport.from(mRecyclerView); if (oldAdapter != null && itemSelectionSupport != null) { itemSelectionSupport.clearChoices(); } }
public RecyclerAdapter(Adapter<ViewHolder> adapter, LayoutManager lm) { mHeaderArrays = new SparseArray<>(); mFooterArrays = new SparseArray<>(); setWrappedAdapter(adapter); mLayoutManager = lm; if (lm instanceof GridLayoutManager) { GridLayoutManager glm = (GridLayoutManager) lm; glm.setSpanSizeLookup(new SpanSizeLookup(this, glm.getSpanCount())); } }
private void setWrappedAdapter(Adapter<ViewHolder> adapter) { if (adapter == null) return; if (mAdapter != null) { notifyItemRangeRemoved(getHeadersCount(), getWrappedItemCount()); mAdapter.unregisterAdapterDataObserver(mDataObserver); } mAdapter = adapter; mAdapter.registerAdapterDataObserver(mDataObserver); notifyItemRangeInserted(getHeadersCount(), getWrappedItemCount()); }
@Override public final void addHeaderView(View v) { Adapter adapter = mRecyclerView.getAdapter(); if (adapter == null) throw new IllegalStateException( "Cannot add header view to recycler -- setAdapter has not been called."); ((RecyclerAdapter) adapter).addHeaderView(v); }
@Override public final void addFooterView(View v) { Adapter adapter = mRecyclerView.getAdapter(); if (adapter == null) throw new IllegalStateException( "Cannot add footer view to recycler -- setAdapter has not been called."); ((RecyclerAdapter) adapter).addFooterView(v); }
@Override public final ExRvAdapter getAdapter() { Adapter adapter = mRecyclerView.getAdapter(); if (adapter instanceof RecyclerAdapter) return (ExRvAdapter) ((RecyclerAdapter) adapter).getWrappedAdapter(); else return (ExRvAdapter) adapter; }
protected void addHeaderView(View v) { Adapter adapter = mRecyclerView.getAdapter(); if (adapter == null) throw new IllegalStateException( "Cannot add header view to recycler -- setAdapter has not been called."); ((RecyclerAdapter) adapter).addHeaderView(v); }
protected void addFooterView(View v) { Adapter adapter = mRecyclerView.getAdapter(); if (adapter == null) throw new IllegalStateException( "Cannot add footer view to recycler -- setAdapter has not been called."); ((RecyclerAdapter) adapter).addFooterView(v); }
protected ExRvAdapter getAdapter() { Adapter adapter = mRecyclerView.getAdapter(); if (adapter instanceof RecyclerAdapter) return (ExRvAdapter) ((RecyclerAdapter) adapter).getWrappedAdapter(); else return (ExRvAdapter) adapter; }
@Override public final ExRvAdapter getAdapter() { Adapter adapter = mRecyclerView.getAdapter(); if (adapter instanceof RecyclerAdapter) { return (ExRvAdapter) ((RecyclerAdapter) adapter).getWrappedAdapter(); } else { return (ExRvAdapter) adapter; } }
private void setWrappedAdapter(Adapter<ViewHolder> adapter) { if (adapter == null) { return; } if (mAdapter != null) { notifyItemRangeRemoved(getHeadersCount(), getWrappedItemCount()); mAdapter.unregisterAdapterDataObserver(mDataObserver); } mAdapter = adapter; mAdapter.registerAdapterDataObserver(mDataObserver); notifyItemRangeInserted(getHeadersCount(), getWrappedItemCount()); }