0

How can I use an adapter to dynamically add a NavigationView (programatically) without relying on a RecyclerView (ie, just appending items to the NavigationView based on data I have stored in the app's SQLite database)

I have managed to get items into the list. Right now my XML is like this:

<android.support.design.widget.NavigationView
    android:id="@+id/navigationview"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

        <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.design.widget.NavigationView>

In my code includes methods to implement this:

public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter ViewHolder> {
    final private Context mContext;
    private Cursor mCursor;

    public NavigationAdapter(Context context) {
        mContext = context;
    }

    @Override
    public NavigationAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        // Inflate list item view for NavigationView
    }

    @Override
    public void onBindViewHolder(NavigationAdapterViewHolder taskAdapterViewHolder, int position) {
        // Move cursor to correct position
        // Display text for list item in NavigationView 
    }

    @Override
    public int getItemCount() {
        // Return count
    }

    public class NavigationAdapterViewHolder extends RecyclerView.ViewHolder {

        public NavigationAdapterViewHolder(View view) {
            // ViewHolder pattern for RecyclerView
        }
    }
}

However, since I have used the RecyclerView, I no longer get the benefits of NavigationView like:

  • click handlers
  • adding non-dynamic items (i.e. menu items I always want to display)
  • proper padding + Material Design implementation for menu items

This is because I'm using populating a RecyclerView instead of the NavigationView directly (which I would like to do) which means I would need to reapply all the Material styles to the menu items.

Is there a way I can use an adapter to just add items to the NavigationView, and let the design support library handle all the styling and click handlers?

user2560886
  • 1,910
  • 2
  • 14
  • 20
  • I doubt that. I have implemented both menu items (programmatically) and `RecyclerView` in `NavigationView` individually. I think if you don't use menu , you have to handle all styling on your own. – Sash_KP Jul 28 '15 at 09:36
  • Is there a reason to use `RecyclerView`? `RecyclerView` is specifically in cases where you need to *recycle* already inflated view (e.g. long, scrollable list). If you just want dynamically generated navigation items, you can do that without `RecyclerView` also. – Hindol Jul 28 '15 at 09:43
  • 1
    **Without using adapter**, you can call `getMenu()` on `NavigationView` and use `add()` to add as many items as you like. Kind of like `TabLayout`. I think that is the standard approach. – Hindol Jul 28 '15 at 11:25
  • **Update** If you're looking for this answer, see this Stack Overflow answer instead: https://stackoverflow.com/questions/30978114/add-a-listview-or-recyclerview-to-new-navigationview – user2560886 Dec 31 '16 at 23:45

0 Answers0