HorizontalScrollView實現多頁左右滑動,scrollview不能滑動

來源:互聯網
上載者:User

HorizontalScrollView實現多頁左右滑動,scrollview不能滑動

先看看效果:

上代碼:

PageView是封裝後的一個類,繼承了HorizontalScrollView。

package com.example.testandrid;import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.view.MotionEvent;import android.view.View;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;public class PageView extends HorizontalScrollView {private int mBaseScrollX;//滑動基準。也就是點擊並滑動之前的x值,以此值計算相對滑動距離。private int mScreenWidth;private int mScreenHeight;private LinearLayout mContainer;private boolean flag;private int mPageCount;//頁面數量public PageView(Context context, AttributeSet attrs) {super(context, attrs);DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();mScreenWidth = dm.widthPixels;mScreenHeight = dm.heightPixels;}/** * 添加一個頁面到最後。 * @param page */public void addPage(View page) {addPage(page, -1);}/** * 添加一個頁面。 * @param page */public void addPage(View page, int index) {if(!flag) {mContainer = (LinearLayout) getChildAt(0);flag = true;}LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mScreenWidth, mScreenHeight);if(index == -1) {mContainer.addView(page, params);} else {mContainer.addView(page, index, params);}mPageCount++;}/** * 移除一個頁面。 * @param index */public void removePage(int index) {if(mPageCount < 1) {return;}if(index<0 || index>mPageCount-1) {return;}mContainer.removeViewAt(index);mPageCount--;}/** * 擷取頁面數量 * @return */public int getPageCount() {return mPageCount;}/** * 擷取相對滑動位置。由右向左滑動,返回正值;由左向右滑動,返回負值。 * @return */private int getBaseScrollX() {return getScrollX() - mBaseScrollX;}/** * 使相對於基準移動x距離。 * @param x x為正值時右移;為負值時左移。 */private void baseSmoothScrollTo(int x) {smoothScrollTo(x + mBaseScrollX, 0);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {int action = ev.getAction();switch (action) {case MotionEvent.ACTION_UP:int scrollX = getBaseScrollX();//左滑,大於一半,移到下一頁if (scrollX > mScreenWidth/2) {baseSmoothScrollTo(mScreenWidth);mBaseScrollX += mScreenWidth;} //左滑,不到一半,返回原位else if (scrollX > 0) {baseSmoothScrollTo(0);} //右滑,不到一半,返回原位else if(scrollX > -mScreenWidth/2) {baseSmoothScrollTo(0);} //右滑,大於一半,移到下一頁else {baseSmoothScrollTo(-mScreenWidth);mBaseScrollX -= mScreenWidth;}return true;}return super.onTouchEvent(ev);}}

接下來是布局,fragment_main.xml:

<LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      >    <!-- pageview裡面必須有LinearLayout,這個寫死了。 --><com.example.testandrid.PageView     android:id="@+id/pageview"    android:layout_width="wrap_content"      android:layout_height="fill_parent"      android:scrollbars="none" >      <LinearLayout        android:layout_width="wrap_content"          android:layout_height="fill_parent"          android:orientation="horizontal" >    </LinearLayout></com.example.testandrid.PageView> </LinearLayout>

最後在Activity裡面調用就行。

package com.example.testandrid;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.LinearLayout;public class MainActivity extends Activity {private LayoutInflater inflater;private PageView mPageView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_main);inflater = LayoutInflater.from(this);mPageView = (PageView) findViewById(R.id.pageview);//增加幾個頁面LinearLayout layout = new LinearLayout(this);layout.setBackgroundColor(Color.BLUE);mPageView.addPage(layout);LinearLayout layout2 = new LinearLayout(this);layout2.setBackgroundColor(Color.YELLOW);mPageView.addPage(layout2);//這裡就是個普通的xml布局檔案LinearLayout view = (LinearLayout) inflater.inflate(R.layout.page1, null);mPageView.addPage(view);//刪除一個頁面//mPageView.removePage(1);}}

代碼裡面的注釋基本上都很清楚了,不多說。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.