use of com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter 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.BaseRecyclerViewAdapter 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);
}
});
}
use of com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter in project BaseRecyclerViewAdapter by donghan0912.
the class MultiTypeFragment 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);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
list = new ArrayList<>();
for (int i = 0; i < 6; i++) {
list.add(new FirstItem("第" + i + "条数据"));
list.add(new SecondItem("第" + i + "条数据"));
if (i == 0) {
list.add(new ThirdItem(R.drawable.luoluo_1));
} else if (i == 1) {
list.add(new ThirdItem(R.drawable.luoluo_2));
} else if (i == 2) {
list.add(new ThirdItem(R.drawable.luoluo_3));
} else if (i == 3) {
list.add(new ThirdItem(R.drawable.luoluo_4));
} else if (i == 4) {
list.add(new ThirdItem(R.drawable.luoluo_5));
} else if (i == 5) {
list.add(new ThirdItem(R.drawable.luoluo_6));
}
}
final BaseRecyclerViewAdapter baseRecyclerViewAdapter = new BaseRecyclerViewAdapter();
recyclerView.setAdapter(baseRecyclerViewAdapter);
baseRecyclerViewAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getContext(), position + "", Toast.LENGTH_SHORT).show();
}
});
baseRecyclerViewAdapter.setOnItemLongClickListener(new BaseRecyclerViewAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(View view, int position) {
Toast.makeText(getContext(), position + "", Toast.LENGTH_SHORT).show();
return false;
}
});
baseRecyclerViewAdapter.setStatusItem(new SimpleItem(R.layout.layout_loading));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
baseRecyclerViewAdapter.setStatusItem(new SimpleItem(R.layout.layout_error) {
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
holder.setOnClickListener(R.id.retry, new View.OnClickListener() {
@Override
public void onClick(View v) {
baseRecyclerViewAdapter.setStatusItem(new SimpleItem(R.layout.layout_loading));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
baseRecyclerViewAdapter.setData(list);
}
}, 3000);
}
});
}
});
}
}, 3000);
}
use of com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter in project BaseRecyclerViewAdapter by donghan0912.
the class StaggeredFragment 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);
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
for (int i = 0; i < 20; i++) {
WQ wq = new WQ(getRandomHeight(), getDrawable());
list.add(new FourthItem(wq));
}
final BaseRecyclerViewAdapter adapter = new BaseRecyclerViewAdapter(list);
recyclerView.setAdapter(adapter);
adapter.addHeader(new SimpleItem(R.layout.layout_staggered_head) {
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
p.setFullSpan(true);
}
});
// 设置加载更多布局,并占满整行
adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore) {
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
p.setFullSpan(true);
}
});
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<FourthItem> list2 = new ArrayList<>();
for (int i = 0; i < 21; i++) {
WQ wq = new WQ(getRandomHeight(), getDrawable());
list2.add(new FourthItem(wq));
}
list.addAll(list2);
if (list.size() > 70) {
adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore_complete) {
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
p.setFullSpan(true);
}
});
loadMore = false;
}
adapter.addData(list2);
}
}, 3000);
}
}, 3000);
}
});
}
Aggregations