• 解决viewpager+多个fragment+listview,listview展示内容高度不自适应出现多余空白问题


    一、重写viewpager

    import android.content.Context;
    import android.support.v4.view.ViewPager;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.LinearLayout;
    
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    
    /**
     * Created by lenovo on 2018/3/8.
     */
    
    public class WrapContentHeightViewPager 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 WrapContentHeightViewPager(Context context) {
            super(context);
        }
    
        public WrapContentHeightViewPager(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) {
    
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
                if (layoutParams == null) {
                    layoutParams = new LinearLayout.LayoutParams(LinearLayout.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中提供一个公共的方法,给子fragment调用

    /**
     *
     * 提供子fragment调用方法,解决listview高度不自适应出现多余空白
     */
    public  void setChildObjectForPosition(View view,int poistion){
        mPager.setObjectForPosition(view,poistion);
    }
    

    三、在viewpager的onPageSelected()方法中重置高度

    @Override
    public void onPageSelected(int arg0) {
        mPager.resetHeight(arg0);
    }
    

    四、在子fragment的onCreateView()方法中调用父fragment的方法

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_report_latest,container,false);
        HomeFragment homeFragment = (HomeFragment) getParentFragment();
        homeFragment.setChildObjectForPosition(view,0);
        return view;
    }
    
    

    问题:注意这里如果遇到使用,不遇到可以不予理会
    如果最外层是scrollview 可能出现listview只展示一条
    由于listview展示问题,于是尝试自动测量listview高度动态计算去解决问题,定义自定义listview:

    public class NoScrollListview extends ListView{  
    	  
        public NoScrollListview(Context context, AttributeSet attrs) {  
                super(context, attrs);  
        }  
          
        /** 
         * 动态计算高度 
         */  
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {  
                int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
                                MeasureSpec.AT_MOST);  
                super.onMeasure(widthMeasureSpec, expandSpec);  
     
        }  
     
    }  
    
    
  • 相关阅读:
    go开发实战
    mq的各种问题分析(顺序,一致性,重复,丢失,堆积)
    神经图灵机(NTM)
    手机号隐藏
    验证指定项
    js姓名隐藏
    iview rate 评分验证
    校验成功后,提示文字不消失
    iview 开始时间不能大于结束时间
    [PHP] Laravel 获取模型/表的所有字段
  • 原文地址:https://www.cnblogs.com/wzqnxd/p/10071882.html
Copyright © 2020-2023  润新知