原生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
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
Activity
//Init
@BindView(R.id.swipe_refresh)
SwipeRefreshLayout swipeRefresh;
@BindView(R.id.rv_content)
RecyclerView rvContent;
private List<MBean> 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<MBean> 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.<MyHttpResponse<List<MBean>>>rxSchedulerHelper())
.compose(RxUtil.<List<MBean>>handleMyResult())
.subscribe(new Action1<List<MBean>>() {
@Override
public void call(List<MBean> list) {
mView.showList(list);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
mView.showError("數(shù)據(jù)加載失敗ヽ(≧Д≦)ノf");
}
});
addSubscrebe(rxSubscription);
}
Adapter
public class MAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<MBean> mList;
private Context mContext;
private OnItemClickListener onItemClickListener;
public MAdapter(Context mContext, List<MBean> 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<MBean> 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
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:descendantFocusability="blocksDescendants"
app:cardCornerRadius="2dp"
app:cardElevation="2dp"
app:contentPadding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image"
android:layout_width="160dp"
android:layout_height="110dp"
android:scaleType="centerCrop"
android:src="@drawable/null_img"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No:"
android:textSize="15sp"/>
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
<TextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout >
</LinearLayout>
</android.support.v7.widget.CardView>
PS:多布局Adapter
public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private List<ImageFlagBean> 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<ImageFlagBean> 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
<com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView
android:id="@+id/rv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
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<MBean> list) {
mAdapter.addAllData(list);
rvContent.setPullLoadMoreCompleted();
}
Presenter
public void getList(Integer id, Integer mCount) {
Subscription rxSubscription = mRetrofitHelper.getList(id,mCount)
.compose(RxUtil.<MyHttpResponse<List<MBean>>>rxSchedulerHelper())
.compose(RxUtil.<List<MBean>>handleMyResult())
.subscribe(new Action1<List<MBean>>() {
@Override
public void call(List<MBean> list) {
mView.showList(list);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
mView.showError("數(shù)據(jù)加載失敗ヽ(≧Д≦)ノf");
}
});
addSubscrebe(rxSubscription);
}
Adapter
public class MAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private List<MBean> mList = new ArrayList<>();
public FaultHandleAdapter(Context mContext) {
this.mContext = mContext;
}
public void addAllData(List<MBean> 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);
}
}
}
懶加載后臺(tái)
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<MDto> getList(Integer id, Integer mCount, Integer mSize) throws Exception {
return mRepository. getList(id, mCount, mSize);
}
Repository
public List<MDto> 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();
}