码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-12-05 17:27:47      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:linked   tla   and   set   text   androi   most   contain   widget   

一、重写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);  
 
    }  
 
}  

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

标签:linked   tla   and   set   text   androi   most   contain   widget   

原文地址:https://www.cnblogs.com/wzqnxd/p/10071882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!