Подтвердить что ты не робот

Как получить представление из макета заголовка ящика с привязкой к активности?

Итак, это мой 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"
        tools:context="MainActivity"
    >
    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/ll_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/my_awesome_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/black"
                    android:fitsSystemWindows="true"
                    >

                    <TextView
                        android:id="@+id/toolbar_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginStart="10dp"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/abc_text_size_title_material_toolbar"
                        tools:text="@string/default_toolbar_title"/>

                </android.support.v7.widget.Toolbar>


                <FrameLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                </FrameLayout>
            </LinearLayout>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_fuf"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginRight="20dp"
                android:src="@drawable/flamme"
                app:fabSize="normal"
                />
        </RelativeLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/black"
            **app:headerLayout="@layout/drawer_header"**
            app:itemTextColor="@color/drawer_item_color_selector"
            app:menu="@menu/menu_drawer"/>

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

и я использую привязку для этой активности, поэтому мне не нужно использовать findViewById и накладывать ее и т.д. следующим образом:

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        Toolbar toolbar = binding.myAwesomeToolbar;
        toolbarTitle = binding.toolbarTitle;
        BalrogFontsHelper.SetKhandBoldToView(toolbarTitle);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_dehaze_white_24);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            actionBar.setDisplayShowTitleEnabled(false);
        }


        drawerLayout = binding.drawerLayout;
        **tvLoggedUserEmail = (TextView) findViewById(R.id.tv_logged_user_email);**
        BalrogFontsHelper.SetKhandBoldToView(tvLoggedUserEmail);

Как вы можете видеть, я могу получить представления, которые находятся непосредственно в макете activity_main.xml путем привязки, но когда я пытаюсь получить представление, я не вижу переменную в объекте привязки.

drawer_header.xml:

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="96dp"
                xmlns:tools="http://schemas.android.com/tools"
                android:background="@android:color/black"
                android:theme="@style/ThemeOverlay.AppCompat.Dark">


    <TextView
        android:id="@+id/tv_logged_user_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        tools:text="@string/login_placeholder_email"
        android:textAllCaps="true"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        android:textSize="20sp"/>


</RelativeLayout>

Как я могу получить этот tv_logged_user_email TextView в привязке, чтобы у меня было:

**tvLoggedUserEmail = binding.tvLoggedUserEmail;**
4b9b3361

Ответ 1

Обновленное решение (13/11/2015)

Решение. Обновите библиотеку поддержки проектирования до версии 23.1.1.

Изменения в библиотеке поддержки проектирования 23.1.1:

  • Добавлен метод getHeaderView в класс NavigationView.
  • Исправлена проблема с прозрачным фоном для объекта FloatingActionButton на устройствах под управлением Android 4.0 (уровень API 15) и ниже. (Выпуск 183315).

См. Https://developer.android.com/tools/support-library/index.html для получения дополнительной информации.


Оригинальное решение

Я не знаю, почему нет метода, который обеспечивает представление заголовка, присоединенного программно.

Вместо этого здесь два решения:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = navigationView.inflateHeaderView(R.layout.header_layout)
ImageView iv = (ImageView)headerview.findViewById(R.id.your_image_view)

Или же:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, navigationView, false);
navigationView.addHeaderView(headerView);

ImageView iv = (ImageView) headerView.findViewById(R.id.yourImageView)

Ответ 2

У меня есть немного более чистое решение, где мне не нужно "загрязнять" фрагмент/активность кодом, ответственным за раздувание и добавлением заголовка в NavigationView. Я применил класс расширения для NavigationView. Я сделал это вот так:

Макет моей деятельности:

<data>
    <variable
        name="dashboard"
        type="ramps.view.model.DashboardScreenViewModel"/>
</data>

<android.support.v4.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/activity_dashboard"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        bind:dashboard="@{dashboard}"
        />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:background="@color/white"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:navigationItemSelectedListener="@{dashboard.onMenuItemSelected}"
        app:model="@{dashboard.score}"
        app:menu="@menu/activity_main_drawer"/>

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

Как вы можете видеть, нет приложения: headerLayout в NavigationView, но я добавил свое пользовательское приложение: model, с данными, которые я хочу передать в макет заголовка. Как я определил этот настраиваемый параметр? Класс расширения:

public class NavigationViewExtensions {

@BindingAdapter({"bind:model"})
public static void loadHeader(NavigationView view, ScoreViewModel model) {
    ViewNavigationHeaderBinding binding = ViewNavigationHeaderBinding.inflate(LayoutInflater.from(view.getContext()));
    binding.setScore(model);
    binding.executePendingBindings();
    view.addHeaderView(binding.getRoot());
  }
}

Просто разместите этот класс в любом месте вашего проекта. И макет моего заголовка:

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

<data>

    <import type="android.view.View"/>

    <variable
        name="score"
        type="ramps.view.model.ScoreViewModel"/>
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/graphite"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin_large"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin_large"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <include
        android:id="@+id/player_details_header_score"
        layout="@layout/view_avatar_score_header"
        bind:score="@{score}"/>

</LinearLayout>

Ответ 3

У меня/была такая же проблема.

Обходной путь состоит в том, чтобы раздуть заголовок и добавить его программно.

Вот так:

DrawerHeaderBinding drawerHeaderBinding = DrawerHeaderBinding.inflate(LayoutInflater.from(navigationView.getContext()));
navigationView.addHeaderView(drawerHeaderBinding.getRoot());
drawerHeaderBinding.tvLoggedUserEmail = "email";
drawerHeaderBinding.executePendingBindings();

Итак, удалите приложение: headerLayout и сделайте это программно. Я действительно думаю, что google должен исправить основную проблему здесь, хотя либо в библиотеке дизайна, либо в библиотеке привязки данных.

Ответ 4

ImageView imageView = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.imageButton);

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if (flag) {
            navigationView.getMenu().clear(); //clear old inflated items.
            navigationView.inflateMenu(R.menu.drawer_view1);
            flag = false;
        } else {
            navigationView.getMenu().clear(); //clear old inflated items.
            navigationView.inflateMenu(R.menu.drawer_view);
            flag = true;
        }
    }
});

Ответ 5

Если вы установите app:headerLayout="@layout/drawer_header, вам не нужно снова раздувать представление. Вы можете использовать .bind вместо .inflate.

Вы можете получить уже надутый заголовок и связать его следующим образом:

View headerView = binding.navigationView.getHeaderView(0);
DrawerHeaderBinding headerBinding = DrawerHeaderBinding.bind(headerView);

Ответ 6

Довольно простое решение здесь. Предположим, вы добавили NavigationView как

<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"
    app:headerLayout="@layout/my_nav_drawer_header"
    app:menu="@menu/menu_nav_drawer"/>

И что вы хотите получить доступ к TextView в my_nav_drawer_header.xml который имеет идентификатор textView2. Таким образом, вы просто используете этот код в своей деятельности:

View headerContainer = navigationView.getHeaderView(0); // This returns the container layout in your navigation drawer header layout file (e.g., the parent RelativeLayout/LinearLayout in your my_nav_drawer_header.xml file)
TextView textView2 = (TextView)headerContainer.findViewById(R.id.textView2);
textView2.setText("Sorted!");

Нет необходимости раздувать или мешать существующему коду и т.д.

Ответ 7

Попробуйте это с помощью библиотеки DataBinding, она работает для меня.

