this.dataSet.clear();
appendData(dataSet);
}
public void appendData(List dataSet) {//传数据
if (dataSet != null && !dataSet.isEmpty()) {
this.dataSet.addAll(dataSet);
notifyDataSetChanged();
}
}
public List<T> getDataSet() {
return dataSet;
}
@Override
public int getItemCount() {
return dataSet.size();
}
}
然后就是创建被装饰类,继承BaseAdapter,这个类就是很普通的adapter,展示数据
public class MyAdapter extends BaseAdapter {
private Context mContext;
public MyAdapter(Context context) {
mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.list_item_mine, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((MyViewHolder) holder).bind(getDataSet().get(position));
}
static class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
public MyViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.tv_content);
}
public void bind(CharSequence content) {
mTextView.setText(content);
}
}
}
再然后开始写加载更多的adapter的装饰类,这个类只负责处理加载更多的逻辑,负责处理加载更多时的网络调用:
public class LoadMoreAdapterWrapper extends BaseAdapter {
private BaseAdapter mAdapter;
private static final int mPageSize = 10;
private int mPagePosition = 0;
private boolean hasMoreData = true;
private OnLoad mOnLoad;
public LoadMoreAdapterWrapper(BaseAdapter adapter, OnLoad onLoad) {
mAdapter = adapter;//传过来的adapter是需要被装饰的adapter
mOnLoad = onLoad;//内部类,自定义的一个接口,可以回调处理加载更多的数据
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == R.layout.list_item_no_more) {//如果是已记载全部的布局就显示已加载完成
View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new NoMoreItemVH(view);
} else if (viewType == R.layout.list_item_loading) {
View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new LoadingItemVH(view);
} else {
return mAdapter.onCreateViewHolder(parent, viewType);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof LoadingItemVH) {//判断holder类型,如果是加载更多则请求数据
requestData(mPagePosition, mPageSize);
} else if (holder instanceof NoMoreItemVH) {
} else {
mAdapter.onBindViewHolder(holder, position);
}
}
private void requestData(int pagePosition, int pageSize) {
//网络请求,如果是异步请求,则在成功之后的回调中添加数据,并且调用notifyDataSetChanged方法,hasMoreData为true
//如果没有数据了,则hasMoreData为false,然后通知变化,更新recylerview
if (mOnLoad != null) {
mOnLoad.load(pagePosition, pageSize, new ILoadCallback() {
@Override
public void onSuccess() {
notifyDataSetChanged();
mPagePosition = (mPagePosition + 1) * mPageSize;
hasMoreData = true;
}
@Override
public void onFailure() {
hasMoreData = false;
}
});
}
}
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1) {//先判断position是不是最后一个,是的话就加载底部布局
if (hasMoreData) {
return R.layout.list_item_loading;
} else {
return R.layout.list_item_no_more;
}
} else {
return mAdapter.getItemViewType(position);//不是就加在数据源数据
}
}
@Override
public int getItemCount() {
return mAdapter.getItemCount() + 1;//因为有了底部view,所以数量总会比数据源数量多一个
}
static class LoadingItemVH extends RecyclerView.ViewHolder {
public LoadingItemVH(View itemView) {
super(itemView);
}
}
最后
我这里整理了一份完整的学习思维以及Android开发知识大全PDF。
当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。
View.ViewHolder {
public LoadingItemVH(View itemView) {
super(itemView);
}
}
最后
我这里整理了一份完整的学习思维以及Android开发知识大全PDF。
[外链图片转存中…(img-OhOy5e2p-1720083379894)]
当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。