Android自定义控件实战——仿多看阅读平移翻页

来源:互联网 发布:怎么激光编程 编辑:程序博客网 时间:2024/05/16 05:11

Android自定义控件实战——仿多看阅读平移翻页

    转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38728119

之前自己做的一个APP需要用到翻页阅读,网上看过立体翻页效果,不过bug太多了还不兼容。看了一下多看阅读翻页是采用平移翻页的,于是就仿写了一个平移翻页的控件。效果如下:


在翻页时页面右边缘绘制了阴影,效果还不错。要实现这种平移翻页控件并不难,只需要定义一个布局管理页面就可以了。具体实现上有以下难点:

    1、循环翻页,页面的重复利用。

    2、在翻页时过滤掉多点触碰。

    3、采用setAdapter的方式设置页面布局和数据。

下面就来一一解决这几个难点。首先看循环翻页问题,怎么样能采用较少的页面实现这种翻页呢?由于屏幕上每次只能显示一张完整的页面,翻过去的页面也看不到,所以可以把翻过去的页面拿来重复利用,不必每次都new一个页面,所以,我只用了三张页面实现循环翻页。要想重复利用页面,首先要知道页面在布局中序号和对应的层次关系,比如一个父控件的子view的序号越大就位于越上层。循环利用页面的原理图如下:

向右翻页时状态图是这样的,只用了0、1、2三张页面,页面序号为2的位于最上层,我把它隐藏在左边,所以看到的只有页面1,页面0在1下面挡着也看不到,向右翻页时,页面2被滑到屏幕中,这时候把页面0的内容替换成页面2的前一页内容,把它放到之前页面2的位置,这时,状态又回到了初始状态,又可以继续向右翻页了!


向左翻页时是这样的,初始状态还是一样,当页面1被往左翻过时,看到的是页面0,这时候页面0下面已经没有页面了,而页面2已经用不到了,这时候把页面2放到页面0下面,这时候状态又回到了初始状态,就可以继续往左翻页了。


类似于这种循环效果的实现我一直用的解决方案都是将选中的置于最中间,比如原理图中的页面1,每次翻页完成后可见的都是页面1。在滚动选择器PickerView中也是同样的方案。这就解决了页面的重复利用问题了。

解决难点2 翻页时过滤多点触碰这个问题在仿淘宝商品浏览界面中已经解决过了,就是用一个控制变量mEvents过滤掉pointer down或up后到来的第一个move事件。

解决难点3 采用adapter方式设置页面的布局和数据。这个在Android的AdapterView里用到的,但是我没有看它的adapter机制,太复杂了,我就搞了个简单的adapter,如下:

PageAdapter.java:

[java] view plaincopy
  1. package com.jingchen.pagerdemo;  
  2.   
  3. import android.view.View;  
  4.   
  5. public abstract class PageAdapter  
  6. {  
  7.     /** 
  8.      * @return 页面view 
  9.      */  
  10.     public abstract View getView();  
  11.   
  12.     public abstract int getCount();  
  13.   
  14.     /** 
  15.      * 将内容添加到view中 
  16.      *  
  17.      * @param view 
  18.      *            包含内容的view 
  19.      * @param position 
  20.      *            第position页 
  21.      */  
  22.     public abstract void addContent(View view, int position);  
  23. }  
这是一个抽象类,getView()用于返回页面的布局,getCount()返回数据总共需要多少页,addContent(View view, int position)这个是每翻过一页后将会被调用来请求页面数据的,参数view就是页面,position是表明第几页。待会儿会在自定义布局中定义setAdapter方法设置设配器。

    OK,难点都解决了,自定义一个布局叫ScanView继承自RelativeLayout:

ScanView.java:

