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

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;
}
}






猜你喜欢

转载自www.cnblogs.com/anny0920/p/12757670.html
今日推荐