我已经编写了这个简单的示例来测试paging library并使用LiveData观察对PagedList的更改,但是在创建LiveData<PagedList<Integer>>对象时,它仅通知观察者一次。我在 Activity 中使用按钮加载了更多数据,并且pagedList对象已正确加载,但未观察到更改。这是我的 Activity 代码:

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();
    }

}

这是我的DataSource,为简单起见,它仅生成随机整数:
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不会反射(reflect)更改后的项目本身。换句话说,PagedList是不可变的。您应该在发生更新时在当前数据源上调用invalidate()。将通过LivePagedListBuilder创建一个新的PagedList/DataSource对,当当前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/item_based_paging
  • 关于android - LiveData无法将PagedList对象中的更改通知其观察者,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50019707/

    10-09 13:18