MeasureUtil

public class MeasureUtil {


    //测量滑动距离(也可以尝试一下别的滑动控件,我没试过!)
    public static int getScrolledDistance(PullToRefreshRecyclerView pullToRefreshRecyclerView){
        LinearLayoutManager layoutManager = (LinearLayoutManager) pullToRefreshRecyclerView.getLayoutManager();
        View childAt = pullToRefreshRecyclerView.getChildAt(0);
        int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
        int height = childAt.getHeight();
        int decoratedBottom = layoutManager.getDecoratedBottom(childAt);
        return (firstVisibleItemPosition+1)* height - decoratedBottom;
    }


    //获取状态栏(通知栏)的高度
    public static int getStatusBarHeight(Context context){
        Class c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }


    //动态设置listView的高度,解决嵌套冲突导致的只显示一条数据的问题
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        if(listView == null) return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }


    //动态设置gridView的高度,解决嵌套冲突导致的只显示一条数据的问题
    public static void reMesureGridViewHeight(GridView gridView) {
        // 获取GridView对应的Adapter
        ListAdapter listAdapter = gridView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int rows;
        int columns = 0;
        int horizontalBorderHeight = 0;
        Class clazz = gridView.getClass();
        try {
            // 利用反射,取得每行显示的个数
            Field column = clazz.getDeclaredField("mRequestedNumColumns");
            column.setAccessible(true);
            columns = (Integer) column.get(gridView);
            //columns = gridView.getNumColumns();//Call requires API level 11


            // 利用反射,取得横向分割线高度
            Field horizontalSpacing = clazz.getDeclaredField("mRequestedHorizontalSpacing");
            horizontalSpacing.setAccessible(true);
            horizontalBorderHeight = (Integer) horizontalSpacing.get(gridView);
            //horizontalBorderHeight = gridView.getHorizontalSpacing();//Call requires API level 16
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 判断数据总数除以每行个数是否整除。不能整除代表有多余,需要加一行
        if (listAdapter.getCount() % columns > 0) {
            rows = listAdapter.getCount() / columns + 1;
        } else {
            rows = listAdapter.getCount() / columns;
        }
        int totalHeight = 0;
        for (int i = 0; i < rows; i++) { // 只计算每项高度*行数
            View listItem = listAdapter.getView(i, null, gridView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }
        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        params.height = totalHeight + horizontalBorderHeight * (rows - 1);// 最后加上分割线总高度
        gridView.setLayoutParams(params);
    }


    //获取屏幕宽度
    public static int getWindowWidth(Activity activity){
        DisplayMetrics dm = new DisplayMetrics();
        //取得窗口属性
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);


        //窗口的宽度
        int screenWidth = dm.widthPixels;


        return screenWidth;
    }


    //获取屏幕高度
    public static int getWindowHeight(AppCompatActivity activity){
        DisplayMetrics dm = new DisplayMetrics();
        //取得窗口属性
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);


        //窗口高度
        int screenHeight = dm.heightPixels;


        return screenHeight;
    }
}

你可能感兴趣的:(MeasureUtil)