简体   繁体   中英

User Accounts in Navigation Drawer

I have successfully setup the navigation drawer using the new design support library but not sure how to proceed further. I need to provide user accounts,could someone provide a code or an idea of how to go through with that.

导航抽屉

This is the code i currently have.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

</RelativeLayout>


<android.support.design.widget.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:menu="@menu/nav_menu" />

Assuming you have SQLite helper class ( example ) set up, and have the knowledge to get user details using HashMap, you can easily have this done using by adding a couple of views on your navigation drawer xml.

You will need to add a textview and a circleimageview from hdondenhof that will display your username and profile picture. And if you want more views to be displayed, you can do that too. Assign them with IDs so you can display the needed information at its java counterpart.

In your Navigation Drawer fragment, you can pass the username and picture parameters here if it is already stored in your SQLite DB. In my case, I would usually call this in onCreateView of my fragment:

// Initialize and declare SQLite handler class to retrieve user's details:
final SQLiteHandler sqLiteHandler = new SQLiteHandler(getActivity());
final HashMap<String, String> user = sqLiteHandler.getUserDetails();

// Retrieving the user's username for display on navUsername
String setUsername = user.get("username");
navUsername = (TextView) view.findViewById(R.id.nav_username);
navUsername.setText("Welcome, " + setUsername);

And it's really just that easy. The circleimageview works just like imageview so you can get the user's picture from the DB or display a drawable if no picture is found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM