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?