navigation_view_header.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/navigation_view_header_height"
    android:paddingLeft="@dimen/navigation_view_padding"
    android:paddingTop="@dimen/navigation_view_top_padding"
    android:background="@color/colorPrimary">


    <ImageView
        android:id="@+id/avatar"
        android:layout_width="@dimen/avatar_dimen"
        android:layout_height="@dimen/avatar_dimen"
        android:contentDescription="@null"
        android:src="@drawable/default_avatar" />

    <TextView
        android:id="@+id/profile_email"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_below="@+id/avatar"
        android:text="email"
        android:textColor="@color/white"
        android:layout_alignParentBottom="true"
        android:gravity="center_vertical" />

</RelativeLayout>
</layout>

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="navigationItemSelectedListener"
            type="com.example.MainActivity"/>
    </data>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:tag="layout">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:titleTextColor="@color/white"
            android:background="@color/colorPrimary"
            app:theme="@style/Toolbar.Theme"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="@dimen/navigation_view_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:theme="@style/Theme.AppCompat.Light"
        app:menu="@menu/drawer_menu"
        app:navigationItemSelectedListener="@{navigationItemSelectedListener::onNavigationItemSelected}"/>

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

и внутри вашей деятельности:

ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,
                R.layout.activity_main);
activityMainBinding.setNavigationItemSelectedListener(this);/* with this line navigation menu item selection events are handled in onNavigationItemSelected() specified in navigation_view_header.xml*/

NavigationViewHeaderBinding navigationViewHeaderBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.navigation_view_header,activityMainBinding.navigationView,false);
        activityMainBinding.navigationView.addHeaderView(navigationViewHeaderBinding.getRoot());

и убедитесь, что ваша активность реализует NavigationView.OnNavigationItemSelectedListener

Ответ 8

  • Измените файл gradle, чтобы обновить библиотеки com.android.support до версии 23.1.1 или далее.

  • Используйте navigationView.getHeaderView(i), где i - это индекс headerview. Если вы только что определили этот вид в макете, это 0.

Ответ 9

У меня есть обновленные инструменты сборки из Android sdk manager, тогда 23.1.0 также отлично работает для меня.

Я использую buildToolsVersion "23.0.2" До этого это было 23.0.1.

и нет необходимости использовать:

(View) navigationView.findViewById(R.id.idOfViewFromHeaderView);

В своей деятельности вы можете напрямую использовать:

(View) findViewById(R.id.idOfViewFromHeaderView);

Ответ 10

Просто поместите navigationView.getHeaderView(0), затем используйте любые представления

        TextView profile,info;
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        profile = navigationView.getHeaderView(0).findViewById(R.id.profile);
        info = navigationView.getHeaderView(0).findViewById(R.id.info);
        profile.setOnClickListener(this);
        info.setOnClickListener(this);

Ответ 11

Это работает для меня.

MainActivity.java:

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    NavHeaderMainBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.nav_header_main, navigationView, false);

    navigationView.addHeaderView(binding.getRoot());

activity_main.xml:

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

    <RelativeLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include layout="@layout/toolbar" />

        </android.support.design.widget.AppBarLayout>

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/activity_main_drawer" />

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

nav_header.xml:

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

<data>

    <variable
        name="account"
        type="lonja.dreamteam.su.trainingdiary.view_model.AccountViewModel"/>

</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/header_background"
    android:gravity="bottom"
    android:orientation="vertical"
    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">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/imageView"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:src="@{account.sex}"
        app:civ_border_color="@color/colorAccent"
        app:civ_border_width="0dp" />

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

</LinearLayout>

Ответ 12

Вы можете это сделать. Сначала инициализируйте навигационное представление.

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);

а затем инициализируйте все виды, которые находятся внутри навигации.

    CircleImageView circleView = (CircleImageView) navigationView.findViewById(R.id.circleView);
    TextView name = (TextView) navigationView.findViewById(R.id.name);
    TextView email = (TextView) navigationView.findViewById(R.id.email);
    LinearLayout header = (LinearLayout) navigationView.findViewById(R.id.header);

а затем вы можете использовать эти представления по своему усмотрению. например:

        name.setText(NAME);
        email.setText(EMAIL);