[java] view plaincopy
  1. package com.jingchen.pagerdemo;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import android.content.Context;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.LinearGradient;  
  9. import android.graphics.Paint;  
  10. import android.graphics.Paint.Style;  
  11. import android.graphics.RectF;  
  12. import android.graphics.Shader.TileMode;  
  13. import android.os.Handler;  
  14. import android.os.Message;  
  15. import android.util.AttributeSet;  
  16. import android.util.Log;  
  17. import android.view.MotionEvent;  
  18. import android.view.VelocityTracker;  
  19. import android.view.View;  
  20. import android.widget.RelativeLayout;  
  21.   
  22. /** 
  23.  * @author chenjing 
  24.  * 
  25.  */  
  26. public class ScanView extends RelativeLayout  
  27. {  
  28.     public static final String TAG = "ScanView";  
  29.     private boolean isInit = true;  
  30.     // 滑动的时候存在两页可滑动,要判断是哪一页在滑动  
  31.     private boolean isPreMoving = true, isCurrMoving = true;  
  32.     // 当前是第几页  
  33.     private int index;  
  34.     private float lastX;  
  35.     // 前一页,当前页,下一页的左边位置  
  36.     private int prePageLeft = 0, currPageLeft = 0, nextPageLeft = 0;  
  37.     // 三张页面  
  38.     private View prePage, currPage, nextPage;  
  39.     // 页面状态  
  40.     private static final int STATE_MOVE = 0;  
  41.     private static final int STATE_STOP = 1;  
  42.     // 滑动的页面,只有前一页和当前页可滑  
  43.     private static final int PRE = 2;  
  44.     private static final int CURR = 3;  
  45.     private int state = STATE_STOP;  
  46.     // 正在滑动的页面右边位置,用于绘制阴影  
  47.     private float right;  
  48.     // 手指滑动的距离  
  49.     private float moveLenght;  
  50.     // 页面宽高  
  51.     private int mWidth, mHeight;  
  52.     // 获取滑动速度  
  53.     private VelocityTracker vt;  
  54.     // 防止抖动  
  55.     private float speed_shake = 20;  
  56.     // 当前滑动速度  
  57.     private float speed;  
  58.     private Timer timer;  
  59.     private MyTimerTask mTask;  
  60.     // 滑动动画的移动速度  
  61.     public static final int MOVE_SPEED = 10;  
  62.     // 页面适配器  
  63.     private PageAdapter adapter;  
  64.     /** 
  65.      * 过滤多点触碰的控制变量 
  66.      */  
  67.     private int mEvents;  
  68.   
  69.     public void setAdapter(ScanViewAdapter adapter)  
  70.     {  
  71.         removeAllViews();  
  72.         this.adapter = adapter;  
  73.         prePage = adapter.getView();  
  74.         addView(prePage, 0new LayoutParams(LayoutParams.MATCH_PARENT,  
  75.                 LayoutParams.MATCH_PARENT));  
  76.         adapter.addContent(prePage, index - 1);  
  77.   
  78.         currPage = adapter.getView();  
  79.         addView(currPage, 0new LayoutParams(LayoutParams.MATCH_PARENT,  
  80.                 LayoutParams.MATCH_PARENT));  
  81.         adapter.addContent(currPage, index);  
  82.   
  83.         nextPage = adapter.getView();  
  84.         addView(nextPage, 0new LayoutParams(LayoutParams.MATCH_PARENT,  
  85.                 LayoutParams.MATCH_PARENT));  
  86.         adapter.addContent(nextPage, index + 1);  
  87.   
  88.     }  
  89.   
  90.     /** 
  91.      * 向左滑。注意可以滑动的页面只有当前页和前一页 
  92.      *  
  93.      * @param which 
  94.      */  
  95.     private void moveLeft(int which)  
  96.     {  
  97.         switch (which)  
  98.         {  
  99.         case PRE:  
  100.             prePageLeft -= MOVE_SPEED;  
  101.             if (prePageLeft < -mWidth)  
  102.                 prePageLeft = -mWidth;  
  103.             right = mWidth + prePageLeft;  
  104.             break;  
  105.         case CURR:  
  106.             currPageLeft -= MOVE_SPEED;  
  107.             if (currPageLeft < -mWidth)  
  108.                 currPageLeft = -mWidth;  
  109.             right = mWidth + currPageLeft;  
  110.             break;  
  111.         }  
  112.     }  
  113.   
  114.     /** 
  115.      * 向右滑。注意可以滑动的页面只有当前页和前一页 
  116.      *  
  117.      * @param which 
  118.      */  
  119.     private void moveRight(int which)  
  120.     {  
  121.         switch (which)  
  122.         {  
  123.         case PRE:  
  124.             prePageLeft += MOVE_SPEED;  
  125.             if (prePageLeft > 0)  
  126.                 prePageLeft = 0;  
  127.             right = mWidth + prePageLeft;  
  128.             break;  
  129.         case CURR:  
  130.             currPageLeft += MOVE_SPEED;  
  131.             if (currPageLeft > 0)  
  132.                 currPageLeft = 0;  
  133.             right = mWidth + currPageLeft;  
  134.             break;  
  135.         }  
  136.     }  
  137.   
  138.     /** 
  139.      * 当往回翻过一页时添加前一页在最左边 
  140.      */  
  141.     private void addPrePage()  
  142.     {  
  143.         removeView(nextPage);  
  144.         addView(nextPage, -1new LayoutParams(LayoutParams.MATCH_PARENT,  
  145.                 LayoutParams.MATCH_PARENT));  
  146.         // 从适配器获取前一页内容  
  147.         adapter.addContent(nextPage, index - 1);  
  148.         // 交换顺序  
  149.         View temp = nextPage;  
  150.         nextPage = currPage;  
  151.         currPage = prePage;  
  152.         prePage = temp;  
  153.         prePageLeft = -mWidth;  
  154.     }  
  155.   
  156.     /** 
  157.      * 当往前翻过一页时,添加一页在最底下 
  158.      */  
  159.     private void addNextPage()  
  160.     {  
  161.         removeView(prePage);  
  162.         addView(prePage, 0new LayoutParams(LayoutParams.MATCH_PARENT,  
  163.                 LayoutParams.MATCH_PARENT));  
  164.         // 从适配器获取后一页内容  
  165.         adapter.addContent(prePage, index + 1);  
  166.         // 交换顺序  
  167.         View temp = currPage;  
  168.         currPage = nextPage;  
  169.         nextPage = prePage;  
  170.         prePage = temp;  
  171.         currPageLeft = 0;  
  172.     }  
  173.   
  174.     Handler updateHandler = new Handler()  
  175.     {  
  176.   
  177.         @Override  
  178.         public void handleMessage(Message msg)  
  179.         {  
  180.             if (state != STATE_MOVE)  
  181.                 return;  
  182.             // 移动页面  
  183.             // 翻回,先判断当前哪一页处于未返回状态  
  184.             if (prePageLeft > -mWidth && speed <= 0)  
  185.             {  
  186.                 // 前一页处于未返回状态  
  187.                 moveLeft(PRE);  
  188.             } else if (currPageLeft < 0 && speed >= 0)  
  189.             {  
  190.                 // 当前页处于未返回状态  
  191.                 moveRight(CURR);  
  192.             } else if (speed < 0 && index < adapter.getCount())  
  193.             {  
  194.                 // 向左翻,翻动的是当前页  
  195.                 moveLeft(CURR);  
  196.                 if (currPageLeft == (-mWidth))  
  197.                 {  
  198.                     index++;  
  199.                     // 翻过一页,在底下添加一页,把最上层页面移除  
  200.                     addNextPage();  
  201.                 }  
  202.             } else if (speed > 0 && index > 1)  
  203.             {  
  204.                 // 向右翻,翻动的是前一页  
  205.                 moveRight(PRE);  
  206.                 if (prePageLeft == 0)  
  207.                 {  
  208.                     index--;  
  209.                     // 翻回一页,添加一页在最上层,隐藏在最左边  
  210.                     addPrePage();  
  211.                 }  
  212.             }  
  213.             if (right == 0 || right == mWidth)  
  214.             {  
  215.                 releaseMoving();  
  216.                 state = STATE_STOP;  
  217.                 quitMove();  
  218.             }  
  219.             ScanView.this.requestLayout();  
  220.         }  
  221.   
  222.     };  
  223.   
  224.     public ScanView(Context context, AttributeSet attrs, int defStyle)  
  225.     {  
  226.         super(context, attrs, defStyle);  
  227.         init();  
  228.     }  
  229.   
  230.     public ScanView(Context context)  
  231.     {  
  232.         super(context);  
  233.         init();  
  234.     }  
  235.   
  236.     public ScanView(Context context, AttributeSet attrs)  
  237.     {  
  238.         super(context, attrs);  
  239.         init();  
  240.     }  
  241.   
  242.     /** 
  243.      * 退出动画翻页 
  244.      */  
  245.     public void quitMove()  
  246.     {  
  247.         if (mTask != null)  
  248.         {  
  249.             mTask.cancel();  
  250.             mTask = null;  
  251.         }  
  252.     }  
  253.   
  254.     private void init()  
  255.     {  
  256.         index = 1;  
  257.         timer = new Timer();  
  258.         mTask = new MyTimerTask(updateHandler);  
  259.     }  
  260.   
  261.     /** 
  262.      * 释放动作,不限制手滑动方向 
  263.      */  
  264.     private void releaseMoving()  
  265.     {  
  266.         isPreMoving = true;  
  267.         isCurrMoving = true;  
  268.     }  
  269.   
  270.     @Override  
  271.     public boolean dispatchTouchEvent(MotionEvent event)  
  272.     {  
  273.         if (adapter != null)  
  274.             switch (event.getActionMasked())  
  275.             {  
  276.             case MotionEvent.ACTION_DOWN:  
  277.                 lastX = event.getX();  
  278.                 try  
  279.                 {  
  280.                     if (vt == null)  
  281.                     {  
  282.                         vt = VelocityTracker.obtain();  
  283.                     } else  
  284.                     {  
  285.                         vt.clear();  
  286.                     }  
  287.                 } catch (Exception e)  
  288.                 {  
  289.                     e.printStackTrace();  
  290.                 }  
  291.                 vt.addMovement(event);  
  292.                 mEvents = 0;  
  293.                 break;  
  294.             case MotionEvent.ACTION_POINTER_DOWN:  
  295.             case MotionEvent.ACTION_POINTER_UP:  
  296.                 mEvents = -1;  
  297.                 break;  
  298.             case MotionEvent.ACTION_MOVE:  
  299.                 // 取消动画  
  300.                 quitMove();  
  301.                 Log.d("index""mEvents = " + mEvents + ", isPreMoving = "  
  302.                         + isPreMoving + ", isCurrMoving = " + isCurrMoving);  
  303.                 vt.addMovement(event);  
  304.                 vt.computeCurrentVelocity(500);  
  305.                 speed = vt.getXVelocity();  
  306.                 moveLenght = event.getX() - lastX;  
  307.                 if ((moveLenght > 0 || !isCurrMoving) && isPreMoving  
  308.                         && mEvents == 0)  
  309.                 {  
  310.                     isPreMoving = true;  
  311.                     isCurrMoving = false;  
  312.                     if (index == 1)  
  313.                     {  
  314.                         // 第一页不能再往右翻,跳转到前一个activity  
  315.                         state = STATE_MOVE;  
  316.                         releaseMoving();  
  317.                     } else  
  318.                     {  
  319.                         // 非第一页  
  320.                         prePageLeft += (int) moveLenght;  
  321.                         // 防止滑过边界  
  322.                         if (prePageLeft > 0)  
  323.                             prePageLeft = 0;  
  324.                         else if (prePageLeft < -mWidth)  
  325.                         {  
  326.                             // 边界判断,释放动作,防止来回滑动导致滑动前一页时当前页无法滑动  
  327.                             prePageLeft = -mWidth;  
  328.                             releaseMoving();  
  329.                         }  
  330.                         right = mWidth + prePageLeft;  
  331.                         state = STATE_MOVE;  
  332.                     }  
  333.                 } else if ((moveLenght < 0 || !isPreMoving) && isCurrMoving  
  334.                         && mEvents == 0)  
  335.                 {  
  336.                     isPreMoving = false;  
  337.                     isCurrMoving = true;  
  338.                     if (index == adapter.getCount())  
  339.                     {  
  340.                         // 最后一页不能再往左翻  
  341.                         state = STATE_STOP;  
  342.                         releaseMoving();  
  343.                     } else  
  344.                     {  
  345.                         currPageLeft += (int) moveLenght;  
  346.                         // 防止滑过边界  
  347.                         if (currPageLeft < -mWidth)  
  348.                             currPageLeft = -mWidth;  
  349.                         else if (currPageLeft > 0)  
  350.                         {  
  351.                             // 边界判断,释放动作,防止来回滑动导致滑动当前页是前一页无法滑动  
  352.                             currPageLeft = 0;  
  353.                             releaseMoving();  
  354.                         }  
  355.                         right = mWidth + currPageLeft;  
  356.                         state = STATE_MOVE;  
  357.                     }  
  358.   
  359.                 } else  
  360.                     mEvents = 0;  
  361.                 lastX = event.getX();  
  362.                 requestLayout();  
  363.                 break;  
  364.             case MotionEvent.ACTION_UP:  
  365.                 if (Math.abs(speed) < speed_shake)  
  366.                     speed = 0;  
  367.                 quitMove();  
  368.                 mTask = new MyTimerTask(updateHandler);  
  369.                 timer.schedule(mTask, 05);  
  370.                 try  
  371.                 {  
  372.                     vt.clear();  
  373.                     vt.recycle();  
  374.                 } catch (Exception e)  
  375.                 {  
  376.                     e.printStackTrace();  
  377.                 }  
  378.                 break;  
  379.             default:  
  380.                 break;  
  381.             }  
  382.         super.dispatchTouchEvent(event);  
  383.         return true;  
  384.     }  
  385.   
  386.     /* 
  387.      * (非 Javadoc) 在这里绘制翻页阴影效果 
  388.      *  
  389.      * @see android.view.ViewGroup#dispatchDraw(android.graphics.Canvas) 
  390.      */  
  391.     @Override  
  392.     protected void dispatchDraw(Canvas canvas)  
  393.     {  
  394.         super.dispatchDraw(canvas);  
  395.         if (right == 0 || right == mWidth)  
  396.             return;  
  397.         RectF rectF = new RectF(right, 0, mWidth, mHeight);  
  398.         Paint paint = new Paint();  
  399.         paint.setAntiAlias(true);  
  400.         LinearGradient linearGradient = new LinearGradient(right, 0,  
  401.                 right + 3600xffbbbbbb0x00bbbbbb, TileMode.CLAMP);  
  402.         paint.setShader(linearGradient);  
  403.         paint.setStyle(Style.FILL);  
  404.         canvas.drawRect(rectF, paint);  
  405.     }  
  406.   
  407.     @Override  
  408.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
  409.     {  
  410.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  411.         mWidth = getMeasuredWidth();  
  412.         mHeight = getMeasuredHeight();  
  413.         if (isInit)  
  414.         {  
  415.             // 初始状态,一页放在左边隐藏起来,两页叠在一块  
  416.             prePageLeft = -mWidth;  
  417.             currPageLeft = 0;  
  418.             nextPageLeft = 0;  
  419.             isInit = false;  
  420.         }  
  421.     }  
  422.   
  423.     @Override  
  424.     protected void onLayout(boolean changed, int l, int t, int r, int b)  
  425.     {  
  426.         if (adapter == null)  
  427.             return;  
  428.         prePage.layout(prePageLeft, 0,  
  429.                 prePageLeft + prePage.getMeasuredWidth(),  
  430.                 prePage.getMeasuredHeight());  
  431.         currPage.layout(currPageLeft, 0,  
  432.                 currPageLeft + currPage.getMeasuredWidth(),  
  433.                 currPage.getMeasuredHeight());  
  434.         nextPage.layout(nextPageLeft, 0,  
  435.                 nextPageLeft + nextPage.getMeasuredWidth(),  
  436.                 nextPage.getMeasuredHeight());  
  437.         invalidate();  
  438.     }  
  439.   
  440.     class MyTimerTask extends TimerTask  
  441.     {  
  442.         Handler handler;  
  443.   
  444.         public MyTimerTask(Handler handler)  
  445.         {  
  446.             this.handler = handler;  
  447.         }  
  448.   
  449.         @Override  
  450.         public void run()  
  451.         {  
  452.             handler.sendMessage(handler.obtainMessage());  
  453.         }  
  454.   
  455.     }  
  456. }  
