美文网首页
viewpager嵌多个fragment高度不一致时的问题解决

viewpager嵌多个fragment高度不一致时的问题解决

作者: 楷桐 | 来源:发表于2017-06-08 20:59 被阅读550次

解决思路:在fragment里动态获取当前布局高度,然后通过sendBroadcast发送给广播接收者,然后动态设置viewpager的高度

在Fragment里

  @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint() && isResumed()) {
            int layoutParamsHeight = ViewUtil.getLayoutParamsHeight(f_vdd_layout);           
            getActivity().sendBroadcast(new Intent(TestActivity.ACTION_LAYOUT_PARAMS).putExtra("receive_height ", layoutParamsHeight));
        }
    }


在TestActivity

public static final String ACTION_LAYOUT_PARAMS = "layOut_vieWP1ger";

  private BroadcastReceiver br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int receiveHeight = intent.getIntExtra("receive_height ", -1);
            Log.e(TAG, "onReceive: " + receiveHeight);
            ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams();
            layoutParams.height = receiveHeight;
            viewPager.setLayoutParams(layoutParams);
        }
    };

用到的工具类

   public class ViewUtil {
       private static final String TAG = "ViewUtil";

    /**
     * 获取ListView所有子View叠加的高度
     *
     * @param listView
     * @return
     */
    public static int getListViewParams(ListView listView) {
        //通过ListView获取其中的适配器adapter
        ListAdapter listAdapter = listView.getAdapter();

        //声明默认高度为0
        int totalHeight = 0;
        //便利ListView所有的item,累加所有item的高度就是ListView的实际高度
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getLayoutParams().height;
            Log.e(TAG, "getListViewParams: " + totalHeight + " -- " + listItem.getMeasuredHeight() + " -- " + listItem.getLayoutParams().height + " -- " + listAdapter.getCount() + " -- " + " -- " + i);
        }
        //将累加获取的totalHeight赋值给LayoutParams的height属性
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        int height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

        return height;
    }

    /**
     * 获取GirdView所有子View叠加的高度
     *
     * @param gridView
     * @return
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public static int getGirdViewParams(GridView gridView) {
        //通过ListView获取其中的适配器adapter
        ListAdapter listAdapter = gridView.getAdapter();
        //声明默认高度为0
        int totalHeight = 0;
        //遍历ListView所有的item,累加所有item的高度就是ListView的实际高度
        for (int i = 0; i < listAdapter.getCount(); i += 2) {
            View listItem = listAdapter.getView(i, null, gridView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
            Log.e(TAG, "getGirdViewParams: " + " -- " + totalHeight + " -- " + listItem.getMeasuredHeight() + " -- " + listItem.getLayoutParams().height + " -- " + listAdapter.getCount() + " -- " + " -- " + i);
            listItem.measure(0, 0);
        }
        //将累加获取的totalHeight赋值给LayoutParams的height属性
        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        params.height = totalHeight + (gridView.getVerticalSpacing() * (listAdapter.getCount() - 1));
        int height = totalHeight + (gridView.getVerticalSpacing() * (listAdapter.getCount() - 1));

        return height;
    }

    /**
     * 获取ViewGroup的布局里的高度
     *
     * @param viewGroup
     * @return
     */
    public static int getLayoutParamsHeight(ViewGroup viewGroup) {
        int height = viewGroup.getLayoutParams().height;
        int measuredHeight = viewGroup.getMeasuredHeight();
        Log.e(TAG, "getLayoutParamsHeight: " + height + " -- " + measuredHeight);
        return height;
    }
}

相关文章

网友评论

      本文标题:viewpager嵌多个fragment高度不一致时的问题解决

      本文链接:https://www.haomeiwen.com/subject/vdwtqxtx.html