LifeCycle & Room & dataBinding sample

这个 sample 的简单需求:

获取数据库中的数据, 并展示为 list, 点击后进入详情页

数据库设计

字段名 说明
id primary key
name 名称
description 描述
price 价格

1.entity table表的实体类
使用 arch.room 类进行绑定

@Entity(tableName = "products")
public class ProductEntity implements Product {
    @PrimaryKey
    private int id;
    private String name;
    private String description;
    private int price;
}

2.DAO android 调用的接口
使用 arch.room 进行数据库的使用, 类似retrofit
CRUD
功能1: 获取全部products
功能2: 插入所有
功能3: 根据 id 取1个 product

@Dao
public interface ProductDao {
    @Query("SELECT * FROM products")
    LiveData<List<ProductEntity>> loadAllProducts();

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<ProductEntity> products);

    @Query("select * from products where id = :productId")
    LiveData<ProductEntity> loadProduct(int productId);

}

3.viewModel
arch.lifecycle 的 viewModel

public class ProductViewModel extends AndroidViewModel {
        public ProductViewModel(@NonNull Application application,
                            final int productId) {
        super(application);
        mProductId = productId;

        final DatabaseCreator databaseCreator = DatabaseCreator.getInstance(this.getApplication());

        mObservableComments = Transformations.switchMap(databaseCreator.isDatabaseCreated(), new Function<Boolean, LiveData<List<CommentEntity>>>() {
            @Override
            public LiveData<List<CommentEntity>> apply(Boolean isDbCreated) {
                if (!isDbCreated) {
                    //noinspection unchecked
                    return ABSENT;
                } else {
                    //noinspection ConstantConditions
                    return databaseCreator.getDatabase().commentDao().loadComments(mProductId);
                }
            }
        });
        databaseCreator.createDb(this.getApplication());

    }
}

4.dataBinding
在 gradle 中使用 dataBingding 进行 xml 和 java 的绑定

dataBinding {
        enabled = true
    }

5.list_fragment.xml 和 ProductListFragment.xml 之间的关系
在 listfragment 这页面, 根据数据是否获取成功, 展示 loading 页面和 list 页面

使用@ BindingAdapter("visibleOrGone")
用 java代码实现app:visibleOrGone="@{isLoading}" 逻辑

public class BindingAdapters {
    @BindingAdapter("visibleOrGone")
    public static void showHide(View view, boolean show) {
        view.setVisibility(show ? View.VISIBLE : View.GONE);
    }
}

在.xml 中使用app:visibleOrGone="@{isLoading}"动态判断

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

    <data>
        <variable
            name="isLoading"
            type="boolean" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/cardview_light_background"
        android:orientation="vertical">

        <TextView
            android:id="@+id/loading_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/loading_products"
            android:textAlignment="center"
            app:visibleOrGone="@{isLoading}"
            />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/products_list"
            android:contentDescription="@string/cd_products_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="LinearLayoutManager"
            app:visibleOrGone="@{!isLoading}"/>

    </LinearLayout>
</layout>

6.如何把 entity 和页面对应:
使用

 <variable name="product"
                  type="com.example.android.persistence.model.Product"/>
android:text="@{product.name}"

和 textview 进行一一对应
然后使用

<variable name="callback" type="com.example.android.persistence.ui.ProductClickCallback"/>
android:onClick="@{() ->  callback.onClick(product)}"

和点击事件绑定

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="product"
                  type="com.example.android.persistence.model.Product"/>
        <variable name="callback"
                  type="com.example.android.persistence.ui.ProductClickCallback"/>
    </data>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="@dimen/product_item_min_height"
        android:onClick="@{() ->  callback.onClick(product)}"
        android:orientation="horizontal"
        android:layout_marginStart="@dimen/item_horizontal_margin"
        android:layout_marginEnd="@dimen/item_horizontal_margin"
        app:cardUseCompatPadding="true">

        <RelativeLayout
            android:layout_marginStart="@dimen/item_horizontal_margin"
            android:layout_marginEnd="@dimen/item_horizontal_margin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/cd_product_name"
                android:text="@{product.name}"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="5dp"
                android:text="@{@string/product_price(product.price)}"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/name"
                android:text="@{product.description}"/>
        </RelativeLayout>

    </android.support.v7.widget.CardView>
</layout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容