/** * 根据屏幕宽度计算得到view的宽度(用于适配GridLayout) * * @param columnCount 要显示的GridView的列数 * @return view宽度(px) */ public int getViewWidthByWindowWidth(int columnCount) { DisplayMetrics dm = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(dm); float density = dm.density; int windth = (int) ((dm.widthPixels - getSidebarWidth() - (20 + 10 * columnCount) * density) / columnCount); Log.i(activity.getPackageName(), "===========计算得到的view-width:" + windth); return windth; }
/** * 当Fragment改变时,改变左侧对应条目的背景颜色,并实现ScrollView自动滚动到焦点位置 * * @param clickedView */ public void setLeftTextViewBackgroundColorWhenFragmentChanged(String leftTextViewTag) { Fragment fragmentLeft = activity.getSupportFragmentManager().findFragmentById(R.id.fragment_left); View leftView = fragmentLeft.getView(); View clickedLeftTextView = leftView.findViewWithTag(leftTextViewTag); ScrollView scrollView = (ScrollView) leftView.findViewById(R.id.scrollView_left); clickedLeftTextView.setFocusable(true); clickedLeftTextView.requestFocus(); // view获取焦点 if (leftTextViewTag.equals("0") || leftTextViewTag.equals("1") || leftTextViewTag.equals("2")) scrollView.fullScroll(ScrollView.FOCUS_FORWARD); // 向前滚动 else scrollView.fullScroll(ScrollView.FOCUS_DOWN); // 向后滚动 Log.i(getClass().toString(), "clickedLeftTextView:" + clickedLeftTextView); setLeftTextViewBackgroundColor(clickedLeftTextView, Integer.parseInt(leftTextViewTag)); }
/** * 获取左边导航栏的宽度 * * @return */ private int getSidebarWidth() { View view = activity.findViewById(R.id.fragment_left_container); return AndroidUtil.getViewWidth(view); }