在scrollview里面的Viewpager

时间:2015-05-26 10:27:41

标签: java android eclipse android-viewpager adt

ViewPager内有ScrollView以及其他组件。当我在ViewPager中为XML提供高度时,ScrollView正常工作但我无法为ScrollView提供高度,因为ViewPager应根据页面展开高度。我已经实现了一个runnable,它将在运行时扩展ViewPager

问题是我无法滚动持有ScrollView的外部ViewPager

XML =======

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
  <ImageView
    android:src="@drawable/img1"
    android:scaleType="fitXY"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:id="@+id/img_1"
    />
<ViewPager
    android:layout_width="match_parent"
    android:layout_height="wrap_content">        
</ViewPager>
</ScrollView>

JAVA动态设置viewpager的高度

viewpager.post(new Runnable() {
@Override
public void run() {
    ViewGroup.LayoutParams lp = viewpager.getLayoutParams();
    lp.height = viewpager.getChildAt(viewpager.getCurrentItem()).getHeight();
    viewpager.setLayoutParams(lp);
}

});

3 个答案:

答案 0 :(得分:0)

我认为你必须删除你的java代码。但是将此属性添加到scrollview xml:

android:fillViewport="true"

答案 1 :(得分:0)

这个问题在这里得到解答:Dynamic height viewpager

无论如何,我将为您提供解决问题所需的代码:

public class CustomPager extends ViewPager {

    private View mCurrentView;

    public CustomPager(Context context) {
        super(context);
    }

    public CustomPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mCurrentView == null) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        int height = 0;
        mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = mCurrentView.getMeasuredHeight();
        if (h > height) height = h;
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void measureCurrentView(View currentView) {
        mCurrentView = currentView;
        requestLayout();
    }

    public int measureFragment(View view) {
        if (view == null)
            return 0;

        view.measure(0, 0);
        return view.getMeasuredHeight();
    }
}

就像老板的回答说:

  

每次更改页面时都需要重新测量ViewPager。这样做的好地方是PagerAdapter的setPrimaryItem函数

public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;
    private int mCurrentPosition = -1;

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragments = new ArrayList<Fragment>();
        fragments.add(new FirstFragment());
        fragments.add(new SecondFragment());
        fragments.add(new ThirdFragment());
        fragments.add(new FourthFragment());
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        if (position != mCurrentPosition) {
            Fragment fragment = (Fragment) object;
            CustomPager pager = (CustomPager) container;
            if (fragment != null && fragment.getView() != null) {
                mCurrentPosition = position;
                pager.measureCurrentView(fragment.getView());
            }
        }
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

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

您可以找到所有代码here

答案 2 :(得分:0)

我找到了很好的解决方法here

无论如何,它是文章中的代码:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            int currentPagePosition=0;
            View child = getChildAt(currentPagePosition);
            if (child != null) {
                child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                int h = child.getMeasuredHeight();
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

您需要创建自己的自定义视图分页器,该自定义视图分页器是从ViewPager扩展并覆盖onMeasure方法