2014-01-21 81 views
0

我的viewpager的內容在我的viewpager放入scrollview之後消失,它的手冊viewpager有三個選項卡,每個選項卡都有獨立的佈局。 這是我的主要活動:Viewpager的內容將其放入滾動視圖後消失

public class MainActivity extends Activity { 

private static final String TAG = "MainActivity"; 

private TabBar tabBar; 

private ViewPager viewPager; 
private PagerAdapter pagerAdapter; 
private LayoutInflater layoutInflater; 
private List<View> pages; 

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


    pages = new ArrayList<View>(); 
    initWidgets(); 
    initAdapters(); 
    initListeners(); 
} 

private void initWidgets() { 
    tabBar = (TabBar) findViewById(R.id.MainTabBar); 
    viewPager = (ViewPager) findViewById(R.id.MainViewPager); 

    layoutInflater = LayoutInflater.from(this); 
    View tab1= layoutInflater.inflate(R.layout.tab1, null); 
    View tab2 = layoutInflater.inflate(R.layout.tab2, null); 
    View tab3=layoutInflater.inflate(R.layout.tab3, null); 
    pages.add(tab1); 
    pages.add(tab2); 
    pages.add(tab3); 
} 

private void initAdapters() { 
    pagerAdapter = new PagerAdapter() { 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return pages.size(); 
     } 

     @Override 
     public boolean isViewFromObject(View arg0, Object arg1) { 
      // TODO Auto-generated method stub 
      return arg0 == arg1; 
     } 

     @Override 
     public Object instantiateItem(ViewGroup container, int position) { 
      ((ViewPager) container).addView(pages.get(position)); 
      return pages.get(position); 
     } 

     @Override 
     public void destroyItem(ViewGroup container, int pos, Object obj) { 
      ((ViewPager) container).removeView(pages.get(pos)); 
     } 
    }; 

    viewPager.setAdapter(pagerAdapter); 
} 

private void initListeners() { 
    TabBar.OnCurrentTabChangedListener tabListener = new TabBar.OnCurrentTabChangedListener() { 

     @Override 
     public void onCurrentTabChanged(int index) { 
      // TODO Auto-generated method stub 
      if (index > -1 && index < viewPager.getAdapter().getCount() 
        && index != viewPager.getCurrentItem()) { 
       viewPager.setCurrentItem(index, true); 
      } 
     } 
    }; 

    ViewPager.OnPageChangeListener pageListener = new ViewPager.OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int arg0) { 
      // TODO Auto-generated method stub 
      tabBar.setCurrentTab(arg0); 
     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) { 
      // TODO Auto-generated method stub 

     } 
    }; 

    tabBar.setOnCurrentTabChangedListener(tabListener); 
    viewPager.setOnPageChangeListener(pageListener); 
} 
} 

我也有另外一個類來實現tabBar, 這是一個PART我的主要佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:pupa="http://schemas.android.com/apk/res/com.pupa.TabBarDemo" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="\n sssss TextView\n TextView\n TextView\n TextView\n TextView" /> 

     <com.pupa.common.widget.TabBar 
      android:id="@+id/MainTabBar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:background="@android:drawable/title_bar" 
      pupa:icons="@array/tab_icons" 
      pupa:seperator="@drawable/tab_seperator" 
      pupa:titles="@array/tab_titles" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/MainViewPager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
    </RelativeLayout> 

我不得不使用RelativeLayout,因爲我有SlidingDrawer

回答

1

只需將android:fillViewport =「true」添加到滾動視圖