通过 DataBinding 控制 UI Recycler View 项目的可见性
Control UI visibility on Recycler View item via DataBinding
我正在 Android 中学习使用数据绑定的 MVVM,但一直坚持在回收站视图中维护项目的 UI 可见性状态。每个出售的商品都有 2 个文本视图 - 标题(可见)和描述(隐藏)。单击标题,我要 show/hide 描述。为了保持其 UI 状态,我在 POJO 中有一个布尔字段(作为实体与 DB 共享)。
单击标题时,我正在尝试设置此字段。如何使更改触发更改可见性并在滚动期间保持可见性?
这是代码的 Github link,下面是我想要使用的代码片段。
遵循带有存储库的 MVVM 架构来访问网络并更新数据库并通过 LiveData 观察数据。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_margin="8dp"
android:text="@{posts.desc}"
android:textSize="14sp"
android:visibility="@{(posts.isDescVisible()) ? View.VISIBLE : View.GONE}"
tools:text="This is an answer provided by the creator to test the UI layout" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:ellipsize="end"
android:maxLines="1"
android:onClick="@{()-> posts.setDescVisible(!posts.isDescVisible())}"
android:text="@{posts.title}"
android:textSize="16sp"
android:textStyle="bold"
tools:text="This is a question" />
</RelativeLayout>
<data>
<import type="android.view.View" />
<variable
name="posts"
type="com.tyagiabhinav.loremipsum.model.db.Posts" />
</data>
</layout>
这很适合我
import androidx.databinding.BaseObservable;
public class Posts extends BaseObservable {
private int id;
private final String title;
public final String desc;
private boolean descVisible;
public Posts(String title, String desc) {
this.title = title;
this.desc = desc;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
public void setDescVisible(boolean descVisible) {
this.descVisible = descVisible;
notifyPropertyChanged(BR.descVisible);
}
@Bindable
public boolean isDescVisible() {
return descVisible;
}
}
还有 xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_margin="8dp"
android:text="@{posts.desc}"
android:textSize="14sp"
***android:visibility="@{posts.descVisible ? View.VISIBLE : View.GONE}"***
tools:text="This is an answer provided by the creator to test the UI layout" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:ellipsize="end"
android:maxLines="1"
android:onClick="@{()-> posts.setDescVisible(!posts.descVisible)}"
android:text="@{posts.title}"
android:textSize="16sp"
android:textStyle="bold"
tools:text="This is a question" />
</RelativeLayout>
<data>
<import type="android.view.View" />
<variable
name="posts"
type="com.tyagiabhinav.loremipsum.model.db.Posts" />
</data>
</layout>
我正在 Android 中学习使用数据绑定的 MVVM,但一直坚持在回收站视图中维护项目的 UI 可见性状态。每个出售的商品都有 2 个文本视图 - 标题(可见)和描述(隐藏)。单击标题,我要 show/hide 描述。为了保持其 UI 状态,我在 POJO 中有一个布尔字段(作为实体与 DB 共享)。
单击标题时,我正在尝试设置此字段。如何使更改触发更改可见性并在滚动期间保持可见性?
这是代码的 Github link,下面是我想要使用的代码片段。
遵循带有存储库的 MVVM 架构来访问网络并更新数据库并通过 LiveData 观察数据。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_margin="8dp"
android:text="@{posts.desc}"
android:textSize="14sp"
android:visibility="@{(posts.isDescVisible()) ? View.VISIBLE : View.GONE}"
tools:text="This is an answer provided by the creator to test the UI layout" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:ellipsize="end"
android:maxLines="1"
android:onClick="@{()-> posts.setDescVisible(!posts.isDescVisible())}"
android:text="@{posts.title}"
android:textSize="16sp"
android:textStyle="bold"
tools:text="This is a question" />
</RelativeLayout>
<data>
<import type="android.view.View" />
<variable
name="posts"
type="com.tyagiabhinav.loremipsum.model.db.Posts" />
</data>
</layout>
这很适合我
import androidx.databinding.BaseObservable;
public class Posts extends BaseObservable {
private int id;
private final String title;
public final String desc;
private boolean descVisible;
public Posts(String title, String desc) {
this.title = title;
this.desc = desc;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
public void setDescVisible(boolean descVisible) {
this.descVisible = descVisible;
notifyPropertyChanged(BR.descVisible);
}
@Bindable
public boolean isDescVisible() {
return descVisible;
}
}
还有 xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_margin="8dp"
android:text="@{posts.desc}"
android:textSize="14sp"
***android:visibility="@{posts.descVisible ? View.VISIBLE : View.GONE}"***
tools:text="This is an answer provided by the creator to test the UI layout" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:ellipsize="end"
android:maxLines="1"
android:onClick="@{()-> posts.setDescVisible(!posts.descVisible)}"
android:text="@{posts.title}"
android:textSize="16sp"
android:textStyle="bold"
tools:text="This is a question" />
</RelativeLayout>
<data>
<import type="android.view.View" />
<variable
name="posts"
type="com.tyagiabhinav.loremipsum.model.db.Posts" />
</data>
</layout>