Android Paging简单使用,没有多余库,而且只用java

只依赖这个库
implementation 'android.arch.paging:runtime:1.0.1'

LiveData>LivePagedListBuilder生成,
LivePagedListBuilderDataSource.FactoryPagedList.Config.Builder 生成,
DataSource.FactoryPositionalDataSource生成

PositionalDataSourceDataSource的子类
同时还有PageKeyedDataSourceItemKeyedDataSource

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.paging.DataSource;
import android.arch.paging.LivePagedListBuilder;
import android.arch.paging.PagedList;
import android.arch.paging.PagedListAdapter;
import android.arch.paging.PositionalDataSource;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final MyAdapter mAdapter = new MyAdapter();
        PagedList.Config config = new PagedList.Config.Builder()
                .setPageSize(10)                         //配置分页加载的数量
                .setEnablePlaceholders(false)     //配置是否启动PlaceHolders
                .setInitialLoadSizeHint(10)              //初始化加载的数量
                .build();
        /**
         * LiveData> 用LivePagedListBuilder生成
         * LivePagedListBuilder 用 DataSource.Factory 和PagedList.Config.Builder 生成
         * DataSource.Factory 用 PositionalDataSource
         */
        LiveData> liveData = new LivePagedListBuilder(new MyDataSourceFactory(), config)
//                .setBoundaryCallback(null)
//                .setFetchExecutor(null)
                .build();
        liveData.observe(this,new Observer>() {
            @Override
            public void onChanged(@Nullable PagedList dataBeans) {
                mAdapter.submitList(dataBeans);
            }
        });
        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }



    private class DataBean {
        public int id;
        public String content;
    }

    private class MyDataSourceFactory extends DataSource.Factory{
        @Override
        public DataSource create() {
            return new MyDataSource();
        }
    }

    private class MyDataSource extends PositionalDataSource {

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

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

    }

    /**
     * 假设这里需要做一些后台线程的数据加载任务。
     *
     * @param startPosition
     * @param count
     * @return
     */
    private List loadData(int startPosition,int count) {
        List 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;
    }

    private class MyAdapter extends PagedListAdapter {
        public MyAdapter() {
            super(mDiffCallback);
        }

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

        @Override
        public void onBindViewHolder(MyViewHolder holder,int position) {
            DataBean data = getItem(position);
            holder.text1.setText(String.valueOf(position));
            holder.text2.setText(String.valueOf(data.content));
        }
    }

    private DiffUtil.ItemCallback mDiffCallback = new DiffUtil.ItemCallback() {
        @Override
        public boolean areItemsTheSame(@NonNull DataBean oldItem,@NonNull DataBean newItem) {
            Log.d("DiffCallback","areItemsTheSame");
            return oldItem.id == newItem.id;
        }

        @Override
        public boolean areContentsTheSame(@NonNull DataBean oldItem,@NonNull DataBean newItem) {
            Log.d("DiffCallback","areContentsTheSame");
            return (oldItem == newItem);
        }
    };

    private class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView text1;
        public TextView text2;

        public MyViewHolder(View itemView) {
            super(itemView);

            text1 = itemView.findViewById(android.R.id.text1);
            text1.setTextColor(Color.RED);

            text2 = itemView.findViewById(android.R.id.text2);
            text2.setTextColor(Color.BLUE);
        }
    }
}

你可能感兴趣的:(Android Paging简单使用,没有多余库,而且只用java)