代码中的注释写的非常多,原理理解了看代码就容易看懂了。写完这个布局后再写一个ScanViewAdapter继承PageAdapter:

[java] view plaincopy
  1. package com.jingchen.pagerdemo;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.content.res.AssetManager;  
  7. import android.graphics.Typeface;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.widget.TextView;  
  11.   
  12. public class ScanViewAdapter extends PageAdapter  
  13. {  
  14.     Context context;  
  15.     List<String> items;  
  16.     AssetManager am;  
  17.   
  18.     public ScanViewAdapter(Context context, List<String> items)  
  19.     {  
  20.         this.context = context;  
  21.         this.items = items;  
  22.         am = context.getAssets();  
  23.     }  
  24.   
  25.     public void addContent(View view, int position)  
  26.     {  
  27.         TextView content = (TextView) view.findViewById(R.id.content);  
  28.         TextView tv = (TextView) view.findViewById(R.id.index);  
  29.         if ((position - 1) < 0 || (position - 1) >= getCount())  
  30.             return;  
  31.         content.setText("    双峰叠障,过天风海雨,无边空碧。月姊年年应好在,玉阙琼宫愁寂。谁唤痴云,一杯未尽,夜气寒无色。碧城凝望,高楼缥缈西北。\n\n    肠断桂冷蟾孤,佳期如梦,又把阑干拍。雾鬓风虔相借问,浮世几回今夕。圆缺睛明,古今同恨,我更长为客。蝉娟明夜,尊前谁念南陌。");  
  32.         tv.setText(items.get(position - 1));  
  33.     }  
  34.   
  35.     public int getCount()  
  36.     {  
  37.         return items.size();  
  38.     }  
  39.   
  40.     public View getView()  
  41.     {  
  42.         View view = LayoutInflater.from(context).inflate(R.layout.page_layout,  
  43.                 null);  
  44.         return view;  
  45.     }  
  46. }  
