滚动嵌套viewpager,里面的fragment高度问题(已解决)

一:先自定义我们的viewpager

public class MyViewPager extends ViewPager{
   private int current;
   private int height = 0;
   /**
    * 保存position与对于的View
    */
   private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<Integer, View>();

   private boolean scrollble = true;

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

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


   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      if (mChildrenViews.size() > current) {
         View child = mChildrenViews.get(current);
         child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
         height = child.getMeasuredHeight();
      }

      heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
   }

   public void resetHeight(int current) {
      this.current = current;
      if (mChildrenViews.size() > current) {
         FrameLayout.LayoutParams layoutParams= (FrameLayout.LayoutParams) getLayoutParams();
//       LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
         if (layoutParams == null) {
            layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, height);
         } else {
            layoutParams.height = height;
         }
         setLayoutParams(layoutParams);
      }
   }
   /**
    * 保存position与对于的View
    */
   public void setObjectForPosition(View view, int position)
   {
      mChildrenViews.put(position, view);
   }


   @Override
   public boolean onTouchEvent(MotionEvent ev) {
      if (!scrollble) {
         return true;
      }
      return super.onTouchEvent(ev);
   }


   public boolean isScrollble() {
      return scrollble;
   }

   public void setScrollble(boolean scrollble) {
      this.scrollble = scrollble;
   }


}
二:请见我们的fragment

public class View_prodesc extends Fragment{

    MyViewPager vpp;
    View v;
    public View_prodesc(MyViewPager vp) {
        this.vpp = vp;
    }
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        v=inflater.inflate(R.layout.t_lin, null);
        vpp.setObjectForPosition(v,0);
        initView();
        hosabout();
        return v;
    }

三:那么就剩下我们的Fragmentactivity了

   mViewPager.setAdapter(mAdapter);
    //TabLayout的切换监听
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mViewPager.resetHeight(position);
        ((NestedScrollView) findViewById(R.id.item_detail_container)).scrollTo(0, 0);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});
    mViewPager.resetHeight(0);
}
聪明的你,我相信你一看便知,没错我用最简洁的语言记录了这个问题,详情请看代码

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页