BaseRecyclerViewAdapterHelper github地址
BRVAH官方使用指南(持续更新)
BaseRecyclerViewAdapterHelper.wiki
PullToRefreshAndPushDownloadActivity.java
package signin.company.com.recyclerviewdemo.baserecyclerviewadapterhelper.pulltorefresh;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import signin.company.com.recyclerviewdemo.R;
import signin.company.com.recyclerviewdemo.baserecyclerviewadapterhelper.pulltorefresh.loadmore.CustomLoadMoreView;
import signin.company.com.recyclerviewdemo.data.DataServer;
public class PullToRefreshAndPushDownloadActivity extends AppCompatActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
public static void newInstance(Activity activity) {
Intent intent = new Intent(activity,PullToRefreshAndPushDownloadActivity.class);
// intent.putExtra("balance", balance);
// intent.putExtra("mCurrentWithDrawType", mCurrentWithDrawType);
// intent.putExtra("mCurrentWithDrawPayType", mCurrentWithDrawPayType);
activity.startActivity(intent);
}
private RecyclerView mRecyclerView;
private PullToRefreshAdapter pullToRefreshAdapter;
private SwipeRefreshLayout mSwipeRefreshLayout;
private boolean mLoadMoreEndGone = false;
private static final int TOTAL_COUNTER = 18;
private static final int PAGE_SIZE = 6;
private int delayMillis = 1000;
private int mCurrentCounter = 0;//数据条目计数器
private boolean isErr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pull_to_refresh_and_push_download);
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
initAdapter();
addHeadView();
}
@Override
public void onRefresh() {
pullToRefreshAdapter.setEnableLoadMore(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
pullToRefreshAdapter.setNewData(DataServer.getSampleData(PAGE_SIZE));
isErr = false;
mCurrentCounter = PAGE_SIZE;
mSwipeRefreshLayout.setRefreshing(false);
pullToRefreshAdapter.setEnableLoadMore(true);
}
}, delayMillis);
}
private void addHeadView() {
View headView = getLayoutInflater().inflate(R.layout.head_view, (ViewGroup) mRecyclerView.getParent(), false);
headView.findViewById(R.id.iv).setVisibility(View.GONE);
((TextView) headView.findViewById(R.id.tv)).setText("change load view");
headView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLoadMoreEndGone = true;
pullToRefreshAdapter.setLoadMoreView(new CustomLoadMoreView());
mRecyclerView.setAdapter(pullToRefreshAdapter);
Toast.makeText(PullToRefreshAndPushDownloadActivity.this, "change complete", Toast.LENGTH_LONG).show();
}
});
pullToRefreshAdapter.addHeaderView(headView);
}
private void initAdapter() {
pullToRefreshAdapter = new PullToRefreshAdapter();
pullToRefreshAdapter.getData().clear();
pullToRefreshAdapter.setOnLoadMoreListener(this, mRecyclerView);
pullToRefreshAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
View emptyView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) mRecyclerView.getParent(), false);
pullToRefreshAdapter.setEmptyView(emptyView);
// pullToRefreshAdapter.setPreLoadNumber(3);
mRecyclerView.setAdapter(pullToRefreshAdapter);
mCurrentCounter = pullToRefreshAdapter.getData().size();
mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
@Override
public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
Toast.makeText(PullToRefreshAndPushDownloadActivity.this, Integer.toString(position), Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onLoadMoreRequested() {
mSwipeRefreshLayout.setEnabled(false);
if (pullToRefreshAdapter.getData().size() < PAGE_SIZE) {//已经没有更多数据
pullToRefreshAdapter.loadMoreEnd(true);
} else {
// if (mCurrentCounter >= TOTAL_COUNTER) {//总条目
//// pullToRefreshAdapter.loadMoreEnd();//default visible
// //是否显示加载
// pullToRefreshAdapter.loadMoreEnd(mLoadMoreEndGone);//true is gone,false is visible
// } else {
// if (isErr) {//错误样式出现过
// pullToRefreshAdapter.setEnableLoadMore(true);
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
pullToRefreshAdapter.addData(DataServer.getSampleData(PAGE_SIZE));
// mCurrentCounter = pullToRefreshAdapter.getData().size();
pullToRefreshAdapter.loadMoreComplete();
mSwipeRefreshLayout.setEnabled(true);
}
},2000);
//
// } else {
// isErr = true;
// Toast.makeText(PullToRefreshAndPushDownloadActivity.this, R.string.network_err, Toast.LENGTH_LONG).show();
// pullToRefreshAdapter.loadMoreFail();
//
// }
}
// }
}
}
activity_pull_to_refresh_and_push_download.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="signin.company.com.recyclerviewdemo.baserecyclerviewadapterhelper.pulltorefresh.PullToRefreshAndPushDownloadActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
empty_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/id_num"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:gravity="center"
android:paddingLeft="10dp"
android:text="还没有数据" />
</FrameLayout>
PullToRefreshAdapter.java
package signin.company.com.recyclerviewdemo.baserecyclerviewadapterhelper.pulltorefresh;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import signin.company.com.recyclerviewdemo.R;
import signin.company.com.recyclerviewdemo.data.DataServer;
import signin.company.com.recyclerviewdemo.entity.Status;
import signin.company.com.recyclerviewdemo.util.SpannableStringUtils;
import signin.company.com.recyclerviewdemo.util.ToastUtils;
import signin.company.com.recyclerviewdemo.util.Utils;
public class PullToRefreshAdapter extends BaseQuickAdapter<Status, BaseViewHolder> {
public PullToRefreshAdapter() {
super( R.layout.layout_animation, DataServer.getSampleData(10));
}
@Override
protected void convert(BaseViewHolder helper, Status item) {
switch (helper.getLayoutPosition()%
3){
case 0:
helper.setImageResource(R.id.img,R.mipmap.animation_img1);
break;
case 1:
helper.setImageResource(R.id.img,R.mipmap.animation_img2);
break;
case 2:
helper.setImageResource(R.id.img,R.mipmap.animation_img3);
break;
}
helper.setText(R.id.tweetName,"Hoteis in Rio de Janeiro");
String msg="\"He was one of Australia's most of distinguished artistes, renowned for his portraits\"";
( (TextView)helper.getView(R.id.tweetText)).setText(SpannableStringUtils.getBuilder(msg).append("landscapes and nedes").setClickSpan(clickableSpan).create());
( (TextView)helper.getView(R.id.tweetText)).setMovementMethod(LinkMovementMethod.getInstance());
}
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
ToastUtils.showShortToast("事件触发了 landscapes and nedes");
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Utils.getContext().getResources().getColor(R.color.clickspan_color));
ds.setUnderlineText(true);
}
};
}
layout_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="@color/item_bg"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="@mipmap/animation_img1"/>
<TextView
android:id="@+id/tweetName"
android:paddingLeft="@dimen/dp_10"
style="@style/tweetName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chad"
android:layout_marginTop="@dimen/dp_10"
android:layout_below="@+id/img"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:id="@+id/tweetText"
android:paddingLeft="@dimen/dp_10"
style="@style/tweetText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world!"
android:textColor="@android:color/darker_gray"
android:layout_below="@+id/tweetName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingRight="@dimen/dp_10"
android:paddingBottom="@dimen/dp_10"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<TextView
android:id="@+id/tweetDate"
style="@style/tweetDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="04/06/13"
android:paddingRight="@dimen/dp_10"
android:layout_alignBaseline="@+id/tweetName"
android:layout_alignBottom="@+id/tweetName"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>