Android ViewPager赢得';不要滚动到下一页

Android ViewPager赢得';不要滚动到下一页,android,android-viewpager,Android,Android Viewpager,我有一个碎片活动,里面有标签。第一个选项卡中有一个ViewPager。ViewPager不工作。它显示第一页,但不会滚动到下一页。我看到他们被要求进入下一页。我想可能触摸被其他东西截获了,不太确定 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabHost"

我有一个碎片活动,里面有标签。第一个选项卡中有一个ViewPager。ViewPager不工作。它显示第一页,但不会滚动到下一页。我看到他们被要求进入下一页。我想可能触摸被其他东西截获了,不太确定

<?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>
下面是适配器:

    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可以正常工作。我相信这和食客有关。可能是拦截触摸事件或其他什么?

这是如何做到的:

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;

多亏了这个问题,我终于找到了答案:


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

我所做的在测试活动中效果良好。我刚刚发现问题出在tabhost中的第一个选项卡上。我添加了一个按钮,但无法单击它。第三个标签上的相同按钮,我可以点击它,真的很奇怪。
case 0:
        view = inflater.inflate(R.layout.some_layout, null);
        break;