1、首先设计main.xml文件
<?xml version="1.0" encoding="utf-8"?><ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myFilpper" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/red"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textColor="@drawable/black" android:textSize="30dip"/> <Spinner android:id="@+id/sp1" android:entries="@array/citys" android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ImageView android:id="@+id/im1" android:src="@drawable/icon" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/green"> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textColor="@drawable/black" android:textSize="30dip"/> <Spinner android:id="@+id/sp2" android:entries="@array/citys" android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ImageView android:id="@+id/im2" android:src="@drawable/icon" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/blue"> <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textColor="@drawable/black" android:textSize="30dip"/> <Spinner android:id="@+id/sp3" android:entries="@array/citys" android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ImageView android:id="@+id/im3" android:src="@drawable/icon" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </ViewFlipper>?
2、编写界面切换是的效果(xml)
2.1、push_left_in.xml和push_left_out.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="500" /></set><?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.1" android:duration="500" /></set>?
? 2.2、push_right_in.xml和push_right_out.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="500" /></set><?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="500" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.1" android:duration="500" /></set>??
3、编写activity实现OnGestureListener接口
?
package com.jftt;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.MotionEvent;import android.view.View;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.Spinner;import android.widget.TextView;import android.widget.ViewFlipper;import com.jftt.pojo.City;import com.jftt.pojo.City_Web;import com.jftt.pojo.WebSite;import com.jftt.service.DBManager;public class FlipperDemo extends Activity implements OnGestureListener { private ViewFlipper flipper; private GestureDetector detector; private TextView tv1, tv2, tv3; private Spinner sp1, sp2, sp3; private ImageView im1, im2, im3; private ArrayAdapter<String> adapter; private List<String> citys1, citys2, citys3; private DBManager dbManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dbManager = new DBManager(this); addData(); tv1 = (TextView) findViewById(R.id.tv1); tv2 = (TextView) findViewById(R.id.tv2); tv3 = (TextView) findViewById(R.id.tv3); sp1 = (Spinner) findViewById(R.id.sp1); sp2 = (Spinner) findViewById(R.id.sp2); sp3 = (Spinner) findViewById(R.id.sp3); im1 = (ImageView) findViewById(R.id.im1); im2 = (ImageView) findViewById(R.id.im2); im3 = (ImageView) findViewById(R.id.im3); detector = new GestureDetector(this); flipper = (ViewFlipper) findViewById(R.id.myFilpper); tv1.setText("拉手"); tv2.setText("淘宝"); tv3.setText("阿里巴巴"); citys1 = dbManager.findCitiesByIDs(dbManager.findCityIdByWebID(1)); Log.e("myTag", dbManager.findCityIdByWebID(1).toString()); Log.e("myTag", dbManager.findCitiesByIDs(dbManager.findCityIdByWebID(1)).toString()); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, citys1); // 设置下拉样式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 为下拉列表设置适配器 sp1.setAdapter(adapter); // 定义子元素选择监听器 // 为下拉列表绑定事件监听器 sp1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { im1.setImageResource(getIcon(dbManager .findCityPicByCityName(sp1.getSelectedItem().toString()))); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); citys2 = dbManager.findCitiesByIDs(dbManager.findCityIdByWebID(2)); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, citys2); // 设置下拉样式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 为下拉列表设置适配器 sp2.setAdapter(adapter); // 定义子元素选择监听器 // 为下拉列表绑定事件监听器 sp2.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { im2.setImageResource(getIcon(dbManager .findCityPicByCityName(sp2.getSelectedItem().toString()))); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); citys3 = dbManager.findCitiesByIDs(dbManager.findCityIdByWebID(3)); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, citys3); // 设置下拉样式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 为下拉列表设置适配器 sp3.setAdapter(adapter); // 定义子元素选择监听器 // 为下拉列表绑定事件监听器 sp3.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { im3.setImageResource(getIcon(dbManager .findCityPicByCityName(sp3.getSelectedItem().toString()))); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } private int getIcon(String cityName) { if (cityName.equals("suzhou")) { return R.drawable.suzhou; } if (cityName.equals("beijing")) { return R.drawable.beijing; } if (cityName.equals("zhengzhou")) { return R.drawable.zhengzhou; } if (cityName.equals("guangzhou")) { return R.drawable.guangzhou; } if (cityName.equals("shenzhen")) { return R.drawable.shenzhen; } if (cityName.equals("chengdu")) { return R.drawable.chengdu; } if (cityName.equals("dalian")) { return R.drawable.dalian; } if (cityName.equals("shanghai")) { return R.drawable.shanghai; } if (cityName.equals("wuhan")) { return R.drawable.wuhan; } return 0; } private void addData() { if (dbManager.getCwCount() == 0) {City_Web[] cityWebs = new City_Web[] { new City_Web(1, 1), new City_Web(3, 1), new City_Web(5, 1), new City_Web(7, 1), new City_Web(2, 2), new City_Web(4, 2), new City_Web(6, 2), new City_Web(8, 2), new City_Web(1, 3), new City_Web(3, 3), new City_Web(5, 3), new City_Web(7, 3), new City_Web(9, 3) }; dbManager.insertCws(cityWebs); } if (dbManager.getCityCount() == 0) {City[] cities = new City[] { new City("苏州", "suzhou"), new City("北京", "beijing"), new City("郑州", "zhengzhou"), new City("广州", "guangzhou"), new City("深圳", "shenzhen"), new City("成都", "chengdu"), new City("大连", "dalian"), new City("上海", "shanghai"), new City("武汉", "wuhan"), }; dbManager.insertCities(cities); } if (dbManager.getWebCount() == 0) {WebSite[] webSites = new WebSite[] { new WebSite("拉手", "http://www.lashou.com"), new WebSite("淘宝", "http://www.taobao.com"), new WebSite("阿里巴巴", "http://www.alibaba.com") }; dbManager.insertWebSite(webSites); } } @Override public boolean onTouchEvent(MotionEvent event) { return detector.onTouchEvent(event); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX() - e2.getX() > 120) { this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); this.flipper.showNext(); return true; } else if (e1.getX() - e2.getX() < -120) { this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in)); this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out)); this.flipper.showPrevious(); return true; } return false; } @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; }}
?