简体   繁体   English

导航抽屉中的用户帐户

[英]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. 假设您已设置SQLite帮助器类( 示例 ),并且具有使用HashMap获取用户详细信息的知识,则可以通过在导航抽屉xml上添加几个视图来轻松完成此操作。

You will need to add a textview and a circleimageview from hdondenhof that will display your username and profile picture. 您需要从hdondenhof添加文本视图和circleimageview ,以显示您的用户名和个人资料图片。 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. 为它们分配ID,以便可以在其Java对应项上显示所需的信息。

In your Navigation Drawer fragment, you can pass the username and picture parameters here if it is already stored in your SQLite DB. 在导航抽屉片段中,如果用户名和图片参数已经存储在SQLite数据库中,则可以在此处传递用户名和图片参数。 In my case, I would usually call this in onCreateView of my fragment: 就我而言,我通常会在片段的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);

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. circleimageview的工作方式与imageview一样,因此您可以从数据库中获取用户的图片,如果找不到图片,则显示可绘制的图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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