80,350
社区成员
发帖
与我相关
我的任务
分享
/**
* 重定义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 {@link #setOnBorderListener(OnBorderListener)} to set your top and bottom response</li>
* </ul>
*
* @author trinea@trinea.cn 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();
}
}
}
}
public class FragmentActivity1 extends Fragment implements OnBorderListener
/* 以下代码报错:ScrollView cannot be cast to BorderScrollView */
BorderScrollView borderScrollView = (BorderScrollView)this.getActivity().findViewById(R.id.orderlist_scrollview);
<?xml version="1.0" encoding="utf-8"?>
<net.iwetao.init.BorderScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:paddingBottom="48dip"
android:id="@+id/orderlist_scrollview">
<!-- 订单列表区域 -->
<LinearLayout android:id="@+id/orderlist_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:orientation="vertical">
</LinearLayout>
</net.iwetao.init.BorderScrollView>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:paddingBottom="48dip"
android:id="@+id/orderlist_scrollview">
<!-- 订单列表区域 -->
<LinearLayout android:id="@+id/orderlist_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:orientation="vertical">
</LinearLayout>
</ScrollView>