Android 实现WP7风格,ViewFlipper加入手势

1.最近看到很多windows phone 的风格挺漂亮的,所以着手做了一个。

2.说一下现实方法吧:主界面,当然要一个ViewFlipper,然后ViewFlipper里面可以放你喜欢多的Layout啊,因为ViewFlipper是一个帧布局的Widget,加入手势会有更好的效果。

最后写几个Animation的XML文件用于滑动特效。根据自己喜欢了,网上教程有很多,大家可以参考下。

3.下面是我的运行效果:

 

 

 下面是主要代码,由于GridView中用到的Adapter是我自己封装baseAdapter的,有很多东西,所以就不给出来了。

  1 package com.MapBee.Act;
  2 
  3 
  4 
  5 
  6 
  7 import java.util.ArrayList;
  8 
  9 import android.app.Activity;
 10 import android.os.Bundle;
 11 import android.view.GestureDetector;
 12 import android.view.GestureDetector.OnGestureListener;
 13 import android.view.MotionEvent;
 14 import android.view.View;
 15 import android.view.View.OnTouchListener;
 16 import android.view.animation.AnimationUtils;
 17 import android.widget.GridView;
 18 import android.widget.ViewFlipper;
 19 
 20 import com.MapBee.Component.Adapter.CommonAdapter;
 21 import com.MapBee.Component.Adapter.WbLayoutInflater;
 22 import com.MapBee.Component.Listener.MyGustureListener;
 23 
 24 public class MyFlipperActivity extends Activity{
 25     /** Called when the activity is first created. */
 26     private ArrayList<Object> contentList;
 27     private ArrayList<Object> contentList1;
 28     private ViewFlipper flipper;
 29     private View itemView;
 30     private View itemView1;
 31     
 32     private GridView fliperGridView;
 33     private GridView fliperGridView1;
 34     private GestureDetector detector;
 35     
 36     @Override
 37     public void onCreate(Bundle savedInstanceState) {
 38         super.onCreate(savedInstanceState);
 39         setContentView(R.layout.main);
 40         contentList=new ArrayList<Object>();
 41         contentList1=new ArrayList<Object>();
 42         //add data 
 43         contentList.add("Contacts");
 44         contentList.add("Phone");
 45         contentList.add("Message");
 46         contentList.add("E-mail");
 47         contentList.add("Game");
 48         contentList.add("Foods");
 49         contentList.add("Game");
 50         contentList.add("Game");
 51         
 52         
 53         contentList1.add("Sports");
 54         contentList1.add("Sports");
 55         contentList1.add("Sports");
 56         contentList1.add("Sports");
 57         contentList1.add("Sports");
 58       
 59         
 60        
 61         
 62         flipper = (ViewFlipper) this.findViewById(R.id.main_ViewFlipper);//主界面
 63         itemView=getItemView();
 64         itemView1=getItemView1();
 65          MyGustureListener guestureListener=new MyGustureListener(this,flipper);
 66          detector = new GestureDetector(guestureListener);
 67         
 68     //set adpt
 69         
 70         fliperGridView=(GridView)itemView.findViewById(R.id.layout_flipview1_gridview);
 71         CommonAdapter adpt=new CommonAdapter(this, R.layout.layout_flipview_item,
 72                 R.id.layout_flipview_item_textName, 0x11, contentList);
 73         adpt.setItem_count(contentList.size());
 74         fliperGridView.setAdapter(adpt);
//这里需要注意,不加这个事件,在Gridview中无法触发手势事件,估计是GridView中的Scroll事件占优的原因吧。纯属个人观点有不同的欢迎交流
75 fliperGridView.setOnTouchListener(new OnTouchListener() { 76 77 public boolean onTouch(View v, MotionEvent event) { 78 // TODO Auto-generated method stub 79 return detector.onTouchEvent(event); 80 } 81 }); 82 83 //这里的CommonAdapter可以不用理会,自己写一个Adapter然后Set一下就OK了 84 fliperGridView1=(GridView)itemView1.findViewById(R.id.layout_flipview2_gridview); 85 CommonAdapter adapter=new CommonAdapter(this, R.layout.layout_flipview_item, 86 R.id.layout_flipview_item_textName, 0x11, contentList1); 87 adapter.setItem_count(contentList1.size()); 88 fliperGridView1.setAdapter(adapter);
 //这里需要注意,不加这个事件,在Gridview中无法触发手势事件,估计是GridView中的Scroll事件占优的原因吧。纯属个人观点有不同的欢迎交流
89 fliperGridView1.setOnTouchListener(new OnTouchListener() { 90 91 public boolean onTouch(View v, MotionEvent event) { 92 // TODO Auto-generated method stub 93 return detector.onTouchEvent(event); 94 } 95 }); 96 97 flipper.addView(itemView); 98 flipper.addView(itemView1); 99 100 101 102 103 } 104 105 public View getItemView(){ 106 WbLayoutInflater wli=new WbLayoutInflater(this); 107 wli.setViewFromItemLayout(R.layout.layout_flipview1); 108 return wli.getItemView(); 109 } 110 public View getItemView1(){ 111 WbLayoutInflater wli=new WbLayoutInflater(this); 112 wli.setViewFromItemLayout(R.layout.layout_flipview2); 113 return wli.getItemView(); 114 } 115 116 117 118 119 //这里是注册一个触发手势的事件。 120 public boolean onTouchEvent(MotionEvent event) { 121 122 if(detector.onTouchEvent(event)){ 123 System.out.println("fliperGridView on toch"); 124 return true; 125 } 126 else 127 return false; 128 129 } 130 131 132 133 134 }

 下面是Listener:

 1 package com.MapBee.Component.Listener;
 2 
 3 import android.app.Activity;
 4 import android.view.GestureDetector.OnGestureListener;
 5 import android.view.MotionEvent;
 6 import android.view.animation.AnimationUtils;
 7 import android.widget.ViewFlipper;
 8 
 9 import com.MapBee.Act.R;
10 
11 public class MyGustureListener implements OnGestureListener{
12     private Activity activity;
13     private ViewFlipper flipper;
14     public MyGustureListener(Activity activity,ViewFlipper flipper){
15         this.activity=activity;
16         this.flipper=flipper;
17     }
18     public boolean onDown(MotionEvent e) {
19         // TODO Auto-generated method stub
20         System.out.println("ondown");
21         return true;
22     }
23     //这是监听器主要的部分吧。手势要做什么就在这里写什么吧。 
24     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
25             float velocityY) {
26         if (e1.getX() - e2.getX() > 120) {
27             flipper.setInAnimation(AnimationUtils.loadAnimation(activity, R.anim.push_left_in));
28             flipper.setOutAnimation(AnimationUtils.loadAnimation(activity, R.anim.push_left_out));
29             flipper.showNext();
30             return true;
31         } else if (e1.getX() - e2.getX() < -120) {
32             flipper.setInAnimation(AnimationUtils.loadAnimation(activity, R.anim.push_right_in));
33             flipper.setOutAnimation(AnimationUtils.loadAnimation(activity, R.anim.push_right_out));
34             flipper.showPrevious();
35             return true;
36         }
37         return false;
38     }
39 
40     public void onLongPress(MotionEvent e) {
41         // TODO Auto-generated method stub
42         
43     }
44 
45     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
46             float distanceY) {
47         // TODO Auto-generated method stub
48         return true;
49     }
50 
51     public void onShowPress(MotionEvent e) {
52         // TODO Auto-generated method stub
53         
54     }
55 
56     public boolean onSingleTapUp(MotionEvent e) {
57         // TODO Auto-generated method stub
58         return true;
59     }
60 
61 }

 

posted @ 2012-04-11 15:08  JarrahWu  阅读(1263)  评论(1编辑  收藏  举报