LiveData无法通知其观察者PagedList对象的更改

时间:2018-04-25 10:17:12

标签: android viewmodel pagedlist android-livedata android-paging

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

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"

我一直按下按钮多次,但只观察了一次(创作时间)。

1 个答案:

答案 0 :(得分:0)

PagedListAdapter不会反映更改的项目本身。换句话说,PagedList是不可变的。发生更新时,应在当前DataSource上调用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();    
    }
});

请查看此演示应用。