Android快速開發(fā)庫之room-helper

room-helper

Android日常開發(fā)中都會用到數(shù)據(jù)庫,有些時(shí)候可能需要實(shí)時(shí)觀察數(shù)據(jù)狀態(tài)對列表進(jìn)行刷新或是節(jié)省資源進(jìn)行分頁查詢展示,Google在Jetpack組件中提供了數(shù)據(jù)庫(Room)和分頁(Paging)來幫助我們處理以上問題,而room-helper是對Room和Paging的二次封裝,讓我們在開發(fā)中更簡單的去處理數(shù)據(jù)庫的操作。

對于room和Paging的介紹網(wǎng)上有很多這里不做介紹

room-helper支持自定義分頁加載

    /**
     * 每頁加載多少數(shù)據(jù),必須大于0,這里默認(rèn)20
     *
     * @return 每頁加載數(shù)據(jù)數(shù)量
     */
    protected int setPageSize() {
        return 20;
    }

    /**
     * 距底部還有幾條數(shù)據(jù)時(shí),加載下一頁數(shù)據(jù),默認(rèn)為PageSize
     *
     * @return 距底部數(shù)據(jù)的數(shù)量
     */
    protected int setPrefetchDistance() {
        return -1;
    }

    /**
     * 第一次加載多少數(shù)據(jù),必須是PageSize的倍數(shù),默認(rèn)為PageSize*3
     *
     * @return 第一次加載數(shù)據(jù)的數(shù)量 eg:PageSize*n
     */
    protected int setInitialLoadSizeHint() {
        return -1;
    }

    /**
     * 是否啟用占位符,若為true,則視為固定數(shù)量的item
     *
     * @return 默認(rèn)為true
     */
    protected boolean setEnablePlaceholders() {
        return true;
    }

使用room-helper

1. 在module的Gradle中加入

implementation 'com.liang:room-helper:1.0.7'

//官方庫,具體版本見官方
implementation "android.arch.persistence.room:runtime:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"
implementation 'android.arch.paging:runtime:1.0.1'
implementation "android.arch.paging:common:1.0.1"
implementation "android.arch.paging:rxjava2:1.0.0-rc1"
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"

annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'

2. 主要說明如下,其余見項(xiàng)目源碼

public class UserViewModel extends DataSourceModel<User, UserDao> {

    ...

    @Override
    protected UserDao setDao() {
        App app = getApplication();
        return app.appDatabase.userDao();
    }

    @Override
    protected void onZeroItemsLoaded() {
        Log.e("UserViewModel", "onZeroItemsLoaded: ...");
        //數(shù)據(jù)庫沒查詢到0條數(shù)據(jù),加載網(wǎng)絡(luò)數(shù)據(jù)
        getDataWithNetwork();
    }

    @Override
    protected void onItemAtEndLoaded(User itemAtEnd) {
        Log.e("UserViewModel", "onItemAtEndLoaded: " + itemAtEnd.id);
        //用戶已到達(dá)列表末尾,加載網(wǎng)絡(luò)數(shù)據(jù)
        getDataWithNetwork();
    }

    private void getDataWithNetwork() {
        Toast.makeText(getApplication(), "去網(wǎng)絡(luò)拉取數(shù)據(jù)。。。", Toast.LENGTH_SHORT).show();
        //模擬網(wǎng)絡(luò)加載
        int index = 0;
        List<User> users = new ArrayList<>();
        while (index < 50) {
            index++;
            User user = new User();
            user.name = "網(wǎng)絡(luò)數(shù)據(jù): " + index;
            user.age = index;
            users.add(user);
        }
        insert(users);
    }
    ...
}
public class RoomObserveActivity extends AppCompatActivity {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_room_observe);
        viewModel = DataSourceModel.getViewModel(this, UserViewModel.class);
        adapter = new UserObserveAdapter();
        viewDataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        viewDataBinding.recyclerView.setAdapter(adapter);
        viewModel.getDataObserve().observe(this, new Observer<PagedList<User>>() {
            @Override
            public void onChanged(@Nullable PagedList<User> users) {
                adapter.submitList(users);
            }
        });
    }

    ...
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容