2015-08-16 113 views
1

我已成功設置使用新設計支持庫的導航抽屜,但不知道如何進一步處理。 我需要提供用戶帳戶,有人可以提供一個代碼或如何通過這個想法。導航抽屜中的用戶帳戶

Navigation Drawer

這是代碼我現在有。

<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" /> 

回答

2

假設你已經設置了SQLite的輔助類(example),和已有的知識來獲得使用HashMap的用戶詳細信息,您可以輕鬆擁有通過添加一對夫婦的意見您的導航這個用做抽屜xml。

您將需要添加一個textview和一個circleimageview from hdondenhof,它將顯示您的用戶名和個人資料圖片。如果你想要顯示更多的視圖,你也可以這樣做。爲它們分配ID,以便您可以在其對應的Java對話框中顯示所需的信息。

在您的導航抽屜片段中,如果已經存儲在您的SQLite數據庫中,您可以在這裏傳遞用戶名和圖片參數。在我的情況下,我通常會在我的片段的onCreateView中調用它:

// 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); 

而這的確很簡單。 circleimageview的工作原理與imageview相似,因此您可以從數據庫獲取用戶的圖片,或者如果未找到圖片,則顯示可繪製的圖片。