Viewpager背景颜色渗透第一页

时间:2013-05-06 17:05:55

标签: android background android-viewpager fragment

我在片段中有一个viewpager,表现出以下行为。页面2以后显示正确,但第一页显示为好像它位于容器视图的背景后面 - 好像它的z-index低于背景(z-indices向用户增加) - 请参阅图片链接如下。

页面实例化的代码是:

pager.setAdapter(new PagerAdapter() {

    @Override
    public void destroyItem(ViewGroup container, int position, Object item) {
        container.removeView((View) item);
    }

    @Override
    public int getCount() {
        return mImages.size();
    }

    @Override
    public View instantiateItem(ViewGroup container, final int position) {
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View layout = inflater.inflate(R.layout.image_selector, null, false);

        final ImageView image = (ImageView) layout.findViewById(R.id.image);

        image.setImageResource(mImages.get(position));

        container.addView(layout);

        return layout;
    }

    @Override
    public boolean isViewFromObject(View view, Object item) {
        return item.equals(view)
    }

});

..没什么特别的。

viewpager片段的布局是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ff2288dd">

  <TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/help1" />

  <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/text1"
    android:layout_above="@+id/text2"
    android:layout_margin="12dip" />

  <TextView
    android:id="@id/text2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="@string/help2" />

</RelativeLayout>

和每页图片:

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

  <ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true" />

</RelativeLayout>

再一次,没有任何异常。

物理设备和仿真器上的Froyo(2.2)和Gingerbread(2.3)以及运行4.2的物理设备都存在行为。

更奇怪的部分是,当触摸图像以选择它时,新片段替换原始viewpager片段,并且如果所选图像来自viewpager的第一页,则所描述的行为也存在于那里 - 尽管具有完全不同布局的片段(并且新片段没有定义背景)。

所选图像片段的布局:

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

  <ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true" />

</RelativeLayout>

以下参考图片。

First Page

Transition to Second Page

有没有人知道可能会发生什么?

0 个答案:

没有答案