欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

自定義橫向的ScrollView - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 技術(shù)分享 >

自定義橫向的ScrollView

發(fā)表時(shí)間:2021-1-10

發(fā)布人:葵宇科技

瀏覽次數(shù):48


介紹一個(gè)自定義橫向的ScrollView,可以監(jiān)聽滑動(dòng)的狀態(tài),可以監(jiān)聽滑動(dòng)到了最左側(cè),正在中間滑動(dòng)和滑到了最右邊。
思路:
  1.根據(jù)ScrollView中第一層的子View,其實(shí)第一層也就一個(gè)View,這是ScrollView規(guÄ«)定的,ScrollView包含的內(nèi)容,必須全部放到一個(gè)子View中。
  2.根據(jù)最第一個(gè)子View的左側(cè)坐標(biāo),右側(cè)坐標(biāo),ScrollView的寬度和橫向滑動(dòng)的距離,來判斷滑動(dòng)的位置。
  3.定義滑動(dòng)位置的監(jiān)聽接口。



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,證明滑動(dòng)到最左邊
		if (view.getLeft() - getScrollX() == 0) {
			onScrollListener.onLeft();
			// 如果為0證明滑動(dòng)到最右邊
		} 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的第一個(gè)孩子,獲取到左側(cè)的坐標(biāo)view.getLeft(),獲取ScrollView在X方向滑動(dòng)的距離:getScrollX(),就是ScrollView和左側(cè)邊緣的距離。獲取第一個(gè)孩子最右側(cè)X的坐標(biāo):view.getRight().ScrllView的寬度:getWidth()
view.getLeft()å’Œview.getLeft()çš„值是不變的。
view.getLeft():ScrollView的寬度,其實(shí)也是屏幕的寬度。

[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 = { "骨科", "婦科", "普外科", "神經(jīng)內(nèi)科", "神經(jīng)科", "神經(jīng)外科", "普外",
			"普內(nèi)", "呼吸科", "消化科", "兒科", "心內(nèi)" };
	private MyScrollView myView;// 自定義的滑動(dòng)view
	private LinearLayout sortliner;// 滑動(dòng)條

	@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, "在中間滑動(dòng)");
			}

			@Override
			public void onRight() {
				Log.d(TAG, "滑動(dòng)到了最右邊");
				Toast.makeText(getApplicationContext(), "滑到了最右邊", 0).show();
			}

			@Override
			public void onLeft() {
				Log.d(TAG, "滑動(dòng)到了最左邊");
				Toast.makeText(getApplicationContext(), "滑到了最左邊", 0).show();
			}
		});
		initScrollChildView();
	}

	/**
	 * 準(zhǔn)備向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++) {// 往二級(jí)分類中加載數(shù)據(jù)
			Button sort = new Button(MainActivity.this);
			sort.setText(arr[i]);
			sort.setTextSize(15);
			sort.setMinHeight(30);
			sort.setPadding(20, 5, 20, 5);
			// 把TextView添加到滑動(dòng)條內(nèi)
			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

相關(guān)案例查看更多

相關(guān)閱讀