基本使用
"match_parent"
android:layout_height="match_parent">
"match_parent"
android:layout_height="wrap_content">
"match_parent"
android:layout_height="wrap_content"
android:minHeight="0dp"
app:expandedTitleMarginEnd="0dp"
app:expandedTitleMarginStart="0dp"
app:layout_scrollFlags="scroll">
"match_parent"
android:layout_height="200dp"
android:background="#ffffff"
android:gravity="center"
android:text="测试"
android:textSize="25dp" />
"match_parent"
android:layout_height="40dp"
android:background="#f5f566" />
"@+id/refresh_loadmore_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
startActivity(Intent(this,Main22Activity::class.java))
val recyclerView = refresh_loadmore_layout.basseRV
recyclerView.layoutManager = PTLLinearLayoutManager()
val strings = ArrayList<String>()
for (i in 0..4) {
strings.add(i.toString() + "")
}
recyclerView.adapter = MyAdapter(this, strings, R.layout.item)
recyclerView.setOnRefreshListener {
recyclerView.postDelayed({
recyclerView.completeRefresh()
val textView = TextView(this@Main2Activity)
textView.layoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80)
textView.setBackgroundColor(Color.parseColor("#ff0000"))
textView.text = "加一个头部"
textView.gravity = Gravity.CENTER
recyclerView.addHeaderView(textView)
}, 3000)
}
recyclerView.setOnLoadListener {
recyclerView.postDelayed({
recyclerView.completeLoad()
val textView = TextView(this@Main2Activity)
textView.layoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80)
textView.setBackgroundColor(Color.parseColor("#ff0000"))
textView.text = "加一个尾部"
textView.gravity = Gravity.CENTER
recyclerView.addFooterView(textView)
}, 3000)
}
}
internal inner class MyAdapter(context: Context, datas: ArrayList, layoutId: Int) : SimpleAdapter(context, datas, layoutId) {
override fun onBindViewHolder(holder: ViewHolder, data: String) {
val view = holder.getView(R.id.tv)
view.text = data
}
}
}
基本使用
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main22"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jimi_wu.ptlrecyclerviewsample.Main22Activity">
"@+id/refresh_loadmore_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
class Main22Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main22)
val strings = ArrayList()
for (i in 0..14) {
strings.add(i.toString() + "")
}
refresh_loadmore_recycler.adapter = MyAdapter(this,strings,R.layout.item)
refresh_loadmore_recycler.layoutManager = PTLLinearLayoutManager()
refresh_loadmore_recycler.setOnRefreshListener {
refresh_loadmore_recycler.postDelayed({
refresh_loadmore_recycler.completeRefresh()
val textView = TextView(this@Main22Activity)
textView.layoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80)
textView.setBackgroundColor(Color.parseColor("#ff0000"))
textView.text = "加一个头部"
textView.gravity = Gravity.CENTER
refresh_loadmore_recycler.addHeaderView(textView)
}, 3000)
}
refresh_loadmore_recycler.setOnLoadListener {
refresh_loadmore_recycler.postDelayed({
strings.add("+1")
strings.add("+2")
strings.add("+3")
refresh_loadmore_recycler.completeLoad()
}, 3000)
}
}
internal inner class MyAdapter(context: Context, datas: ArrayList, layoutId: Int) : SimpleAdapter(context, datas, layoutId) {
override fun onBindViewHolder(holder: ViewHolder, data: String) {
val view = holder.getView(R.id.tv)
view.text = data
}
}
}
/**
* Created by sunsh on 2017/9/28.
*/
public class DefaultRefreshHeaderCreator extends RefreshHeaderCreator {
private View mRefreshView;
private ImageView iv;
private TextView tv;
private int rotationDuration = 200;
private int loadingDuration = 1000;
private ValueAnimator ivAnim;
@Override
public boolean onStartPull(float distance,int lastState) {
if (lastState == PullToRefreshRecyclerView.STATE_DEFAULT ) {
iv.setImageResource(R.drawable.arrow_down);
iv.setRotation(0f);
tv.setText("下拉刷新");
} else if (lastState == PullToRefreshRecyclerView.STATE_RELEASE_TO_REFRESH) {
startArrowAnim(0);
tv.setText("下拉刷新");
}
return true;
}
@Override
public void onStopRefresh() {
if (ivAnim != null) {
ivAnim.cancel();
}
}
@Override
public boolean onReleaseToRefresh(float distance,int lastState) {
if (lastState == PullToRefreshRecyclerView.STATE_DEFAULT ) {
iv.setImageResource(R.drawable.arrow_down);
iv.setRotation(-180f);
tv.setText("松手立即刷新");
} else if (lastState == PullToRefreshRecyclerView.STATE_PULLING) {
startArrowAnim(-180f);
tv.setText("松手立即刷新");
}
return true;
}
@Override
public void onStartRefreshing() {
iv.setImageResource(R.drawable.loading);
startLoadingAnim();
tv.setText("正在刷新...");
}
@Override
public View getRefreshView(Context context, RecyclerView recyclerView) {
mRefreshView = LayoutInflater.from(context).inflate(R.layout.layout_ptr_ptl,recyclerView,false);
iv = (ImageView) mRefreshView.findViewById(R.id.iv);
tv = (TextView) mRefreshView.findViewById(R.id.tv);
return mRefreshView;
}
private void startArrowAnim(float roration) {
if (ivAnim != null) {
ivAnim.cancel();
}
float startRotation = iv.getRotation();
ivAnim = ObjectAnimator.ofFloat(startRotation,roration).setDuration(rotationDuration);
ivAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
iv.setRotation((Float) animation.getAnimatedValue());
}
});
ivAnim.start();
}
private void startLoadingAnim() {
if (ivAnim != null) {
ivAnim.cancel();
}
ivAnim = ObjectAnimator.ofFloat(0,360).setDuration(loadingDuration);
ivAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
iv.setRotation((Float) animation.getAnimatedValue());
}
});
ivAnim.setRepeatMode(ObjectAnimator.RESTART);
ivAnim.setRepeatCount(ObjectAnimator.INFINITE);
ivAnim.setInterpolator(new LinearInterpolator());
ivAnim.start();
}
}
上拉样式与下拉一样继承LoadFooterCreator,不列出了
allprojects {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
}
在项目Module Gradle 中添加依赖
compile 'com.github.sunshaobei:Refreshview:3.9'
github源码地址https://github.com/sunshaobei/Refreshview/blob/master/README.md