安利一波

1
2
3
compile('me.drakeet.multitype:multitype:2.5.0', {
exclude group: 'com.android.support'
})

基本文档github主页写的较好

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class EmptyDataViewProvider extends ItemViewBinder<EmptyData,EmptyDataViewProvider.ViewHolder>{


@NonNull
@Override
protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
return new ViewHolder(inflater.inflate(R.layout.bm_list_empty_view,parent,false));
}

@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull EmptyData item) {
if (TextUtils.isEmpty(item.getMessage())){
holder.messageTextView.setText("你来到一片荒芜之地, 空空如也, 什么都没有\\n实在是太冷清了╮(╯_╰)╭");
}else{
holder.messageTextView.setText(item.getMessage());
}

}

static class ViewHolder extends RecyclerView.ViewHolder {
private TextView messageTextView;

public ViewHolder(View itemView) {
super(itemView);
this.messageTextView = (TextView) itemView.findViewById(R.id.bm_empty_text);
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new MultiTypeAdapter();
mAdapter.register(EmptyData.class,new EmptyDataViewProvider());
mAdapter.register(SendExpressOrder.class,new OrderViewProvider(new OrderViewProvider.OnItemClickListener() {
@Override
public void onItemClick(View view, int position, SendExpressOrder order) {
Intent intent=new Intent();
intent.setClass(getActivity(), JijianSearchDetailActivity.class);
intent.putExtra("order", order);
startActivity(intent);
}
}));
mRecyclerView.setAdapter(mAdapter);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
searchData();
}
}, 1000);
}
});

下拉刷新

针对LinearLayoutManager

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public abstract class OnLoadMoreListener extends RecyclerView.OnScrollListener{
private LinearLayoutManager layoutManager;
private int itemCount, lastPosition, lastItemCount;

public abstract void onLoadMore();

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

itemCount = layoutManager.getItemCount();
lastPosition = layoutManager.findLastCompletelyVisibleItemPosition();
} else {
L.e("OnLoadMoreListener", "The OnLoadMoreListener only support LinearLayoutManager");
return;
}

if ( lastPosition == itemCount - 1) {
lastItemCount = itemCount;
this.onLoadMore();
}
L.d("itemCount",itemCount+"");
L.d("lastPosition",lastPosition+"");
L.d("lastItemCount",lastItemCount+"");
}
}

暴露子视图接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class PayMentTempLateProvider extends ItemViewBinder<PayMentTempLatePO,PayMentTempLateProvider.ViewHolder> {



public PayMentTempLateProvider(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener=onItemClickListener;
}

public interface OnItemClickListener{
void onItemClick(View view,int position,PayMentTempLatePO payMentTempLatePO);
}

private OnItemClickListener mOnItemClickListener;


@NonNull
@Override
protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
return new ViewHolder(inflater.inflate(R.layout.item_express_template,parent,false));
}

@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull PayMentTempLatePO item) {
holder.templateName_tv.setText(item.getTemplateName());
holder.templatCode.setText(item.getTemplateCode());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(holder.itemView,holder.getLayoutPosition(),item);
}
});

}

class ViewHolder extends RecyclerView.ViewHolder {
private TextView templateName_tv;
private TextView templatCode;
public ViewHolder(View itemView) {
super(itemView);
templateName_tv = (TextView) itemView.findViewById(R.id.templateName);
templatCode = (TextView) itemView.findViewById(R.id.templateCode);
}
}
}

更新ui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Override
public void onCompleted() {
QujianFragment.getInstance().setWeiquItemText(count);
}

@Override
public void onError(Throwable e) {
L.e(e.getMessage());
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
Items items = new Items();
items.add(new EmptyData("请求失败"));
adapter.setItems(items);
adapter.notifyDataSetChanged();


}

@Override
public void onNext(ApiPagesResult<SendExpressOrder> listApiResult) {
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
mRecyclerView.scrollToPosition(0);
if (listApiResult.getFlag().equals("1")) {
Items items = new Items();
if (listApiResult.getContent().getCount() == 0) {
items.add(new EmptyData());
} else {
items.addAll(listApiResult.getContent().getContent());
count = listApiResult.getContent().getCount();

}

adapter.setItems(items);
adapter.notifyDataSetChanged();

}

}