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

ViewPager中height=wrap_content无效,ScrollView里边用ListView显示不全解决办法

时间:2020-04-23 00:23:27      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:ide   hang   gate   eset   hellip   lse   rom   delegate   解决   

 

ViewPager中height=wrap_content无效

public class MyViewPager extends ViewPager {

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();
        int height = 0;
        int expandSpec = 0;
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            ViewGroup.LayoutParams lp = child.getLayoutParams();
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            child.measure(widthMeasureSpec, getChildMeasureSpec(heightMeasureSpec, 0, lp.height));
            int childHeight = child.getMeasuredHeight();
            if (childHeight > height){
                height = childHeight;
            }

            expandSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

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

    public MyViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
}
ScrollView里边用ListView显示不全
public class MyListView extends ListView {
    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 自己测量解决显示不全的问题
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
inflate(R.layout.activity_main, parent, false)解决设置无效问题,包一层不行

setContentView(R.layout.activity_main);

AppCompatActivity###
@Override
public void setContentView(@LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}

abstract class AppCompatDelegate的实现类AppCompatDelegateImpl###
@Override
public void setContentView(int resId) {
ensureSubDecor();
ViewGroup contentParent = mSubDecor.findViewById(android.R.id.content);
contentParent.removeAllViews();
LayoutInflater.from(mContext).inflate(resId, contentParent); /// 最终调用inflate
mAppCompatWindowCallback.getWrapped().onContentChanged();
}

abstract class LayoutInflater###
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
……………………………………………………………………………………
final View temp = createViewFromTag(root, name, inflaterContext, attrs); //1
……………………………………………………………………………………
temp.setLayoutParams(params); //2
……………………………………………………………………
rInflateChildren(parser, temp, attrs, true);
………………………………………………………………………………
root.addView(temp, params);
………………………………………………………………………………
if (root == null || !attachToRoot) {
result = temp;
}
return result;
}
}






ViewPager中height=wrap_content无效,ScrollView里边用ListView显示不全解决办法

标签:ide   hang   gate   eset   hellip   lse   rom   delegate   解决   

原文地址:https://www.cnblogs.com/anny0920/p/12757670.html

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