在ViewPager中,Horizo​​ntalScrollView复制视图

时间:2014-08-08 11:07:54

标签: android view horizontalscrollview

我在ViewPager的两个页面上的RelativeLayout中有Horizo​​ntalScrollView。起初它可以正常工作,但是当我在ViewPager中浏览页面然后再次尝试滚动Horizo​​ntalScrollView时,视图重复如下:enter image description here

所以它就像复制了一个视图粘在背景上,而前一个视图正常滚动。

我已经尝试了一些事情,比如播放背景颜色,persistentDrawingCache,removeAllViews并再次添加它们但它仍然会发生。

你能帮我找出造成这种情况的原因以及如何解决这个问题吗? 非常感谢!

更新:以下是代码段

horizo​​ntal_scrollview_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    android:overScrollMode="never"
    android:persistentDrawingCache="none"
    android:layout_centerHorizontal="true" >

    <LinearLayout
        android:id="@+id/horizontalScrollLinearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />

</HorizontalScrollView>

horizo​​ntal_scrollview_child_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.ursus.nameday"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <com.xxx.CircularImageView
        android:id="@+id/contactPhotoImageView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher"
        app:border="true"
        app:border_color="@android:color/holo_blue_light"
        app:border_width="2dp"
        app:shadow="true" />

    <TextView
        android:id="@+id/contactNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

Horizo​​ntalScrollViewFragment.java

public class HorizontalScrollViewFragment extends Fragment {
    private LinearLayout childLinearLayout;

    @Override
    public View onCreateView(final LayoutInflater inflater,
            final ViewGroup container, final Bundle savedInstanceState) {       
        final View rootView = inflater.inflate(
                R.layout.horizontal_scrollview_layout, container, false);

        contactsLinearLayout = (LinearLayout) rootView
                .findViewById(R.id.horizontalScrollLinearLayout);

        //ADDING CHILDREN HERE
        final View child1 = LayoutInflater.from(getActivity()).inflate(
                R.layout.horizontal_scrollview_child_layout, null);
        childLinearLayout.addView(child1);
        ...

        return rootView;
    }
}

DayViewFragment.java(插入ViewPager的片段)

public class DayViewFragment extends Fragment {
    ...
    private HorizontalScrollViewFragment fragment;

    @Override
    public View onCreateView(final LayoutInflater inflater,
            final ViewGroup container, final Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.day_item_layout,
                container, false);
        ...
        final FragmentManager fm = getChildFragmentManager();
        final FragmentTransaction ft = fm.beginTransaction();

        fragment = new HorizontalScrollViewFragment();
        ft.add(R.id.fragmentContainer, fragment);

        ft.commit();
        ...


        return rootView;
    }
}

1 个答案:

答案 0 :(得分:0)

在达到ViewPager要保留的屏幕限制并且需要重新创建包含片段的片段之后重新创建片段时,会出现问题。 默认值为1因此我只需要将其更改为2(setOffscreenPageLimit(2)),因为我在ViewPager中有3个片段。现在问题不会发生。 这不是一个理想的解决方案,但有效。如果这对其他人没有帮助:您需要控制片段的重建时间和方式,并确保清除视图。