package zhangphil.test;
import android.databinding.BaseObservable;
import android.databinding.ObservableBoolean;
/**
* Created by Phil on 2017/8/25.
*/
public class ViewModel extends BaseObservable {
public final ObservableBoolean isRefreshing = new ObservableBoolean();
}
写布局,activity_main.xml:
自定义的PhilView.java:
package zhangphil.test;
import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;
/**
* Created by Phil on 2017/8/25.
*/
public class PhilView extends NestedScrollView {
private static final String TAG = "调试";
private static boolean isRefreshing = false;
private static InverseBindingListener mInverseBindingListener;
public PhilView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@BindingAdapter(value = "refreshing", requireAll = false)
public static void setRefreshing(PhilView view, boolean refreshing) {
if (isRefreshing == refreshing) {
//防止死循环
Log.d(TAG, "重复设置");
return;
} else {
Log.d(TAG, "setRefreshing " + refreshing);
isRefreshing = refreshing;
}
}
@InverseBindingAdapter(attribute = "refreshing", event = "refreshingAttrChanged")
public static boolean getRefreshing(PhilView view) {
return isRefreshing;
}
@BindingAdapter(value = {"refreshingAttrChanged"}, requireAll = false)
public static void setRefreshingAttrChanged(PhilView view, final InverseBindingListener inverseBindingListener) {
Log.d(TAG, "setRefreshingAttrChanged");
if (inverseBindingListener == null) {
view.setRefreshingListener(null);
} else {
mInverseBindingListener = inverseBindingListener;
view.setRefreshingListener(mOnRefreshingListener);
}
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if ((y < oldy) && y == 0) {
if (isRefreshing) {
Log.d(TAG, "正在刷新,请勿重复加载");
return;
} else {
longTimeTask();
}
}
}
public void setRefreshingListener(OnRefreshingListener listener) {
this.mOnRefreshingListener = listener;
}
public static abstract class OnRefreshingListener {
public void startRefreshing() {
isRefreshing = true;
mInverseBindingListener.onChange();
}
public void stopRefreshing() {
isRefreshing = false;
mInverseBindingListener.onChange();
}
}
private static OnRefreshingListener mOnRefreshingListener = new OnRefreshingListener() {
@Override
public void startRefreshing() {
super.startRefreshing();
}
@Override
public void stopRefreshing() {
super.stopRefreshing();
}
};
private void longTimeTask() {
new Thread(new Runnable() {
@Override
public void run() {
mOnRefreshingListener.startRefreshing();
try {
//假设这里做了一个长时间的耗时操作
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mOnRefreshingListener.stopRefreshing();
}
}).start();
}
}
package zhangphil.test;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import zhangphil.test.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
ViewModel vm = new ViewModel();
binding.setModel(vm);
}
}
代码运行结果初始化状态:
当下拉时候:
注意,代码有些小问题:只有先向上翻滚,再往下拉时候才会触发头部的刷新滚动提醒。然而这不是本文要解决的重点,随后的文章将改进。