"> ">
Download presentation
Presentation is loading. Please wait.
1
滑動 建國科技大學 資管系 饒瑞佶
2
利用既有XML畫面 首先加入一個ViewFlipper到xml畫面中 <ViewFlipper
android:layout_width="match_parent" android:layout_height="match_parent" >
3
利用既有XML畫面 其次,將要滑動的版面依序放在ViewFlipper中 <ViewFlipper
android:layout_width="match_parent" android:layout_height="match_parent" > <include /> /> </ViewFlipper> 這個順序就是滑動順序
4
滑動主程式 首先增加OnTouchListener監聽事件 加入物件宣告 加入物件在onCreate
public class Main extends Activity implements OnTouchListener { private ViewFlipper viewFlipper; private float touchDownX;//手指按下去的x座標 private float touchUpX; //手指放開的x座標 //取得ViewFlipper物件 viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1); viewFlipper.setOnTouchListener(this);
5
滑動主程式 需要加入必要的ontouch事件 @Override
public boolean onTouch(View arg0, MotionEvent event) {
6
Ontouch事件 if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 左右滑動時手指按的x座標 touchDownX = event.getX(); return true; } else if (event.getAction() == MotionEvent.ACTION_UP) { // 左右滑動時手指鬆開的x座標 touchUpX = event.getX(); // 從左往右滑動 if (touchUpX - touchDownX > 100) { // 顯示上一個畫面的動畫 viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in)); viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out)); // 顯示上一個畫面 viewFlipper.showPrevious(); // 從右往左滑動 } else if (touchDownX - touchUpX > 100) { //顯示上一個畫面的動畫 R.anim.push_left_in)); R.anim.push_left_out)); // 顯示下一個畫面 viewFlipper.showNext(); }
7
push_left_in.xml <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=" <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" </set>
8
push_left_out.xml <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=" <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.1" </set>
9
push_right_in.xml <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=" <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" </set>
10
push_right_out.xml <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=" <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="500" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.1" </set>
11
加入畫面物件事件 依照原本處理事件方式處理就可以 但ViewFlipper碰到ScrollView會失效
12
ViewPager
13
activity_main.xml <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
14
修改Home.java private ViewPager mViewPager; List<View> viewList;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LayoutInflater mInflater = getLayoutInflater().from(this); View v1 = mInflater.inflate(R.layout.home, null); View v2 = mInflater.inflate(R.layout.bmi, null); View v3 = mInflater.inflate(R.layout.main4, null); //加入需要的頁面 viewList = new ArrayList<View>(); viewList.add(v1); viewList.add(v2); viewList.add(v3);
15
// 將所有版面加入 mViewPager = (ViewPager) findViewById(R.id.viewpager); mViewPager.setAdapter(new MyViewPagerAdapter(viewList)); mViewPager.setCurrentItem(0); // 設置預設要顯示的頁面 // 找到畫面上需要的View View view = viewList.get(0); Button intentbutton=(Button)view.findViewById(R.id.intentbutton); // 定義(set)物件intentbutton的事件click intentbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 跳到Main2.java // 跳Activity用的是Intent類別 Intent it=new Intent(); it.setClass(Home.this,Main2.class); startActivity(it); } });
16
MyViewPagerAdapter.java class MyViewPagerAdapter extends PagerAdapter { private List<View> mListViews; public MyViewPagerAdapter(List<View> mListViews) { this.mListViews = mListViews; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); public Object instantiateItem(ViewGroup container, int position) { View view = mListViews.get(position); container.addView(view); return view; public int getCount() { return mListViews.size(); public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1;
17
同時保有ScrollView與滑動
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.