介绍一个自定义横向的ScrollView,可以监听滑动的状态,可以监听滑动到了最左侧,正在中间滑动和滑到了最右边。
思路:
1.根据ScrollView中第一层的子View,其实第一层也就一个View,这是ScrollView规定的,ScrollView包含的内容,必须全部放到一个子View中。
2.根据最第一个子View的左侧坐标,右侧坐标,ScrollView的宽度和横向滑动的距离,来判断滑动的位置。
3.定义滑动位置的监听接口。
public class MyScrollView extends HorizontalScrollView {
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = (View) getChildAt(getChildCount() - 1);
// 如果为0,证明滑动到最左边
if (view.getLeft() - getScrollX() == 0) {
onScrollListener.onLeft();
// 如果为0证明滑动到最右边
} else if ((view.getRight() - (getWidth() + getScrollX())) == 0) {
onScrollListener.onRight();
// 说明在中间
} else {
onScrollListener.onScroll();
}
super.onScrollChanged(l, t, oldl, oldt);
}
/**
* 定义接口
*
* @author admin
*/
public interface OnScrollListener1 {
void onRight();
void onLeft();
void onScroll();
}
private OnScrollListener1 onScrollListener;
public void setOnScrollListener(OnScrollListener1 onScrollListener) {
this.onScrollListener = onScrollListener;
}
}
首先获取ScrollView的第一个孩子,获取到左侧的坐标view.getLeft(),获取ScrollView在X方向滑动的距离:getScrollX(),就是ScrollView和左侧边缘的距离。获取第一个孩子最右侧X的坐标:view.getRight().ScrllView的宽度:getWidth()
view.getLeft()和view.getLeft()的值是不变的。
view.getLeft():ScrollView的宽度,其实也是屏幕的宽度。
使用步骤:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fef4dc"
android:gravity="center"
android:orientation="horizontal" >
<com.example.view.MyScrollView
android:id="@+id/myView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/sortliner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
</LinearLayout>
</com.example.view.MyScrollView>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private final String TAG = MainActivity.class.getSimpleName();
final String[] arr = { "骨科", "妇科", "普外科", "神经内科", "神经科", "神经外科", "普外",
"普内", "呼吸科", "消化科", "儿科", "心内" };
private MyScrollView myView;// 自定义的滑动view
private LinearLayout sortliner;// 滑动条
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
Toast.makeText(getApplicationContext(), ""+screenWidth, 0).show();
}
public void initView() {
myView = (MyScrollView) findViewById(R.id.myView);
sortliner = (LinearLayout) findViewById(R.id.sortliner);
sortliner.removeAllViews();
myView.setOnScrollListener(new OnScrollListener1() {
@Override
public void onScroll() {
Log.d(TAG, "在中间滑动");
}
@Override
public void onRight() {
Log.d(TAG, "滑动到了最右边");
Toast.makeText(getApplicationContext(), "滑到了最右边", 0).show();
}
@Override
public void onLeft() {
Log.d(TAG, "滑动到了最左边");
Toast.makeText(getApplicationContext(), "滑到了最左边", 0).show();
}
});
initScrollChildView();
}
/**
* 准备向Scroller中添加View
*
* @param params
*/
private void initScrollChildView() {
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
for (int i = 0; i < arr.length; i++) {// 往二级分类中加载数据
Button sort = new Button(MainActivity.this);
sort.setText(arr[i]);
sort.setTextSize(15);
sort.setMinHeight(30);
sort.setPadding(20, 5, 20, 5);
// 把TextView添加到滑动条内
sortliner.addView(sort, i, params);
}
}
}
源码下载地址:http://download.youkuaiyun.com/detail/forwardyzk/8339959
效果图: