1. 程式人生 > >Android Jetpack之Paging

Android Jetpack之Paging

Paging開發文件

Kotlin資料比較多,java資料比較少。

大家推薦的demo也是kotlin,不過看起來不太困難。

demo:sunFlower

 

java demo參見:Android Paging簡單使用,沒有多餘庫,而且只用java

擴充套件功能請自行研究。

 

1. 新增依賴

def paging_version = "1.0.0"//目前最新穩定版本

implementation "android.arch.paging:runtime:$paging_version"

2. 資料來源

LiveData<PagedList<DataBean>>

LivePagedListBuilder生成,
LivePagedListBuilder用 DataSource.Factory<Integer,DataBean>PagedList.Config.Builder生成,
DataSource.Factory<Integer,DataBean>用 PositionalDataSource<DataBean>生成

PositionalDataSourceDataSource的子類

LiveData<——LivePagedListBuilder<——DataSource.Factory<Integer,DataBean>

(+PagedList.Config.Builder)<——PositionalDataSource

 mLiveData = new LivePagedListBuilder<>(new MyDataSourceFactory(), config)
                .build();
/*DataSource.Factory*/
    private class MyDataSourceFactory extends DataSource.Factory<Integer, DataBean> {

        @Override
        public DataSource<Integer, DataBean> create() {
            return new MyDataSource();
        }
    }

    /*DataSource*/
    private class MyDataSource extends PositionalDataSource<DataBean> {


        @Override
        public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<DataBean> callback) {
            callback.onResult(loadData(0, 10), 0, 10);
        }

        @Override
        public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<DataBean> callback) {
            callback.onResult(loadData(params.startPosition, 10));
        }
    }

模擬獲取資料

 /**
     * 模擬獲取資料
     *
     * @param startPosition
     * @param count
     * @return
     */
    private List<DataBean> loadData(int startPosition, int count) {
        List<DataBean> list = new ArrayList();

        for (int i = 0; i < count; i++) {
            DataBean data = new DataBean();
            data.id = startPosition + i;
            data.content = "測試的內容=" + data.id;
            list.add(data);
        }
        return list;
    }

3. 資料繫結

 mLiveData.observe(this, new Observer<PagedList<DataBean>>() {

            @Override
            public void onChanged(@Nullable PagedList<DataBean> dataBeans) {
                mMyAdapter.submitList(dataBeans);
            }
        });

4. 建立adapter

mMyAdapter = new MyAdapter(mItemCallback);

DiffUtil判斷資料是否改變。

/**
     * 資料是否改變了
     */
    private DiffUtil.ItemCallback<DataBean> mItemCallback = new DiffUtil.ItemCallback<DataBean>() {
        @Override
        public boolean areItemsTheSame(DataBean oldItem, DataBean newItem) {
            return oldItem.id == newItem.id;
        }

        @Override
        public boolean areContentsTheSame(DataBean oldItem, DataBean newItem) {
            return oldItem == newItem;
        }
    };

adapter需要繼承PagedListAdapter。

public class MyAdapter extends PagedListAdapter<DataBean, MyViewHolder> {
    protected MyAdapter(@NonNull DiffUtil.ItemCallback<DataBean> diffCallback) {
        super(diffCallback);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(InitUtil.getApplicationContext()).inflate(android.R.layout.simple_list_item_2, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        DataBean item = getItem(position);
        holder.getText1().setText("position = " + item.getId());
        holder.getText2().setText(item.getContent());
    }
}
public class MyViewHolder extends RecyclerView.ViewHolder {

    private final TextView mText1;
    private final TextView mText2;

    public MyViewHolder(View itemView) {
        super(itemView);
        mText1 = itemView.findViewById(android.R.id.text1);
        mText2 = itemView.findViewById(android.R.id.text2);
    }

    public TextView getText1() {
        return mText1;
    }

    public TextView getText2() {
        return mText2;
    }
}