use of com.hpu.baserecyclerviewadapter.sample.item.SingleItem in project BaseRecyclerViewAdapter by donghan0912.
the class SingleFragment method onViewCreated.
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swip_refresh);
mRefreshLayout.setColorSchemeResources(R.color.blue_bright, R.color.green_light, R.color.red_light);
mRefreshLayout.setOnRefreshListener(this);
mRefreshLayout.setEnabled(false);
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// 测试数据
final List<BaseItem> testData = new ArrayList<>();
for (int i = 1; i < 20; i++) {
testData.add(new SingleItem(getAvatar()));
}
adapter = new BaseRecyclerViewAdapter<>();
recyclerView.setAdapter(adapter);
// 添加头布局
adapter.addHeader(new SimpleItem(R.layout.layout_head) {
@Override
public void onBindViewHolder(final BaseViewHolder holder, final int position) {
// 绑定数据
}
});
// 添加正在加载布局
adapter.setStatusItem(new SimpleItem(R.layout.layout_loading));
// 模拟网络环境,3秒后加载数据
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// 数据加载成功之后,方可下拉刷新
mRefreshLayout.setEnabled(true);
// adapter加载数据
adapter.setData(testData);
// 如果有更多数据,设置加载更多布局
adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore));
}
}, 3000);
// 添加点击事件
adapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getContext(), position + "", Toast.LENGTH_SHORT).show();
}
});
// 上拉加载更多监听
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
@Override
public void onLoadMore() {
if (!loadMore) {
return;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// 设置加载更多失败
adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore_error) {
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
// 点击重试加载更多事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<BaseItem> list2 = new ArrayList<>();
for (int i = 20; i < 40; i++) {
list2.add(new SingleItem(getAvatar()));
}
list.addAll(list2);
adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore_complete));
loadMore = false;
adapter.addData(list);
}
}, 3000);
}
});
}
});
}
}, 3000);
}
});
}
use of com.hpu.baserecyclerviewadapter.sample.item.SingleItem in project BaseRecyclerViewAdapter by donghan0912.
the class SingleFragment method onRefresh.
@Override
public void onRefresh() {
if (!isRefresh) {
isRefresh = true;
new Handler().postDelayed(new Runnable() {
public void run() {
mRefreshLayout.setRefreshing(false);
isRefresh = false;
List<BaseItem> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(new SingleItem(R.drawable.actor));
}
adapter.insertData(list, 0);
}
}, 3000);
}
}
use of com.hpu.baserecyclerviewadapter.sample.item.SingleItem in project BaseRecyclerViewAdapter by donghan0912.
the class GlidFragment method onViewCreated.
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SwipeRefreshLayout refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swip_refresh);
refreshLayout.setEnabled(false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(manager);
for (int i = 0; i < 20; i++) {
list.add(new SingleItem(getAvatar()));
}
final BaseRecyclerViewAdapter adapter = new BaseRecyclerViewAdapter(list);
recyclerView.setAdapter(adapter);
adapter.addHeader(new SimpleItem(R.layout.layout_head));
adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore));
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return adapter.isExtra(position) || adapter.isHeader(position) || adapter.isFooter(position) || adapter.isStatus(position) ? 2 : 1;
}
});
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
@Override
public void onLoadMore() {
if (!loadMore) {
return;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<BaseItem> list2 = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list2.add(new SingleItem(getAvatar()));
}
list.addAll(list2);
if (list.size() > 40) {
adapter.removeExtraItem();
loadMore = false;
}
adapter.addData(list2);
}
}, 3000);
}
}, 3000);
}
});
}
Aggregations