自定义横向的ScrollView - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

自定义横向的ScrollView

发表时间:2021-1-10

发布人:葵宇科技

浏览次数:39


介绍一个自定义横向的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的宽度,其实也是屏幕的宽度。

[img]http://img.blog.csdn.net/20150108111043953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9yd2FyZHl6aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

使用步骤:


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.csdn.net/detail/forwardyzk/8339959
效果图:
[img]http://img.blog.csdn.net/20150108113330250?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9yd2FyZHl6aw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

相关案例查看更多