BorderScrollView 继承 ScrollView类。
BorderScrollView 创建一个接口供其他类implements:OnBorderListener。
代码如下:
/**
* 重定义ScrollView
* 定义onTop()、onBottom()判断
* 应用场景:滑动到最下方正动加载等
*/
package net.iwetao.init;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;
/**
* BorderScrollView
* <ul>
* <li>onTop and onBottom response ScrollView</li>
* <li>you can [email protected] #setOnBorderListener(OnBorderListener)} to set your top and bottom response</li>
* </ul>
*
* @author [email protected] 2013-5-21
*/
public class BorderScrollView extends ScrollView {
private OnBorderListener onBorderListener;
private View contentView;
public BorderScrollView(Context context){
super(context);
}
public BorderScrollView(Context context, AttributeSet attrs){
super(context, attrs);
}
public BorderScrollView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
doOnBorderListener();
}
public void setOnBorderListener(final OnBorderListener onBorderListener) {
this.onBorderListener = onBorderListener;
if (onBorderListener == null) {
return;
}
if (contentView == null) {
contentView = getChildAt(0);
}
}
/**
* OnBorderListener, Called when scroll to top or bottom
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-22
*/
public static interface OnBorderListener {
/**
* Called when scroll to bottom
*/
public void onBottom();
/**
* Called when scroll to top
*/
public void onTop();
}
private void doOnBorderListener() {
if (contentView != null && contentView.getMeasuredHeight() <= getScrollY() + getHeight()) {
if (onBorderListener != null) {
onBorderListener.onBottom();
}
} else if (getScrollY() == 0) {
if (onBorderListener != null) {
onBorderListener.onTop();
}
}
}
}
然后在另一个Fragmentactivity引用该子类:
public class FragmentActivity1 extends Fragment implements OnBorderListener
声明变量的时候出现cannot cast 的错误:
/* 以下代码报错:ScrollView cannot be cast to BorderScrollView */
BorderScrollView borderScrollView = (BorderScrollView)this.getActivity().findViewById(R.id.orderlist_scrollview);