类android.support.v7.widget.RecyclerView.Adapter源码实例Demo

下面列出了怎么用android.support.v7.widget.RecyclerView.Adapter的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: ticdesign   文件: FocusableLinearLayoutManager.java
@Override
public boolean validAdapter(Adapter adapter) {
    // TODO: find a better way to valid adapter instead of instance a ViewHolder.
    if (adapter != null && adapter.getItemCount() > 0) {
        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;
}
 
源代码2 项目: 365browser   文件: SelectableListLayout.java
/**
 * 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;
}
 
源代码3 项目: MultiView   文件: ItemSelectionSupport.java
/**
 * 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();
        }
    }
}
 
源代码4 项目: UltimateAndroid   文件: ItemSelectionSupport.java
/**
 * 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();
        }
    }
}
 
源代码5 项目: AndroidChromium   文件: SelectableListLayout.java
/**
 * 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()));
}
 
源代码6 项目: agera   文件: RepositoryAdapter.java
/**
 * Builds the {@link RepositoryAdapter} that presents the provided repositories in order and
 * observes the repositories as well as any additional observables while the provided
 * {@link Activity} is resumed (between {@link Activity#onResume()} and
 * {@link Activity#onPause()}).
 * <p>
 * Note: Can only be called from {@link Activity#onCreate} ()}
 */
@TargetApi(14)
@NonNull
public Adapter<ViewHolder> whileResumed(@NonNull final Activity activity) {
  final RepositoryAdapter repositoryAdapter = new RepositoryAdapter(this);
  activity.getApplication().registerActivityLifecycleCallbacks(
      new WhileResumedActivityLifecycleCallbacks(activity, repositoryAdapter));
  return repositoryAdapter;
}
 
源代码7 项目: agera   文件: RepositoryAdapter.java
/**
 * Builds the {@link RepositoryAdapter} that presents the provided repositories in order and
 * observes the repositories as well as any additional observables while the provided
 * {@link Activity} is started (between (between {@link Activity#onStart()} and
 * {@link Activity#onStop()}).
 * <p>
 * Note: Can only be called from {@link Activity#onCreate} ()}
 */
@TargetApi(14)
@NonNull
public Adapter<ViewHolder> whileStarted(@NonNull final Activity activity) {
  final RepositoryAdapter repositoryAdapter = new RepositoryAdapter(this);
  activity.getApplication().registerActivityLifecycleCallbacks(
      new WhileStartedActivityLifecycleCallbacks(activity, repositoryAdapter));
  return repositoryAdapter;
}
 
源代码8 项目: UltimateAndroid   文件: TwoWayLayoutManager.java
@Override
public void onAdapterChanged(Adapter oldAdapter, Adapter newAdapter) {
    super.onAdapterChanged(oldAdapter, newAdapter);

    final ItemSelectionSupport itemSelectionSupport = ItemSelectionSupport.from(mRecyclerView);
    if (oldAdapter != null && itemSelectionSupport != null) {
        itemSelectionSupport.clearChoices();
    }
}
 
源代码9 项目: letv   文件: SortedListAdapterCallback.java
public SortedListAdapterCallback(Adapter adapter) {
    this.mAdapter = adapter;
}
 
源代码10 项目: MultiView   文件: ItemSelectionSupport.java
/**
 * Sets the checked state of the specified position. The is only valid if
 * the choice mode has been set to {@link ChoiceMode#SINGLE} or
 * {@link ChoiceMode#MULTIPLE}.
 *
 * @param position The item whose checked state is to be checked
 * @param checked  The new checked state for the item
 */
public void setItemChecked(int position, boolean checked) {
    if (mChoiceMode == ChoiceMode.NONE) {
        return;
    }

    final Adapter adapter = mRecyclerView.getAdapter();

    if (mChoiceMode == ChoiceMode.MULTIPLE) {
        boolean oldValue = mCheckedStates.get(position);
        mCheckedStates.put(position, checked);

        if (mCheckedIdStates != null && adapter.hasStableIds()) {
            if (checked) {
                mCheckedIdStates.put(adapter.getItemId(position), position);
            } else {
                mCheckedIdStates.delete(adapter.getItemId(position));
            }
        }

        if (oldValue != checked) {
            if (checked) {
                mCheckedCount++;
            } else {
                mCheckedCount--;
            }
        }
    } else {
        boolean updateIds = mCheckedIdStates != null && adapter.hasStableIds();

        // Clear all values if we're checking something, or unchecking the currently
        // selected item
        if (checked || isItemChecked(position)) {
            mCheckedStates.clear();

            if (updateIds) {
                mCheckedIdStates.clear();
            }
        }

        // This may end up selecting the checked we just cleared but this way
        // we ensure length of mCheckStates is 1, a fact getCheckedItemPosition relies on
        if (checked) {
            mCheckedStates.put(position, true);

            if (updateIds) {
                mCheckedIdStates.put(adapter.getItemId(position), position);
            }

            mCheckedCount = 1;
        } else if (mCheckedStates.size() == 0 || !mCheckedStates.valueAt(0)) {
            mCheckedCount = 0;
        }
    }

    updateOnScreenCheckedViews();
}
 
源代码11 项目: MultiView   文件: ItemSelectionSupport.java
public void onAdapterDataChanged() {
    final Adapter adapter = mRecyclerView.getAdapter();
    if (mChoiceMode == ChoiceMode.NONE || adapter == null || !adapter.hasStableIds()) {
        return;
    }

    final int itemCount = adapter.getItemCount();

    // Clear out the positional check states, we'll rebuild it below from IDs.
    mCheckedStates.clear();

    for (int checkedIndex = 0; checkedIndex < mCheckedIdStates.size(); checkedIndex++) {
        final long currentId = mCheckedIdStates.keyAt(checkedIndex);
        final int currentPosition = mCheckedIdStates.valueAt(checkedIndex);

        final long newPositionId = adapter.getItemId(currentPosition);
        if (currentId != newPositionId) {
            // Look around to see if the ID is nearby. If not, uncheck it.
            final int start = Math.max(0, currentPosition - CHECK_POSITION_SEARCH_DISTANCE);
            final int end = Math.min(currentPosition + CHECK_POSITION_SEARCH_DISTANCE, itemCount);

            boolean found = false;
            for (int searchPos = start; searchPos < end; searchPos++) {
                final long searchId = adapter.getItemId(searchPos);
                if (currentId == searchId) {
                    found = true;
                    mCheckedStates.put(searchPos, true);
                    mCheckedIdStates.setValueAt(checkedIndex, searchPos);
                    break;
                }
            }

            if (!found) {
                mCheckedIdStates.delete(currentId);
                mCheckedCount--;
                checkedIndex--;
            }
        } else {
            mCheckedStates.put(currentPosition, true);
        }
    }
}
 
源代码12 项目: Android-Next   文件: AdvancedRecyclerAdapter.java
public AdvancedRecyclerAdapter(final RecyclerView.Adapter<?> adapter) {
    mWrapped = adapter;
    mWrapped.registerAdapterDataObserver(mAdapterDataObserver);
}
 
源代码13 项目: Android-Next   文件: AdvancedRecyclerAdapter.java
public Adapter getWrapped() {
    return mWrapped;
}
 
源代码14 项目: UltimateAndroid   文件: TwoWayLayoutManager.java
protected Adapter getAdapter() {
    return (mRecyclerView != null ? mRecyclerView.getAdapter() : null);
}
 
源代码15 项目: UltimateAndroid   文件: ItemSelectionSupport.java
/**
 * Sets the checked state of the specified position. The is only valid if
 * the choice mode has been set to {@link com.marshalchen.common.uimodule.twowayview.ItemSelectionSupport.ChoiceMode#SINGLE} or
 * {@link com.marshalchen.common.uimodule.twowayview.ItemSelectionSupport.ChoiceMode#MULTIPLE}.
 *
 * @param position The item whose checked state is to be checked
 * @param checked The new checked state for the item
 */
public void setItemChecked(int position, boolean checked) {
    if (mChoiceMode == ChoiceMode.NONE) {
        return;
    }

    final Adapter adapter = mRecyclerView.getAdapter();

    if (mChoiceMode == ChoiceMode.MULTIPLE) {
        boolean oldValue = mCheckedStates.get(position);
        mCheckedStates.put(position, checked);

        if (mCheckedIdStates != null && adapter.hasStableIds()) {
            if (checked) {
                mCheckedIdStates.put(adapter.getItemId(position), position);
            } else {
                mCheckedIdStates.delete(adapter.getItemId(position));
            }
        }

        if (oldValue != checked) {
            if (checked) {
                mCheckedCount++;
            } else {
                mCheckedCount--;
            }
        }
    } else {
        boolean updateIds = mCheckedIdStates != null && adapter.hasStableIds();

        // Clear all values if we're checking something, or unchecking the currently
        // selected item
        if (checked || isItemChecked(position)) {
            mCheckedStates.clear();

            if (updateIds) {
                mCheckedIdStates.clear();
            }
        }

        // This may end up selecting the checked we just cleared but this way
        // we ensure length of mCheckStates is 1, a fact getCheckedItemPosition relies on
        if (checked) {
            mCheckedStates.put(position, true);

            if (updateIds) {
                mCheckedIdStates.put(adapter.getItemId(position), position);
            }

            mCheckedCount = 1;
        } else if (mCheckedStates.size() == 0 || !mCheckedStates.valueAt(0)) {
            mCheckedCount = 0;
        }
    }

    updateOnScreenCheckedViews();
}
 
源代码16 项目: UltimateAndroid   文件: ItemSelectionSupport.java
public void onAdapterDataChanged() {
    final Adapter adapter = mRecyclerView.getAdapter();
    if (mChoiceMode == ChoiceMode.NONE || adapter == null || !adapter.hasStableIds()) {
        return;
    }

    final int itemCount = adapter.getItemCount();

    // Clear out the positional check states, we'll rebuild it below from IDs.
    mCheckedStates.clear();

    for (int checkedIndex = 0; checkedIndex < mCheckedIdStates.size(); checkedIndex++) {
        final long currentId = mCheckedIdStates.keyAt(checkedIndex);
        final int currentPosition = mCheckedIdStates.valueAt(checkedIndex);

        final long newPositionId = adapter.getItemId(currentPosition);
        if (currentId != newPositionId) {
            // Look around to see if the ID is nearby. If not, uncheck it.
            final int start = Math.max(0, currentPosition - CHECK_POSITION_SEARCH_DISTANCE);
            final int end = Math.min(currentPosition + CHECK_POSITION_SEARCH_DISTANCE, itemCount);

            boolean found = false;
            for (int searchPos = start; searchPos < end; searchPos++) {
                final long searchId = adapter.getItemId(searchPos);
                if (currentId == searchId) {
                    found = true;
                    mCheckedStates.put(searchPos, true);
                    mCheckedIdStates.setValueAt(checkedIndex, searchPos);
                    break;
                }
            }

            if (!found) {
                mCheckedIdStates.delete(currentId);
                mCheckedCount--;
                checkedIndex--;
            }
        } else {
            mCheckedStates.put(currentPosition, true);
        }
    }
}
 
源代码17 项目: ticdesign   文件: TicklableLayoutManager.java
/**
 * Check if the adapter is valid for this layout manager.
 */
boolean validAdapter(Adapter adapter);
 
/**
 * Constructs a new listener which will delegate its calls back to the
 * given adapter.
 *
 * @param adapter The adapter to send calls back to.
 */
public RecyclerViewListObserverListener(RecyclerView.Adapter<?> adapter) {
    this.adapter = adapter;
}
 
 类方法
 同包方法