Java Code Examples for androidx.recyclerview.widget.RecyclerView#Adapter

The following examples show how to use androidx.recyclerview.widget.RecyclerView#Adapter . 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: SuperDividerItemDecoration.java    From RecyclerViewHelper with Apache License 2.0 6 votes vote down vote up
/**
 * Determine if it is the last row.
 *
 * @param view   ChildView
 * @param parent RecyclerView
 * @return true is the last row.
 */
private boolean isLastRow(View view, RecyclerView parent) {
    RecyclerView.Adapter adapter = parent.getAdapter();
    if (adapter == null) {
        return false;
    }

    int childCount = adapter.getItemCount();
    int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        int spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        if (mOrientation == VERTICAL) {
            childCount = childCount - childCount % spanCount;
            return position >= childCount;
        }
        return (position + 1) % spanCount == 0;

    } else if (layoutManager instanceof LinearLayoutManager) {
        return position + 1 == childCount;
    }
    return false;
}
 
Example 2
Source File: ListDialogDecorator.java    From AndroidMaterialDialog with Apache License 2.0 6 votes vote down vote up
@Override
public final <VH extends RecyclerView.ViewHolder> void setSingleChoiceItems(
        @Nullable final RecyclerView.Adapter<VH> adapter,
        @Nullable final RecyclerView.LayoutManager layoutManager, final int checkedItem,
        @Nullable final DialogInterface.OnClickListener listener) {
    this.items = null;
    this.iconResourceIds = null;
    this.singleChoiceItems = null;
    this.multiChoiceItems = null;
    this.adapter = adapter != null ?
            new RecyclerViewAdapterWrapper<>(getContext(), adapter, new SingleChoiceMode()) :
            null;
    this.layoutManager = adapter != null ?
            (layoutManager != null ? layoutManager : new LinearLayoutManager(getContext())) :
            null;
    this.singleChoiceListener = adapter != null ? listener : null;
    this.multiChoiceListener = null;

    if (checkedItem != -1 && this.adapter != null) {
        this.adapter.setItemChecked(checkedItem, true);
    }

    attachAdapter();
}
 
Example 3
Source File: DemoActivity.java    From StickyHeaders with MIT License 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menuItemInspect:
            showInspectDialog();
            break;

        case R.id.scrollToBottom:
            showScrollToPositionDialog();
            break;

        case R.id.reloadMenuItem:
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof SectioningAdapter) {
                ((SectioningAdapter) adapter).notifyAllSectionsDataSetChanged();
            } else {
                adapter.notifyDataSetChanged();
            }
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 4
Source File: RecyclerViewManager.java    From cathode with Apache License 2.0 6 votes vote down vote up
public void setAdapter(RecyclerView.Adapter adapter) {
  if (adapter != this.adapter) {
    if (this.adapter != null) {
      this.adapter.unregisterAdapterDataObserver(adapterObserver);
    }

    this.adapter = adapter;
    if (this.adapter != null) {
      if (recyclerView != null) {
        recyclerView.setAdapter(this.adapter);
      }

      adapter.registerAdapterDataObserver(adapterObserver);

      if (adapter.getItemCount() > 0) {
        emptyView.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
      } else {
        emptyView.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
      }
    }
  }
}
 
Example 5
Source File: MainActivity.java    From googleads-mobile-android-examples with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recycler_view);

    // Use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView.
    recyclerView.setHasFixedSize(true);

    // Specify a linear layout manager.
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    // Update the RecyclerView item's list with menu items and banner ads.
    addMenuItemsFromJson();
    addBannerAds();
    loadBannerAds();

    // Specify an adapter.
    RecyclerView.Adapter<RecyclerView.ViewHolder> adapter = new RecyclerViewAdapter(this,
        recyclerViewItems);
    recyclerView.setAdapter(adapter);
}
 
Example 6
Source File: AbstractListDialog.java    From AndroidMaterialDialog with Apache License 2.0 5 votes vote down vote up
@Override
public final <VH extends RecyclerView.ViewHolder> void setSingleChoiceItems(
        @Nullable final RecyclerView.Adapter<VH> adapter,
        @Nullable final RecyclerView.LayoutManager layoutManager, final int checkedItem,
        @Nullable final OnClickListener listener) {
    decorator.setSingleChoiceItems(adapter, layoutManager, checkedItem, listener);
}
 
Example 7
Source File: Indicator.java    From MaterialScrollBar with Apache License 2.0 5 votes vote down vote up
/**
 * This method tests the adapter to make sure that it implements the needed interface.
 *
 * @param adapter The adapter of the attached {@link RecyclerView}.
 */
void testAdapter(RecyclerView.Adapter adapter) {
    if  (adapter == null) {
        Log.e("MaterialScrollBarLib", "The adapter for your recyclerView has not been set; " +
                "skipping indicator layout.");
        return;
    }
    if(!adapterClass.isInstance(adapter)) {
        throw new IllegalArgumentException(
                "In order to add this indicator, the adapter for your recyclerView, "
                        + adapter.getClass().getName()
                        + ", MUST implement " + Utils.getGenericName(this) + ".");
    }
}
 
