ViewPager不会滚动到下一页

时间:2013-01-20 07:04:37

标签: android android-viewpager

我有一个FragmentActivity,里面有标签。第一个选项卡中包含ViewPager。 ViewPager不起作用。它显示第一页,但不会滚动到下一页。我看到instantiateItem被调用下一页。我想也许触摸被其他东西截获,不太确定。

<?xml version="1.0" encoding="utf-8"?>


<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/topbanner" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:id="@+id/hometab"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

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

            </LinearLayout>
            <ScrollView
                android:id="@+id/scroller"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:fillViewport="true" >

                <LinearLayout
                    android:id="@+id/articlestab"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >
                </LinearLayout>
            </ScrollView>

            <LinearLayout
                android:id="@+id/videostab"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/txt3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="This is tab 3" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>

这是我放入ViewPager的image_page,我添加了6个。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image_page"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/border"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_gravity="center_vertical"
        android:src="@drawable/app" />

    <TextView
        android:id="@+id/caption"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textStyle="bold" />

</LinearLayout>

这是我添加适配器的地方:

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

这是适配器:

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

    public Object instantiateItem(View collection, int position) {
        String image = null;
        String caption = null;
        Log.d(TAG, "instantiateItem called for position: " + position);
        if (sliderResults != null) {
            LinearLayout layout1 = null;
            LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            JSONArray jArray = null;
            try {
                jArray = new JSONArray(sliderResults);
                JSONObject j = jArray.getJSONObject(0);
                layout1 = (LinearLayout) inflater.inflate(R.layout.image_page, null);

                switch (position) {
                case 0:
                    image = "img1";
                    caption = "capt1";
                    break;
                case 1:
                    image = "img2";
                    caption = "capt2";
                    break;
                case 2:
                    image = "img3";
                    caption = "capt3";
                    break;
                case 3:
                    image = "img4";
                    caption = "capt4";
                    break;
                case 4:
                    image = "img5";
                    caption = "capt5";
                    break;
                case 5:
                    image = "img6";
                    caption = "capt6";
                    break;
                }
                new DownloadImageTask((ImageView) layout1.findViewById(R.id.image), 100).execute("http://removed" + j.getString(image));
                ((TextView) layout1.findViewById(R.id.caption)).setText(j.getString(caption));

            } catch (JSONException e) {
                Log.d(TAG, "JSONException");
                e.printStackTrace();
            }


            // View view = inflater.inflate(resId, null);
            ((ViewPager) collection).addView(layout1, 0);
            return layout1;
        }else{
            Log.d(TAG, "slider results not ready yet");
        }
        return null;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}

我使用相同的代码创建了一个测试视图活动,并且ViewPager正常工作。它与我相信的TabHost有关。也许是拦截触摸事件还是什么?

2 个答案:

答案 0 :(得分:0)

这是怎么做的:

int resId = 0;
switch (position) {
case 0:
    resId = R.layout.your_first_layout_id;
    break;
case 1:
    resId = R.layout.your_second_layout_id;
    break;
case 2:
    resId = R.layout.your_third_layout_id;
    break;
case 3:
    resId = R.layout.your_fourth_layout_id;
    break;

}

View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;

}

我就是这样做的,你需要在switch / case中调用你的布局ID。 或者你可以像这样夸大观点:

case 0:
        view = inflater.inflate(R.layout.some_layout, null);
        break;

答案 1 :(得分:0)

最后通过这个问题找出答案:Buttons inside TAB not clickable/responding

不要在FrameLayout中放置任何内容。将所有布局移动到单独的xml文件中,然后以编程方式将它们添加到framelayout。最初我在framelayout中只尝试了一个孩子,但是这使得最后一个选项卡无法正常运行。