2016-07-01 112 views
0

我正在嘗試做一個小應用程序來嘗試一些我正在開發的更大的應用程序。我想添加一個顯示四個圖像的viewpager,我獨立開發了這個viewpager並且工作,但是當我試圖用標籤將它添加到我的其他應用程序(只有幾個按鈕)時,它不顯示任何內容,我我真的停留在這裏。可以通過這種方式添加Viewpager嗎?老實說,我沒有太多的Java經驗,我通常只編程C和彙編程序,如果你能幫助我,我會很高興。Android ViewPager not Showing

這是與按鈕的XML文件和包括:

<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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
android:weightSum="1"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Calculo Rapido" 
    android:id="@+id/btcr" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="0.16" 
    android:layout_alignParentStart="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Calculo Avanzado" 
    android:id="@+id/button" 
    android:layout_centerVertical="true" 
    android:layout_below="@+id/btcr" /> 

<include layout="@layout/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:translationZ="15dp" /> 

現在,在MainActivity,因爲我不停的Viewpager一個與該viewpager鏈接的活動中持有的按鈕活動原始佈局

Button siguiente; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    siguiente = (Button)findViewById(R.id.btcr); 
    siguiente.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent siguiente = new Intent(MainActivity.this, Main2Activity.class); 
      startActivity(siguiente); 
     } 
    }); 
} 

最後,mainpager主要活動的代碼,它使用保存在另一個java類中的CustomPagerAdapter。正如我之前所說的,這個viewpager確實獨立工作,但是當我嘗試在/ include標籤中實現它時,它不顯示任何內容。

公共類MainActivity擴展活動{

CustomPagerAdapter mCustomPagerAdapter; 
ViewPager mViewPager; 
private static int currentPage = 0; 
private static int NUM_PAGES = 0; 
Button siguiente; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // == Setting up the ViewPager == 

    mCustomPagerAdapter = new CustomPagerAdapter(this); 

    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mCustomPagerAdapter); 

    final Handler handler = new Handler(); 
    final Runnable Update = new Runnable() { 
     public void run() { 
      if (currentPage == NUM_PAGES) { 
       currentPage = 0; 
      } 
      mViewPager.setCurrentItem(currentPage++); 

      if (currentPage == 5) { 
       currentPage = 0; 
       mViewPager.setCurrentItem(currentPage++, false); 
      } 
     } 
    }; 
    Timer swipeTimer = new Timer(); 
    swipeTimer.schedule(new TimerTask() { 
     @Override 
     public void run() { 
      handler.post(Update); 
     } 
    }, 2000, 2000); 

} 

很抱歉,如果太長的傢伙,但我有點沮喪,我已經嘗試了很多東西,但還沒有成型。無論按鈕做什麼,ViewPager都不起作用,或者ViewPager可以正常工作,但按鈕已經死了。

我會reaaally aprecciate你的幫助:)

回答

0

事實證明,我需要的是隻是簡單的解決方案。我必須將viewpager放在我的activity的佈局文件上作爲另一個對象,只需配置它的大小和位置即可。然後,在我的onCreate上粘貼了ViewPager的代碼。非常簡單,不需要其他佈局:)

0

創建layout文件夾,並將其命名爲secondactivity.xml

內一個新的XML文件,並把這段代碼

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

    <android.support.v4.view.ViewPager 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/pager"> 

    </android.support.v4.view.ViewPager> 
</LinearLayout> 

現在裏面你Main2Activity.class

變更

setContentView(R.layout.activity_main);

setContentView(R.layout.secondactivity);

並從以前的XML刪除include

+0

並解釋您有多少活動和xml文件。 – ebyt

+0

我有另外3個xml佈局。一個帶有閃屏,另一個用於測試(我的按鈕實際上跳轉到此佈局),另一個爲View Pager提供元素。我還有另外三個活動,即啓動屏幕,允許按鈕在圖層中移動的那一個,以及使viewpager工作的那個(繪製圖像,創建例程等)。感謝您的幫助ebyt,我現在正在實施您的解決方案:) – mrRSA