public class AsyncListUtilActivity extends Activity {
private static final String TAG = "xiaobo";
private SwipeRefreshLayout mRefreshLayout;
private RecyclerView mRecyclerView;
private View mEmptyView;
private MyAdapter mAdapter;
private LinearLayoutManager mLayoutManager;
private int mFirstVisibleItem = 0;
private int mVisibleItemCount = 0;
private AsyncListUtil<String> mUtil = new AsyncListUtil<>(String.class, 50, new AsyncListUtil
.DataCallback<String>() {
@Override
public int refreshData() {
// query the database to determine the amount of data
Log.d(TAG, "refreshData()");
SystemClock.sleep(1000);
return 200;
}
@Override
public void fillData(String[] data, int startPosition, int itemCount) {
// query the database to get the details of data from startPosition to startPosition+itemCount
for (int i = 0; i < itemCount; i++) {
data[i] = "this is " + (startPosition + i);
SystemClock.sleep(10);
}
Log.d(TAG, "fillData(), startPosition: " + startPosition + ", itemCount: " + itemCount);
}
}, new AsyncListUtil.ViewCallback() {
@Override
public void getItemRangeInto(int[] outRange) {
outRange[0] = mFirstVisibleItem;
outRange[1] = mFirstVisibleItem + mVisibleItemCount;
Log.d(TAG, "getItemRangeInto(), outRange: " + outRange[0] + "," + outRange[1]);
}
@Override
public void onDataRefresh() {
mEmptyView.setVisibility(View.GONE);
mRefreshLayout.setRefreshing(false);
mAdapter.notifyDataSetChanged();
Log.d(TAG, "onDataRefresh()");
}
@Override
public void onItemLoaded(int position) {
mAdapter.notifyItemChanged(position);
Log.d(TAG, "onItemLoaded(), position: " + position);
}
});
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_async_util);
mEmptyView = findViewById(R.id.progress_bar);
mAdapter = new MyAdapter();
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
mUtil.onRangeChanged();
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
mFirstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
mVisibleItemCount = mLayoutManager.findLastVisibleItemPosition() - mFirstVisibleItem + 1;
}
});
mRefreshLayout = findViewById(R.id.swipe_refresh_layout);
mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mUtil.refresh();
}
});
}
private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View root = new TextView(parent.getContext());
return new MyViewHolder(root);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// can be null if the data is loading.
String s = mUtil.getItem(position);
holder.mTextView.setText(null == s ? "loading..." : s);
}
@Override
public int getItemCount() {
return mUtil.getItemCount();
}
}
private static class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
public MyViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView;
mTextView.setTextSize(36);
mTextView.setGravity(Gravity.CENTER);
mTextView.setTextColor(Color.BLACK);
}
}
}