将其放入Scrollview后,Viewpager的内容会消失

时间:2014-01-21 07:48:38

标签: android android-viewpager scrollview

我将viewpager放入viewpager后,scrollview的内容消失,其手册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, 这是我的主要布局:

<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 个答案:

答案 0 :(得分:1)

只需将android:fillViewport =“true”添加到ScrollView

即可