Example 8
Source File: NeopixelComponentSelectorFragment.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Dismiss on click outside
    AppCompatDialog dialog = (AppCompatDialog) getDialog();
    if (dialog != null) {
        dialog.setCanceledOnTouchOutside(true);
    }

    // UI
    Context context = getContext();
    if (context != null) {
        RecyclerView standardComponentsRecyclerView = view.findViewById(R.id.standardComponentsRecyclerView);
        standardComponentsRecyclerView.setHasFixedSize(true);
        standardComponentsRecyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        RecyclerView.Adapter standardBoardSizesAdapter = new StandardComponentsAdapter(mSelectedComponent, components -> {
            mSelectedComponent = components;
            if (mListener != null) {
                mListener.onComponentsSelected(components, mIs400KhzEnabled);
                dismiss();
            }
        });
        standardComponentsRecyclerView.setAdapter(standardBoardSizesAdapter);


        SwitchCompat mode400HhzSwitch = view.findViewById(R.id.mode400HhzSwitch);
        mode400HhzSwitch.setChecked(mIs400KhzEnabled);
        mode400HhzSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (buttonView.isPressed()) {
                mIs400KhzEnabled = isChecked;
                if (mListener != null) {
                    mListener.onComponentsSelected(mSelectedComponent, mIs400KhzEnabled);
                }
            }
        });
    }
}
 
Example 9
Source File: FastScrollView.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set a new {@link RecyclerView.Adapter} that implements {@link FastScroller.SectionIndexer}
 * to provide child views for the RecyclerView and section text for the FastScroller.
 *
 * @param adapter The new {@link RecyclerView.Adapter} to set, or null to set none
 */
public void setAdapter(@Nullable RecyclerView.Adapter adapter) {
    recyclerView.setAdapter(adapter);

    if (adapter instanceof FastScroller.SectionIndexer) {
        fastScroller.setSectionIndexer((FastScroller.SectionIndexer) adapter);
    } else if (adapter == null) {
        fastScroller.setSectionIndexer(null);
    }
}
 
Example 10
Source File: EventBridge.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Installs the event bridge for on the supplied adapter/helper.
 *
 * @param adapter
 * @param selectionTracker
 * @param keyProvider
 *
 * @param <K> Selection key type. @see {@link StorageStrategy} for supported types.
 */
public static <K> void install(
        @NonNull RecyclerView.Adapter<?> adapter,
        @NonNull SelectionTracker<K> selectionTracker,
        @NonNull ItemKeyProvider<K> keyProvider) {

    // setup bridges to relay selection and adapter events
    new TrackerToAdapterBridge<>(selectionTracker, keyProvider, adapter);
    adapter.registerAdapterDataObserver(selectionTracker.getAdapterDataObserver());
}
 
Example 11
Source File: OmegaRecyclerView.java    From OmegaRecyclerView with MIT License 5 votes vote down vote up
@Nullable
protected BaseStickyDecoration provideStickyDecoration(@NonNull RecyclerView.Adapter adapter, @Nullable StickyAdapter stickyAdapter) {
    if (stickyAdapter == null) return null;
    switch (mStickyMode) {
        case StickyAdapter.Mode.MIDDLE:
            return new MiddleStickyDecoration(stickyAdapter);
        case StickyAdapter.Mode.HEADER:
        default:
            return new HeaderStickyDecoration(stickyAdapter);
    }
}
 
Example 12
Source File: OmegaPagerRecyclerView.java    From OmegaRecyclerView with MIT License 4 votes vote down vote up
public int getItemCount() {
    RecyclerView.Adapter adapter = getAdapter();
    return adapter == null ? 0 : adapter.getItemCount();
}
 
Example 13
Source File: FavouritesListFragment.java    From Kore with Apache License 2.0 4 votes vote down vote up
@Override
protected RecyclerView.Adapter createAdapter() {
    return new FavouritesAdapter(getActivity(), HostManager.getInstance(getActivity()));
}
 
Example 14
Source File: ClickWrapper.java    From RecyclerViewHelper with Apache License 2.0 4 votes vote down vote up
public ClickWrapper(RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
    this.mAdapter = adapter;
}
 
Example 15
Source File: MediaAdapter.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
public static void setMediaSize(RecyclerView recyclerView, int mediaSize) {
    RecyclerView.Adapter adapter = recyclerView.getAdapter();
    if (adapter instanceof MediaAdapter) {
        ((MediaAdapter) adapter).setMediaSize(mediaSize);
    }
}
 
Example 16
Source File: DocumentTreeRecyclerAdapter.java    From libcommon with Apache License 2.0 4 votes vote down vote up
public boolean onItemLongClick(@NonNull RecyclerView.Adapter<?> parent,
@NonNull View view, @NonNull final DocumentFile item);
 
Example 17
Source File: DocumentTreeRecyclerAdapter.java    From libcommon with Apache License 2.0 4 votes vote down vote up
public void onItemClick(@NonNull RecyclerView.Adapter<?> parent,
@NonNull View view, @NonNull final DocumentFile item);
 
Example 18
Source File: FamiliarRefreshRecyclerView.java    From FamiliarRecyclerView with MIT License 4 votes vote down vote up
public void setAdapter(RecyclerView.Adapter adapter) {
    mRvList.setAdapter(adapter);
}
 
Example 19
Source File: FillLastGridLayoutManager.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onAdapterChanged(RecyclerView.Adapter oldAdapter, RecyclerView.Adapter newAdapter) {
    heights.clear();
    calcLastItemHeight();
    super.onAdapterChanged(oldAdapter, newAdapter);
}
 
Example 20
Source File: ArrayListRecyclerViewAdapter.java    From libcommon with Apache License 2.0 4 votes vote down vote up
public void onItemClick(
@NonNull final RecyclerView.Adapter<?> parent,
@NonNull final View view,
final int position, @Nullable final T item);