<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:id="@+id/tv_hello" android:layout_height="wrap_content" android:text="@string/hello" /> <com.qin.scrollerview.MultiViewGroup android:id="@+id/mymultiViewGroup" android:layout_below="@id/tv_hello" android:layout_height="wrap_content" android:layout_width="wrap_content"> </com.qin.scrollerview.MultiViewGroup> <Button android:id="@+id/bt_scrollLeft" android:layout_alignParentBottom="true" android:layout_width="200dip" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/bt_scrollRight" android:layout_width="200dip" android:layout_toRightOf="@id/bt_scrollLeft" android:layout_alignParentBottom="true" android:layout_height="wrap_content" android:text="@string/hello" /> </RelativeLayout>
package com.qin.scrollerview; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import android.widget.Scroller; public class MultiScreenActivity extends Activity implements OnClickListener { private Button bt_scrollLeft; private Button bt_scrollRight; private MultiViewGroup mulTiViewGroup; public static int screenWidth; public static int scrrenHeight; private int curscreen = 0; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); screenWidth = metric.widthPixels; scrrenHeight = metric.heightPixels; setContentView(R.layout.multiview); mulTiViewGroup = (MultiViewGroup) findViewById(R.id.mymultiViewGroup); bt_scrollLeft = (Button) findViewById(R.id.bt_scrollLeft); bt_scrollRight = (Button) findViewById(R.id.bt_scrollRight); bt_scrollLeft.setOnClickListener(this); bt_scrollRight.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_scrollLeft: if (curscreen > 0) { curscreen--; } //mulTiViewGroup.scrollTo(curscreen * screenWidth, 0); mulTiViewGroup.beginScroll(curscreen * screenWidth,-screenWidth); break; case R.id.bt_scrollRight: if (curscreen < 2) { curscreen++; } //mulTiViewGroup.scrollTo(curscreen * screenWidth, 0); mulTiViewGroup.beginScroll(curscreen * screenWidth,screenWidth); break; } } }
package com.qin.scrollerview; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Scroller; public class MultiViewGroup extends ViewGroup { private Context mContext; private Scroller mScroller = null; public MultiViewGroup(Context context) { super(context); mContext = context; mScroller = new Scroller(context); init(); } // 在XML 文件中定义的com.qin.scroolerview.MultiViewGroup,会走这个构造方法 public MultiViewGroup(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; mScroller = new Scroller(context); init(); } private void init() { LinearLayout oneLL = new LinearLayout(mContext); oneLL.setBackgroundColor(Color.RED); addView(oneLL); LinearLayout twoLL = new LinearLayout(mContext); twoLL.setBackgroundColor(Color.YELLOW); addView(twoLL); LinearLayout threeLL = new LinearLayout(mContext); threeLL.setBackgroundColor(Color.BLUE); addView(threeLL); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(width, height); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); child.measure(MultiScreenActivity.screenWidth, MultiScreenActivity.scrrenHeight); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int startLeft = 0; int startTop = 10; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); child.layout(startLeft, startTop, startLeft + MultiScreenActivity.screenWidth, startTop + MultiScreenActivity.scrrenHeight); startLeft = startLeft + MultiScreenActivity.screenWidth; } } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } } public void beginScroll(int start,int dx) { mScroller.startScroll(start, 0, dx, 0, 2000); invalidate(); } }