原生recyclerView
gradle
dependencies {
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
}
xml
Activity
//Init
@BindView(R.id.swipe_refresh)
SwipeRefreshLayout swipeRefresh;
@BindView(R.id.rv_content)
RecyclerView rvContent;
private List mList = new ArrayList<>();
private MAdapter mAdapter;
//onCreate
swipeRefresh.setRefreshing(true);
swipeRefresh.setColorSchemeResources(R.color.colorBG);
mAdapter = new MAdapter(mContext, mList);
rvContent.setLayoutManager(new LinearLayoutManager(mContext));
rvContent.setAdapter(mAdapter);
mPresenter.getList(id);
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mPresenter.getList(id);
}
});
mAdapter.setOnItemClickListener(new MAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
mAdapter.notifyItemChanged(position);
}
});
//show
@Override
public void showList(List list) {
if(swipeRefresh.isRefreshing())
swipeRefresh.setRefreshing(false);
mAdapter.setLists(list);
mAdapter.notifyDataSetChanged();
}
Presenter
@Override
public void getList(Integer id) {
Subscription rxSubscription = mRetrofitHelper.getList(id)
.compose(RxUtil.>>rxSchedulerHelper())
.compose(RxUtil.>handleMyResult())
.subscribe(new Action1>() {
@Override
public void call(List list) {
mView.showList(list);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
mView.showError("数据加载失败ヽ(≧Д≦)ノf");
}
});
addSubscrebe(rxSubscription);
}
Adapter
public class MAdapter extends RecyclerView.Adapter {
private List mList;
private Context mContext;
private OnItemClickListener onItemClickListener;
public MAdapter(Context mContext, List mList) {
this.mList = mList;
this.mContext = mContext;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item, parent, false));
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Glide.with(mContext).load(mList.get(position).getImage())
.placeholder(R.drawable.null_img)
.error(R.drawable.null_img)
.into(((ViewHolder) holder).ivImage);
((ViewHolder) holder).tvNumber.setText(String.valueOf(mList.get(position).getId()));
((ViewHolder) holder).tvType.setText(
mContext.getResources().getStringArray(R.array.type)
[mList.get(position).geType()]);
((ViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(onItemClickListener != null) {
onItemClickListener.onItemClick(holder.getAdapterPosition());
}
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
public void setLists(List list) {
mList.clear();
mList.addAll(list);
}
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.iv_image)
ImageView ivImage;
@BindView(R.id.tv_number)
TextView tvNumber;
@BindView(R.id.tv_type)
TextView tvType;
ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
R.layout.item
PS:多布局Adapter
public class ImageAdapter extends RecyclerView.Adapter {
private Context mContext;
private List mList;
private OnItemClickListener onItemClickListener;
public interface OnItemClickListener {
void onItemClickDefaultPole(View view);
void onItemClickDefaultSign(View view);
void onItemClick(View view, Integer position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public ImageAdapter(Context mContext, List mList) {
this.mContext = mContext;
this.mList = mList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
if(viewType == 1) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_image_default, parent, false);
viewHolder = new ViewHolderDefaultPole(view);
} else if(viewType == 2) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_image_default, parent, false);
viewHolder = new ViewHolderDefaultSign(view);
} else if(viewType == 3) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_image, parent, false);
viewHolder = new ViewHolder(view);
}
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof ViewHolder) {
((ViewHolder) holder).ivRecycler.setImageBitmap(mList.get(position).getImage());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListener != null) {
onItemClickListener.onItemClick(holder.itemView, position);
}
}
});
} else if(holder instanceof ViewHolderDefaultPole) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListener != null) {
onItemClickListener.onItemClickDefaultPole(holder.itemView);
}
}
});
} else if(holder instanceof ViewHolderDefaultSign) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListener != null) {
onItemClickListener.onItemClickDefaultSign(holder.itemView);
}
}
});
}
}
@Override
public int getItemCount() {
return mList.size();
}
@Override
public int getItemViewType(int position) {
int result = 0;
if(mList.get(position).getFlag() == 1)
result = 1;
else if(mList.get(position).getFlag() == 2)
result = 2;
else if(mList.get(position).getFlag() == 3)
result = 3;
return result;
}
static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.iv_recycler)
ImageView ivRecycler;
ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
static class ViewHolderDefaultPole extends RecyclerView.ViewHolder {
@BindView(R.id.iv_recycler_default)
ImageView ivRecyclerDefault;
ViewHolderDefaultPole(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
static class ViewHolderDefaultSign extends RecyclerView.ViewHolder {
@BindView(R.id.iv_recycler_default)
ImageView ivRecyclerDefault;
ViewHolderDefaultSign(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
懒加载recyclerView
Github原址
gradle
dependencies {
compile 'com.wuxiaolong.pullloadmorerecyclerview:library:1.1.2'
}
xml
Activity
//Init
@BindView(R.id. rv_content)
PullLoadMoreRecyclerView rvContent;
private MAdapter mAdapter;
private Integer mCount;
//onCreate
rvContent.setLinearLayout();
rvContent.setColorSchemeResources(R.color.colorBG);
rvContent.setRefreshing(true);
rvContent.setFooterViewText("正在加载");
mAdapter = new MAdapter(mContext);
rvContent.setAdapter(mAdapter);
rvContent.setOnPullLoadMoreListener(new
PullLoadMoreRecyclerView.PullLoadMoreListener() {
@Override
public void onRefresh() {
mCount = 1;
mAdapter.clearData();
mPresenter. getList(id, mCount);
}
@Override
public void onLoadMore() {
mCount++;
mPresenter. getList(id, mCount);
}
});
//onResume
@Override
protected void onResume() {
super.onResume();
mCount = 1;
mAdapter.clearData();
mPresenter. getList(id, mCount);
}
//show
@Override
public void showList(List list) {
mAdapter.addAllData(list);
rvContent.setPullLoadMoreCompleted();
}
Presenter
public void getList(Integer id, Integer mCount) {
Subscription rxSubscription = mRetrofitHelper.getList(id,mCount)
.compose(RxUtil.>>rxSchedulerHelper())
.compose(RxUtil.>handleMyResult())
.subscribe(new Action1>() {
@Override
public void call(List list) {
mView.showList(list);
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
mView.showError("数据加载失败ヽ(≧Д≦)ノf");
}
});
addSubscrebe(rxSubscription);
}
Adapter
public class MAdapter extends RecyclerView.Adapter {
private Context mContext;
private List mList = new ArrayList<>();
public FaultHandleAdapter(Context mContext) {
this.mContext = mContext;
}
public void addAllData(List list) {
mList.addAll(list);
notifyDataSetChanged();
}
public void clearData() {
this.mList.clear();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_fault, parent, false));
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
((MainViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, SomeActivity.class);
intent.putExtra("id", mList.get(holder.getAdapterPosition()).getId());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
static class MainViewHolder extends RecyclerView.ViewHolder {
MainViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
懒加载后台
Controller
@RequestMapping("/getList")
@ResponseBody
public JsonMsgDto getList(Integer id, Integer mCount, Integer mSize) {
JsonMsgDto jsonMsgDto = new JsonMsgDto();
try {
jsonMsgDto.setJsonData(mService. getList(id, mCount, mSize));
jsonMsgDto.setIsSuccess(true);
} catch (Exception e) {
e.printStackTrace();
jsonMsgDto.setIsSuccess(false);
}
return jsonMsgDto;
}
Service
@Override
public List getList(Integer id, Integer mCount, Integer mSize) throws Exception {
return mRepository. getList(id, mCount, mSize);
}
Repository
public List getList(Integer id, Integer mCount, Integer mSize) throws Exception {
String sql = "select m.*, n.* " +
"from tb_m m, tb_n n " +
"where m.p = n.p";
//MDto 的字段必须包含所有的 tb_m 和 tb_n 的字段
Query query = em.createNativeQuery(sql);
query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(MDto.class));
return query.setFirstResult((mCount - 1) * mySize).setMaxResults(mSize).getResultList();
}