Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有滑动布局的ViewPager内的Listview-高度问题_Android_Listview_Android Viewpager_Android View - Fatal编程技术网

Android 带有滑动布局的ViewPager内的Listview-高度问题

Android 带有滑动布局的ViewPager内的Listview-高度问题,android,listview,android-viewpager,android-view,Android,Listview,Android Viewpager,Android View,我有一个片段,其中我托管了一个带有TabLayout的ViewPager,ViewPager的内容是一个包含ListView的片段 我知道我的ViewPager无法使用wrap_内容,无法计算起点的高度 所以我这样做了: @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasure

我有一个片段,其中我托管了一个带有TabLayout的ViewPager,ViewPager的内容是一个包含ListView的片段

我知道我的ViewPager无法使用wrap_内容,无法计算起点的高度

所以我这样做了:

 @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;

        final View tab = getChildAt(0);
        if (tab == null) {
            return;
        }

        int width = getMeasuredWidth();
        if (wrapHeight) {
            // Keep the current measured width.
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
        }
        Fragment fragment = ((Fragment) getAdapter().instantiateItem(this, getCurrentItem()));
        heightMeasureSpec = getMeasureExactly(fragment.getView(), widthMeasureSpec);

        //Log.i(Constants.TAG, "item :" + getCurrentItem() + "|height" + heightMeasureSpec);
        // super has to be called again so the new specs are treated as
        // exact measurements.
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    int getMeasureExactly(View child, int widthMeasureSpec) {
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int height = calculateHeight ((ListView)child.findViewById(R.id.list)); //getMeasuredHeight();

        System.out.println("Height is: "+height);
        return MeasureSpec.makeMeasureSpec(height , MeasureSpec.EXACTLY);
    }


    private int calculateHeight(ListView list) {

        int height = 0;

        for (int i = 0; i <10; i++) {
            View childView = list.getAdapter().getView(i, null, list);
            childView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            height+= childView.getMeasuredHeight();
        }

        //dividers height
      //  height += list.getDividerHeight() * 11; //list.getCount();

        return height;

    }
@覆盖
测量时的公共空隙(int-widthMeasureSpec,int-heightMeasureSpec){
超级测量(宽度测量、高度测量);
布尔wrapHeight=MeasureSpec.getMode(heightMeasureSpec)==MeasureSpec.AT_;
最终视图选项卡=getChildAt(0);
if(tab==null){
返回;
}
int width=getMeasuredWidth();
如果(愤怒){
//保持当前测量的宽度。
widthMeasureSpec=MeasureSpec.MakeMasureSpec(宽度,精确测量);
}
Fragment Fragment=((Fragment)getAdapter().instanceItem(this,getCurrentItem());
heightMeasureSpec=getMeasureExactly(fragment.getView(),widthmasurespec);
//Log.i(Constants.TAG,“项:+getCurrentItem()+“| height”+heightMeasureSpec);
//必须再次调用super,以便将新规范视为
//精确的测量。
超级测量(宽度测量、高度测量);
}
int getMeasureExactly(查看子对象,int widthMeasureSpec){
child.measure(widthmasurespec,MeasureSpec.makeMeasureSpec(0,MeasureSpec.unspected));
int height=calculateHeight((ListView)child.findViewById(R.id.list));//getMeasuredHeight();
System.out.println(“高度为:”+高度);
返回MeasureSpec.makeMeasureSpec(高度,精确测量);
}
私有整数计算权重(列表视图列表){
整数高度=0;

对于XML文件中的(inti=0;i,放入列表视图选项

android:layout_weight="0.92"

这听起来可能很傻,但你试过乘以11吗?是的,但这违背了逻辑-不太符合逻辑,可能会产生不可预测的结果。如果我把它放在循环中,这也会给我一个ArrayIndexOutOfBounds。