我已经写了这个简单的示例来测试分页库并使用livedata观察PageDlist的更改,但它仅通知观察者一次,当创建LiveData<PagedList<Integer>>
对象时。我使用活动中的一个按钮加载了更多数据,并且PageList对象正确加载了,但没有观察到更改。这是我活动的代码:
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.arch.paging.PagedList;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
Button b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PagedList<Integer> s = mainViewModel.getIntegers().getValue();
s.loadAround(s.size());
}
});
mainViewModel.setUpPaging(1);
mainViewModel.getIntegers().observe(this, new Observer<PagedList<Integer>>() {
@Override
public void onChanged(@Nullable PagedList<Integer> integers) {
//logging some text
Log.i("MyLog","new list Observed");
}
});
}
}
这是我的ViewModel类:
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModel;
import android.arch.paging.LivePagedListBuilder;
import android.arch.paging.PagedList;
public class MainViewModel extends ViewModel {
LiveData<PagedList<Integer>> integers;
public LiveData<PagedList<Integer>> getIntegers() {
return integers;
}
public void setUpPaging(Integer startFrom){
integers = new LivePagedListBuilder<Integer,Integer>(IntegersDataSource.randomNumbersStartingFrom(startFrom),
new PagedList.Config.Builder()
.setPageSize(5)
.setEnablePlaceholders(false)
.build()).build();
}
}
这是我的数据源,它仅生成随机整数:
import android.arch.paging.DataSource;
import android.arch.paging.PageKeyedDataSource;
import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class IntegersDataSource extends PageKeyedDataSource<Integer,Integer> {
private Integer initialInt;
private final Integer BOUND = 300;
public IntegersDataSource(Integer initialInt) {
this.initialInt = initialInt;
}
@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, Integer> callback) {
Random r = new Random();
Integer i = r.nextInt(BOUND - 1) + 1;
List<Integer> l = new ArrayList<>();
l.add(i);
callback.onResult(l,initialInt-1, initialInt+1);
}
@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Integer> callback) {
Random r = new Random();
Integer i = r.nextInt(BOUND - 1) + 1;
List<Integer> l = new ArrayList<>();
l.add(i);
callback.onResult(l, params.key-1);
}
@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Integer> callback) {
Random r = new Random();
Integer i = r.nextInt(BOUND - 1) + 1;
List<Integer> l = new ArrayList<>();
l.add(i);
callback.onResult(l, params.key+1);
}
private static class RandomsFactory extends DataSource.Factory{
Integer srartFrom;
public RandomsFactory(Integer startFrom) {
this.srartFrom = startFrom;
}
@Override
public DataSource create() {
return new IntegersDataSource(srartFrom);
}
}
public static DataSource.Factory<Integer, Integer> randomNumbersStartingFrom(Integer startFrom) {
return new RandomsFactory(startFrom);
}
}
这是我的构建gradle:
implementation "android.arch.paging:runtime:1.0.0-rc1"
我一直多次按下按钮,但只观察到一次(创建时间(。
pagedlistadapter不会反映更改的项目本身。换句话说,PageDlist是不可变的。发生更新时,应在当前数据源上调用invalidate()
。将通过LivePagedListBuilder创建一个新的PageList/DataSource对,并在当前数据源无效时将其通过Livedata传递给观察者。
尝试此代码。
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PagedList<Integer> s = mainViewModel.getIntegers().getValue();
s.getDataSource().invalidate();
}
});
请查看此演示应用程序。
- https://github.com/jungilhan/cheese-aac-paging-sample/tree/tree/item_based_paging