2016-10-04 107 views
0

我試圖讓這個pageviewer與片段工作,但我偶然發現一個問題,在視圖停留在屏幕上刷卡。理想情況下,我希望它只能在一個特定的佈局上,但我不太確定爲什麼它「繼續」到其他屏幕(在片段之間的過渡期間它也保留在屏幕上)。Android - PageViewer視圖卡在屏幕上

任何反饋將不勝感激!

Link to Imgur Screenshots

主要:

private void initUI() 
{ 
    // ---------Page Viewer with Fragments ----------- 

    viewPager = (ViewPager) findViewById(R.id.pager); 
    actionBar = getSupportActionBar(); 
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); 

    viewPager.setAdapter(mAdapter); 
    actionBar.setHomeButtonEnabled(false); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Adding Tabs 
    for (String tab_name : tabs) { 
     actionBar.addTab(actionBar.newTab().setText(tab_name) 
       .setTabListener(this)); 
    } 


    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int position) { 
      // on changing the page 
      // make respected tab selected 
      actionBar.setSelectedNavigationItem(position); 
     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) { 
     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) { 
     } 
    }); 
} 


    @Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { 
     viewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { 

    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { 

    } 
} 

適配器:

public class TabsPagerAdapter extends FragmentPagerAdapter { 

public TabsPagerAdapter(FragmentManager fm) { 
    super(fm); 
} 

@Override 
public Fragment getItem(int index) { 

    switch (index) { 
     case 0: 
      // Main Frag 
      return new Fragment1(); 
     case 1: 
      // Second Frag 
      return new Fragment2(); 
     case 2: 
      // Third Frag 
      return new Fragment3(); 
    } 

    return null; 
} 

@Override 
public int getCount() { 
    return 3; 
} 
} 

片段類基本上是這樣:

public class Fragment1 extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.main, container, false); 

    return rootView; 
} 
} 

主要佈局:

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

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

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

回答

1

的的EditText是在主要佈局中,相同的結構佈局,將ViewPager處於它應的標籤內膨脹。您需要爲每個標籤創建一個單獨的佈局。

主要佈局

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

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

</RelativeLayout> 

layout_fragment_1.xml

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

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

然後,你需要在片段1

View rootView = inflater.inflate(R.layout.layout_fragment_1, container, false); 

return rootView; 
+0

膨脹的新佈局啊,你是100%正確的。感謝您快速直接的答覆。 – WonderfulWonder