2014-02-27 41 views
2

我需要在由三頁組成的應用程序中查看尋呼機。如何在android的視圖頁面頁面中使用滑動手勢?

我是新來的android視圖尋呼機。

我有一個視圖名爲TestSwipingView在屏幕的後半部分和另一個視圖爲屏幕的上半部分。

如何僅將TestSwipingView配置爲可滑動?

+0

檢查我的回答如下,讓我知道,如果你有還有更多的問題。 – mike20132013

+0

查看本教程http://www.gadgetsaint.com/android/create-viewpager-tabs-android/#.WPuppVN97BI – ASP

回答

1

這就是我實現它的方式。它也許會給你一些想法。

第1步:在我的主要活動中,我創建了我的頁面適配器,並在onCreate()中調用它。

public class SomeActivity extends FragmentActivity { 

    WebView mWebView; 
    private boolean mFromDropdown = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.tutorial_activity); 

     Window window = getWindow(); 
     DisplayMetrics metrics = getResources().getDisplayMetrics(); 
     window.setGravity(Gravity.CENTER); 
     int width = (int) (metrics.widthPixels * 1); 
     int height = (int) (metrics.heightPixels * .85); 
     window.setLayout(width, height); 

     mFromDropdown = getIntent().getBooleanExtra("fromDropdown", false); 

     MyPagerAdapter adapter = new MyPagerAdapter(); 
     ViewPager myPager = (ViewPager) findViewById(R.id.pager); 
     myPager.setAdapter(adapter); 
     myPager.setCurrentItem(0); 

    } 

第二步:這是我的自定義適配器爲您的示例:

private class MyPagerAdapter extends PagerAdapter { 
      public int getCount() { 
       return 3; 
      } 

      public Object instantiateItem(ViewGroup container, int position) { 
       LayoutInflater inflater = (LayoutInflater) container.getContext() 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

       // Using different layouts in the view pager instead of images. 

       int resId = -1; 

         //Getting my layout's in my adapter. Three layouts defined. 
       switch (position) { 
       case 0: 
        resId = R.layout.tutorial1; 
        break; 
       case 1: 
        resId = R.layout.tutorial2; 
        break; 
       case 2: 
        resId = R.layout.tutorial3; 
        break; 

       } 

       View view = inflater.inflate(resId, container, false); 
       ((ViewPager) container).addView(view, 0); 
       return view; 
      } 

      @Override 
      public void destroyItem(ViewGroup container, int position, Object object) { 
       container.removeView((View) object); 
      } 

      @Override 
      public boolean isViewFromObject(View view, Object object) { 
       return view == object; 
      } 

     } 

    } 

第3步:我的佈局:

主要佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#F3F3F4" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/tutorial_actionbar" 
     android:layout_width="match_parent" 
     android:layout_height="48dp" > 

     <TextView 
      android:id="@+id/tutorial_header" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:paddingLeft="25dp" 
      android:paddingRight="9dp" 
      android:text="Your Text" 
      android:textColor="#666666" 
      android:textSize="16sp" /> 
    </RelativeLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:paddingLeft="9dp" 
     android:paddingRight="9dp" > 
    </android.support.v4.view.ViewPager> 

</LinearLayout> 

佈局把裏面的ViewPager in MainLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#F3F3F4" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#F3F3F4" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:padding="18dp" > 

      <TextView 
       android:id="@+id/tutorialText2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#F3F3F4" 
       android:text="title1_secondscreen" 
       android:textColor="#666666" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/tutorialText3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#F3F3F4" 
       android:text="header1_secondscreen" 
       android:textColor="#666666" 
       android:textSize="16sp" /> 
     </LinearLayout> 

     <ImageView 
      android:id="@+id/image2" 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:layout_weight="0.60" 
      android:scaleType="centerInside" 
      android:src="@drawable/page2" /> 

     <TextView 
      android:id="@+id/tutorialText4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="#F3F3F4" 
      android:padding="10dp" 
      android:text="footer1_secondscreen" 
      android:textColor="#666666" 
      android:textSize="32sp" /> 
    </LinearLayout> 

</RelativeLayout> 

佈局把ViewPager裏面MainLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#F3F3F4" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#F3F3F4" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:padding="18dp" > 

      <TextView 
       android:id="@+id/tutorialText2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#F3F3F4" 
       android:text="title1_thirdscreen" 
       android:textColor="#666666" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/tutorialText3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#F3F3F4" 
       android:text="header1_thirdscreen" 
       android:textColor="#666666" 
       android:textSize="16sp" /> 

      <ImageView 
       android:id="@+id/image1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:scaleType="centerInside" 
       android:src="@drawable/page3" /> 

      <!-- <TextView 
       android:id="@+id/tutorialText4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#F3F3F4" 
       android:text="@string/tutoril_text3" 
       android:textColor="#666666" 
       android:textSize="16sp" /> --> 
     </LinearLayout> 


    </LinearLayout> 
    <TextView 
       android:id="@+id/tutorialText5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:background="#F3F3F4" 
       android:gravity="center" 
       android:padding="10dp" 
       android:text="footer1_thirdscreen" 
       android:textColor="#666666" 
       android:textSize="32sp" /> 

</RelativeLayout> 

希望這有助於你.. :) ..好運.. :)