基于 RecyclerView 封装,上拉加载更多的库。
Gradle 脚本引入:
compile 'com.udaye.library:pulltoload:1.0.0'效果图如下
网格布局视图
<com.udaye.library.pullloadlibrary.GridRecyclerView
android:id="@+id/recyclerview"android:layout_width="match_parent"android:layout_height="match_parent"android:clipToPadding="false"android:orientation="vertical"android:padding="@dimen/grid_item_spacing"android:scrollbarStyle="insideOverlay"android:scrollbars="vertical"
/>Java 代码:
GridRecyclerViewmRecyclerView;
GridLayoutManagermGridLayoutManager;
@Nullable@OverridepublicViewonCreateView(LayoutInflaterinflater, @NullableViewGroupcontainer, @NullableBundlesavedInstanceState) {
//todo init viewmRecyclerView = (GridRecyclerView) view.findViewById(R.id.recyclerview);
returnview;
}
@OverridepublicvoidonViewCreated(Viewview, @NullableBundlesavedInstanceState) {
mGridLayoutManager = newGridLayoutManager(getContext(), 3);
mGridLayoutManager.setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup() {
@OverridepublicintgetSpanSize(intposition) {
switch (position % 4) {
case0:
return3;
default:
return1;
}
}
});
mRecyclerView.setLayoutManager(mGridLayoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setOnLoadMoreListener(newOnLoadMoreListener() {
@OverridepublicvoidonLoadMore(SuperRecyclerViewrecyclerView) {
if (isHasNext()) {
recyclerView.showFootProgress();
//requestData()
} else {
recyclerView.showFootProgressEnd();
}
//todo request load more
}
});
//todo other request
}
privatebooleanisHasNext(){
//todo ishasnextreturntrue;
}线性布局方式
<com.udaye.library.pullloadlibrary.LinearRecyclerView
android:id="@+id/rv_recyclerview"android:layout_width="match_parent"android:layout_height="match_parent"android:clipToPadding="false"android:orientation="vertical"android:padding="@dimen/grid_item_spacing"android:scrollbarStyle="insideOverlay"android:scrollbars="vertical"app:spanCount="1" />
Java 代码:
LinearRecyclerViewrecyclerView;
SwipeRefreshLayoutmSwipeRefreshLayout;
@Nullable@OverridepublicViewonCreateView(LayoutInflaterinflater, @NullableViewGroupcontainer, @NullableBundlesavedInstanceState) {
Viewview = inflater.inflate(R.layout.fragment_top250, null);
recyclerView = (LinearRecyclerView) view.findViewById(R.id.rv_recyclerview);
returnview;
}
@OverridepublicvoidonViewCreated(Viewview, @NullableBundlesavedInstanceState) {
LinearLayoutManagerlinearLayoutManager = newLinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setOnLoadMoreListener(newOnLoadMoreListener() {
@OverridepublicvoidonLoadMore(SuperRecyclerViewrecyclerView) {
if (isHasNext()) {
recyclerView.showFootProgress();
//requestData()
} else {
recyclerView.showFootProgressEnd();
}
}
});
}
privatebooleanisHasNext(){
//todo ishasnextreturntrue;
}
/** * Created on 16-6-6. */publicclassCustomerAdapterextendscom.udaye.library.pullloadlibrary.RecyclerViewCommonAdapter<CommonBean.SubjectsBean> {
publicCustomerAdapter(List<CommonBean.SubjectsBean> list, Contextcontext) {
super(context, list, R.layout.view_list_item_home);
}
/***添加 list 集合*/publicvoidupdate(List<CommonBean.SubjectsBean> list) {
addList((ArrayList<CommonBean.SubjectsBean>) list);
}
@OverridepublicvoidonListBindViewHolder(CommonViewHolderholder, intposition) {
finalCommonBean.SubjectsBeanentity = mList.get(position);
Glide.with(mContext)
.load(mList.get(position).getImages().getLarge())
.placeholder(android.R.color.white)
.into((ImageView) holder.getView(R.id.photo));
holder.setText(R.id.tv_movie_name, entity.getTitle());
holder.itemView.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(Viewv) {
mContext.startActivity(MovieDetailActivity.getCallingIntent(mContext, entity.getId()));
}
});
}
}其中 Adapter 必须要继承 RecyclerViewCommonAdapter ,这是一个封装的 adapter 使用起来更方便。
Copyright [brokge]
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