这里只是我的demo里写的Adapter,也可以写成带更多内容的Adapter。addContent里带的参数view就是getView里面返回的view,这样就可以根据inflate的布局设置内容了,getView返回的布局page_layout.xml如下:

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="@drawable/cover" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/content"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:layout_marginTop="60dp"  
  12.         android:padding="10dp"  
  13.         android:textColor="#000000"  
  14.         android:textSize="22sp" />  
  15.   
  16.     <TextView  
  17.         android:id="@+id/index"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignParentBottom="true"  
  21.         android:layout_centerHorizontal="true"  
  22.         android:layout_marginBottom="60dp"  
  23.         android:textColor="#000000"  
  24.         android:textSize="30sp" />  
  25.   
  26. </RelativeLayout>  
只包含了两个TextView,所以在adapter中可以根据id查找到这两个TextView再给它设置内容。

OK了,MainActivity的布局如下:

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" >  
  4.   
  5.     <com.jingchen.pagerdemo.ScanView  
  6.         android:id="@+id/scanview"  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent" />  
  9.   
  10. </RelativeLayout>  
很简单,只包含了ScanView。

MainActivity的代码:

[java] view plaincopy
  1. package com.jingchen.pagerdemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.Menu;  
  9.   
  10. public class MainActivity extends Activity  
  11. {  
  12.     ScanView scanview;  
  13.     ScanViewAdapter adapter;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState)  
  17.     {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         scanview = (ScanView) findViewById(R.id.scanview);  
  21.         List<String> items = new ArrayList<String>();  
  22.         for (int i = 0; i < 8; i++)  
  23.             items.add("第 " + (i + 1) + " 页");  
  24.         adapter = new ScanViewAdapter(this, items);  
  25.         scanview.setAdapter(adapter);  
  26.     }  
  27.   
  28.     @Override  
  29.     public boolean onCreateOptionsMenu(Menu menu)  
  30.     {  
  31.         getMenuInflater().inflate(R.menu.main, menu);  
  32.         return true;  
  33.     }  
  34.   
  35. }  
给ScanView设置Adapter就可以了。

好啦,仿多看的平移翻页就完成了~

源码下载


0 0
原创粉丝点击