gpt4 book ai didi

安卓数据绑定(bind) : Pass variable to NavigationView

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:22 25 4
gpt4 key购买 nike

当用户登录我的应用程序时,我会在 MainActivity 中收到他们的个人资料信息(姓名、头像等) ,我想通过数据绑定(bind)用用户参数填充我的导航 View 标题中的一些字段。我试过 the docs 中提出的方法, 但可能仅限于 <include />只是?

activity_main.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="user"
type="com.example.app.models.User" />
</data>

<android.support.v4.widget.DrawerLayout
// layout params... >

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView xmlns:bind="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
bind:user="@{user}" />

</android.support.v4.widget.DrawerLayout>
</layout>

nav_header_main.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="user"
type="com.example.app.models.User" />
</data>

<LinearLayout
// layout params... >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@{user.name}"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

</LinearLayout>
</layout>

主 Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setUser(User.getInstance());
// other stuff...
}

我得到 2 个错误:

  1. 错误:找不到符号类 ActivityMainBinding
  2. 在 android.support.design.widget.NavigationView 上找不到参数类型为 com.example.app.models.User 的属性“bind:user”的 setter 。

如果我删除 bind:user它构建的行,但它当然不起作用。有什么方法可以让这种东西发挥作用吗?

最佳答案

这对我有用:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
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">

<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"/>

</androidx.drawerlayout.widget.DrawerLayout>

</layout>

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_nav_view_header">

<data>
<variable
name="user"
type="com.myapp.model.UserSettings"/>
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round"
android:contentDescription="@string/nav_header_desc"
android:id="@+id/imageView"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@{user.name}"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nav_header_subtitle"
android:id="@+id/textView"/>

</LinearLayout>

</layout>

然后在 Activity 的 onCreate 中:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val viewHeader = nav_view.getHeaderView(0)
val navViewHeaderBinding : NavHeaderMainBinding = NavHeaderMainBinding.bind(viewHeader)
navViewHeaderBinding.user = UserSettings(null, null, "John Doe", "Android Developer", null)
}

关于安卓数据绑定(bind) : Pass variable to NavigationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49523124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com