170 likes | 184 Views
滑動. 建國科技大學 資管系 饒瑞佶. 利用既有 XML 畫面. 首先加入一個 ViewFlipper 到 xml 畫面中. < ViewFlipper android:id = "@+id/viewFlipper1" android:layout_width = "match_parent" android:layout_height = "match_parent" >. 利用既有 XML 畫面. 其次,將要滑動的版面依序放在 ViewFlipper 中. < ViewFlipper android:id = "@+id/viewFlipper1"
E N D
滑動 建國科技大學 資管系 饒瑞佶
利用既有XML畫面 • 首先加入一個ViewFlipper到xml畫面中 <ViewFlipper android:id="@+id/viewFlipper1" android:layout_width="match_parent" android:layout_height="match_parent" >
利用既有XML畫面 • 其次,將要滑動的版面依序放在ViewFlipper中 <ViewFlipper android:id="@+id/viewFlipper1" android:layout_width="match_parent" android:layout_height="match_parent" > <include android:id="@+id/layout01" layout="@layout/bmi" /> <include android:id="@+id/layout02" layout="@layout/bmirelative" /> </ViewFlipper> 這個順序就是滑動順序
滑動主程式 • 首先增加OnTouchListener監聽事件 • 加入物件宣告 • 加入物件在onCreate publicclass Main extends Activity implementsOnTouchListener { private ViewFlipper viewFlipper; privatefloattouchDownX;//手指按下去的x座標 privatefloattouchUpX; //手指放開的x座標 //取得ViewFlipper物件 viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1); viewFlipper.setOnTouchListener(this);
滑動主程式 • 需要加入必要的ontouch事件 @Override publicboolean onTouch(View arg0, MotionEvent event) {
Ontouch事件 if (event.getAction() == MotionEvent.ACTION_DOWN) { // 左右滑動時手指按的x座標 touchDownX = event.getX(); returntrue; } elseif (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(); // 從右往左滑動 } elseif (touchDownX - touchUpX > 100) { //顯示上一個畫面的動畫 viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); // 顯示下一個畫面 viewFlipper.showNext(); } returntrue; }
push_left_in.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>
push_left_out.xml <?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>
push_right_in.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>
push_right_out.xml <?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>
加入畫面物件事件 • 依照原本處理事件方式處理就可以 但ViewFlipper碰到ScrollView會失效
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
修改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);
// 將所有版面加入 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); } }); }
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); } @Override public Object instantiateItem(ViewGroup container, int position) { View view = mListViews.get(position); container.addView(view); return view; } @Override public int getCount() { return mListViews.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } }