小编kri*_*hna的帖子

如何使用数据绑定访问包含的布局中的视图

我有content_main布局

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

<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

和activity_main

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.CoordinatorLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main"
        android:id="@+id/content"
        app:foo="@{1}">
    </include>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

现在在Java中我有

ActivityMainBinding binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
Run Code Online (Sandbox Code Playgroud)

现在我想访问content_main中的textview作为

binding.content.textView
Run Code Online (Sandbox Code Playgroud)

我尝试在布局标记中使用content_main但它不起作用.我也按照这个链接但它没有用

我怎样才能做到这一点?

android android-layout android-databinding

7
推荐指数
1
解决办法
1792
查看次数

android恢复在backstack中的片段的实例状态

嗨,我有一个有两个片段的活动.

  1. 片段A.
  2. 片段B

    • 片段A有一个EditText和一个ListView.
    • 一旦我在EditText中输入内容并点击Enter,我就会填充ListView.
    • 现在片段A的ListView填充了数据.
    • 单击片段A的ListView上的任何项将将用户发送到片段B.
    • 此时我正在用片段B替换片段A.
    • 因此,当用户按下后退按钮时,他会回到片段A

现在的问题是,如果用户在片段B中并且配置更改发生像屏幕旋转等不止一次,那么我的ListView是空的,因为我的arraylist为null.

请注意,我在片段A和片段B中使用onSavedInstanceState.如果我的当前片段是片段A并且发生配置更改,那么恢复状态就没有问题,因为在onCreateView中我从bundle获取了arraylist.

我知道当我从片段B回到之前在backstack中的片段A时,我的arraylist为空的原因.当片段A在backstack中时,被调用的唯一方法是onSaveInstanceState,因此在第一次配置更改后我的arraylist字段为null因为我无法将存储在savedInstateState包中的arraylist分配给arraylist字段.

我不想在清单中使用android:configchanges属性.

我的问题是如何恢复backstack中片段的状态.

java android listview android-listview android-fragments

5
推荐指数
1
解决办法
842
查看次数