下面列出了androidx.recyclerview.widget.RecyclerView#ItemDecoration ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@NonNull
@Override
public VH createViewHolder(final ViewGroup parent) {
final RendererRecyclerViewAdapter adapter = createAdapter();
for (final ViewRenderer renderer : mRenderers) {
adapter.registerRenderer(renderer);
}
final VH viewHolder = createCompositeViewHolder(parent);
viewHolder.setAdapter(adapter);
if (viewHolder.getRecyclerView() != null) {
viewHolder.getRecyclerView().setLayoutManager(createLayoutManager());
viewHolder.getRecyclerView().setAdapter(adapter);
viewHolder.getRecyclerView().setRecycledViewPool(mRecycledViewPool);
for (final RecyclerView.ItemDecoration itemDecoration : createItemDecorations()) {
viewHolder.getRecyclerView().addItemDecoration(itemDecoration);
}
}
return viewHolder;
}
public void setDividerAlpha(float dividerAlpha) {
if (0 > dividerAlpha || dividerAlpha > 1) return;
if (mDividerItemDecoration != null) {
mDividerItemDecoration.setDividerAlpha(dividerAlpha);
}
boolean hasDividerDecoration = false;
for (int i = 0; i < getItemDecorationCount(); i++) {
RecyclerView.ItemDecoration itemDecoration = getItemDecorationAt(i);
if (itemDecoration instanceof DividerItemDecoration) {
hasDividerDecoration = true;
invalidateItemDecorations();
break;
}
}
if (!hasDividerDecoration && mDividerItemDecoration != null) {
addItemDecoration(mDividerItemDecoration);
}
}
@BindingAdapter({"item_touch"})
public static void setItemTouchHelper(final RecyclerView view, final ItemTouchHelper helper) {
for (int i = 0;; i ++) try {
final RecyclerView.ItemDecoration decoration = view.getItemDecorationAt(i);
if (decoration == null) break; // Null is returned on RecyclerView library 27+
if (decoration == helper) return;
} catch (final IndexOutOfBoundsException ignored) { break; } // IndexOutOfBoundsException is thrown on RecyclerView library prior to 27.
helper.attachToRecyclerView(view);
}
@Bindable
public RecyclerView.ItemDecoration[] getItemDecorations() {
return new RecyclerView.ItemDecoration[] {
new BackgroundDecoration(),
new DividerDecoration(getContext())
};
}
/**
* Please use a constructor without Context
*/
@Deprecated
public CompositeViewBinder(final int layoutID,
@IdRes final int recyclerViewID,
@NonNull final Class<M> type,
@NonNull final Context context,
@NonNull final List<? extends RecyclerView.ItemDecoration> decorations) {
this(layoutID, recyclerViewID, type, context);
mDecorations.addAll(decorations);
}
@CallSuper @Override public void setupViews() {
super.setupViews();
RecyclerView.ItemDecoration itemDecoration = getItemDecoration();
if (itemDecoration != null) {
getRecyclerView().addItemDecoration(itemDecoration);
}
}
public CompositeViewBinder(final int layoutID,
@IdRes final int recyclerViewID,
@NonNull final Class<M> type,
@NonNull final List<? extends RecyclerView.ItemDecoration> decorations) {
this(layoutID, recyclerViewID, type);
mDecorations.addAll(decorations);
}
@Override
protected RecyclerView.ItemDecoration createItemDecoration() {
return new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,
@NonNull RecyclerView.State state) {
outRect.set(20, 20, 20, 20);
}
};
}
public void addDecoration(@NonNull RecyclerView.ItemDecoration decor) {
list.addItemDecoration(decor);
}
public static RecyclerViewHeader fromXml(Context context, @LayoutRes int layoutRes, RecyclerView.ItemDecoration decoration) {
RecyclerViewHeader header = new RecyclerViewHeader(context);
View.inflate(context, layoutRes, header);
header.setDecor(decoration);
return header;
}
protected RecyclerView.ItemDecoration getItemDecoration()
{
//We must draw dividers ourselves if we want them in a list
return new DividerDecoration(getActivity());
}
public void removeItemDecoration(RecyclerView.ItemDecoration decoration) {
mRecyclerView.removeItemDecoration(decoration);
}
@NonNull
@Override
protected List<? extends RecyclerView.ItemDecoration> createItemDecorations() {
return Collections.singletonList(new BetweenSpacesItemDecoration(0, 10));
}
@NonNull
protected List<? extends RecyclerView.ItemDecoration> createItemDecorations() {
return new ArrayList<>();
}
private void setDecor(RecyclerView.ItemDecoration decor) {
mDecoration = decor;
}
@Override
protected RecyclerView.ItemDecoration getItemDecoration()
{
//We must draw dividers ourselves if we want them in a list
return new DividerDecoration(getActivity());
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
adapter = new ScannedDevicesAdapter(new DeviceInfoViewHolder.Generator(R.layout.device_item) {
@Override
public DeviceInfoViewHolder generate(View itemView) {
final ViewHolder holder = new ViewHolder(itemView);
holder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int adapterPos = holder.getAdapterPosition();
if (adapterPos != RecyclerView.NO_POSITION) {
BluetoothDeviceInfo devInfo = (BluetoothDeviceInfo) adapter.getDevicesInfo().get(adapterPos);
connect(devInfo);
}
}
});
return holder;
}
}, context);
discovery = new Discovery(adapter, this);
adapter.setThermometerMode();
discovery.connect(context);
layout = new GridLayoutManager(context, context.getResources().getInteger(R.integer.device_selection_columns), LinearLayoutManager.VERTICAL, false);
itemDecoration = new RecyclerView.ItemDecoration() {
final int horizontalMargin = getResources().getDimensionPixelSize(R.dimen.item_margin);
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
final int columns = layout.getSpanCount();
if (columns == 1) {
outRect.set(0, 0, 0, 0);
} else {
int itemPos = parent.getChildAdapterPosition(view);
if (itemPos % columns == columns - 1) {
outRect.set(0, 0, 0, 0);
} else {
outRect.set(0, 0, horizontalMargin, 0);
}
}
}
};
}
/**
* Remove an {@link RecyclerView.ItemDecoration} from wrapped RecyclerView.
* <p>
* <p>The given decoration will no longer impact the measurement and drawing of
* item views.</p>
*
* @param decor Decoration to remove
* @see #addItemDecoration(RecyclerView.ItemDecoration)
*/
@SuppressWarnings("unused")
public final void removeItemDecoration(RecyclerView.ItemDecoration decor) {
getRvCategories().removeItemDecoration(decor);
}
/**
* Add an {@link RecyclerView.ItemDecoration} to this RecyclerView. Item decorations can affect both measurement and drawing of individual item views.
* <p>Item decorations are ordered. Decorations placed earlier in the list will be run/queried/drawn first for their effects on item views. Padding added to views will be nested; a padding added by an earlier decoration will mean further item decorations in the list will be asked to draw/pad within the previous decoration's given area.</p>
*
* @param itemDecoration Decoration to add
* @param index Position in the decoration chain to insert this decoration at. If this value is negative the decoration will be added at the end.
*/
public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration, int index) {
mRecyclerView.addItemDecoration(itemDecoration, index);
}
/**
* Add an {@link RecyclerView.ItemDecoration} to wrapped RecyclerView. Item decorations can
* affect both measurement and drawing of individual item views.
* <p>
* <p>Item decorations are ordered. Decorations placed earlier in the list will
* be run/queried/drawn first for their effects on item views. Padding added to views
* will be nested; a padding added by an earlier decoration will mean further
* item decorations in the list will be asked to draw/pad within the previous decoration's
* given area.</p>
*
* @param decor Decoration to add
*/
@SuppressWarnings("unused")
public final void addItemDecoration(RecyclerView.ItemDecoration decor) {
getRvCategories().addItemDecoration(decor);
}