Java Code Examples for android.widget.GridView#getAdapter()
The following examples show how to use
android.widget.GridView#getAdapter() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListViewHeight.java From MissZzzReader with Apache License 2.0 | 6 votes |
/** * 日期gridview的高度 * @param gridView * @return */ public static int setDateGridViewHeightBasedOnChildren(GridView gridView) { ListAdapter listAdapter = gridView.getAdapter(); if (listAdapter == null) { // pre-condition return 0; } int totalHeight = 0; for (int i = 0; i < (listAdapter.getCount()/7); i++) { View listItem = listAdapter.getView(i, null, gridView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = gridView.getLayoutParams(); params.height = totalHeight + (1 * (listAdapter.getCount()/7 - 1)); gridView.setLayoutParams(params); return totalHeight + (1 * (listAdapter.getCount()/7 - 1)); }
Example 2
Source File: ShortcutLauncherFolderActivity.java From FreezeYou with Apache License 2.0 | 6 votes |
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if ("pkgS".equals(key)) { final GridView slf_apps_gridView = findViewById(R.id.slf_apps_gridView); if (slf_apps_gridView != null) { Adapter adapter = slf_apps_gridView.getAdapter(); if (adapter instanceof ReplaceableSimpleAdapter) { final ArrayList<Map<String, Object>> folderItems = new ArrayList<>(); generateFolderItems(folderItems, sharedPreferences); ((ReplaceableSimpleAdapter) adapter).replaceAllInFormerArrayList(folderItems); } } } else if ("folderName".equals(key)) { final TextView slf_folderName_textView = findViewById(R.id.slf_folderName_textView); if (slf_folderName_textView != null) { slf_folderName_textView.setText(sharedPreferences.getString("folderName", getString(R.string.folder))); } } }
Example 3
Source File: MainBottomSheetDialogFragmentTest.java From openwebnet-android with MIT License | 6 votes |
private void expectStartActivity(int position, int title, Class<? extends AbstractDeviceActivity> newActivity) { GridView gridView = mainBottomSheetDialog.gridView; assertTrue("invalid instance type", gridView.getAdapter() instanceof BottomSheetDialogAdapter); BottomSheetDialogAdapter adapter = (BottomSheetDialogAdapter) gridView.getAdapter(); MenuItem item = (MenuItem) adapter.getItem(position); assertEquals("invalid title", activity.getResources().getString(title), item.getTitle().toString().replace("\n", " ")); gridView.performItemClick(item.getActionView(), position, gridView.getAdapter().getItemId(position)); Intent expectedIntent = new Intent(activity, newActivity) .putExtra(EXTRA_DEFAULT_ENVIRONMENT, DEFAULT_ENVIRONMENT) .putExtra(EXTRA_DEFAULT_GATEWAY, DEFAULT_GATEWAY); Intent nextStartedActivity = shadowOf(activity).getNextStartedActivity(); assertThat(nextStartedActivity.getComponent(), equalTo(expectedIntent.getComponent())); assertThat(nextStartedActivity.getExtras().size(), equalTo(expectedIntent.getExtras().size())); assertThat(nextStartedActivity.getIntExtra(EXTRA_DEFAULT_ENVIRONMENT, -1), equalTo(DEFAULT_ENVIRONMENT)); assertThat(nextStartedActivity.getStringExtra(EXTRA_DEFAULT_GATEWAY), equalTo(DEFAULT_GATEWAY)); }
Example 4
Source File: JKCalendar.java From JKCalendar with Apache License 2.0 | 6 votes |
/** * 重新計算GridView的高度 * * @param gridView */ public static void setGridViewHeightBasedOnChildren(GridView gridView, int columns) { ListAdapter listAdapter = gridView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount() / columns; i++) { View listItem = listAdapter.getView(i, null, gridView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = gridView.getLayoutParams(); params.height = totalHeight + (listAdapter.getCount() - 1); gridView.setLayoutParams(params); gridView.requestLayout(); }
Example 5
Source File: WidgetUtils.java From styT with Apache License 2.0 | 5 votes |
public static void setListViewHeightBasedOnChildren(GridView listView) { // 获取listview的adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } // 固定列宽,有多少列 int col = 3;// listView.getNumColumns(); int totalHeight = 0; // i每次加4,相当于listAdapter.getCount()小于等于4时 循环一次,计算一次item的高度, // listAdapter.getCount()小于等于8时计算两次高度相加 for (int i = 0; i < listAdapter.getCount(); i += col) { // 获取listview的每一个item View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 获取item的高度和 totalHeight += listItem.getMeasuredHeight(); } // 获取listview的布局参数 ViewGroup.LayoutParams params = listView.getLayoutParams(); // 设置高度 params.height = totalHeight; // 设置margin ((MarginLayoutParams) params).setMargins(10, 10, 10, 10); // 设置参数 listView.setLayoutParams(params); }
Example 6
Source File: GridViewUtil.java From MVPAndroidBootstrap with Apache License 2.0 | 5 votes |
/** * Resizes the gridview. * @param gridView * @param items count * @param columns column number */ public static void resizeGridView(GridView gridView, int columns) { ListAdapter listAdapter = gridView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; int items = listAdapter.getCount(); int rows = 0; int maxHeight = 0; for(int i = 0; i< listAdapter.getCount(); i++ ){ View listItem = listAdapter.getView(i, null, gridView); listItem.measure(0, 0); totalHeight = listItem.getMeasuredHeight(); if(maxHeight < totalHeight){ maxHeight = totalHeight; } } float x = 1; if( items > columns ){ x = items/columns; rows = (int) (x + 1); maxHeight *= rows; } ViewGroup.LayoutParams params = gridView.getLayoutParams(); params.height = maxHeight; gridView.setLayoutParams(params); }
Example 7
Source File: GridViewUtil.java From RxAndroidBootstrap with Apache License 2.0 | 5 votes |
/** * Resizes the gridview. * @param gridView * @param items count * @param columns column number */ public static void resizeGridView(GridView gridView, int columns) { ListAdapter listAdapter = gridView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; int items = listAdapter.getCount(); int rows = 0; int maxHeight = 0; for(int i = 0; i< listAdapter.getCount(); i++ ){ View listItem = listAdapter.getView(i, null, gridView); listItem.measure(0, 0); totalHeight = listItem.getMeasuredHeight(); if(maxHeight < totalHeight){ maxHeight = totalHeight; } } float x = 1; if( items > columns ){ x = items/columns; rows = (int) (x + 1); maxHeight *= rows; } ViewGroup.LayoutParams params = gridView.getLayoutParams(); params.height = maxHeight; gridView.setLayoutParams(params); }
Example 8
Source File: ListViewUtils.java From DevUtils with Apache License 2.0 | 4 votes |
/** * 计算 GridView 高度 * @param gridView {@link GridView} * @param setParams 是否 setLayoutParams * @return GridView 高度 */ public static int calcGridViewHeight(final GridView gridView, final boolean setParams) { if (gridView == null) return 0; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return 0; try { // Adapter ListAdapter listAdapter = gridView.getAdapter(); // Item 总条数 int itemCount = listAdapter.getCount(); // 没数据则直接跳过 if (itemCount == 0) return 0; // 高度 int height = 0; // 获取一共多少列 int numColumns = gridView.getNumColumns(); // 每列之间的间隔 | int horizontalSpacing = gridView.getHorizontalSpacing(); // 每行之间的间隔 - int verticalSpacing = gridView.getVerticalSpacing(); // 获取倍数 ( 行数 ) int lineNumber = NumberUtils.getMultiple(itemCount, numColumns); // 计算总共的宽度 - (GridView 宽度 - 列分割间距 ) / numColumns int childWidth = (gridView.getWidth() - (numColumns - 1) * horizontalSpacing) / numColumns; // 记录每行最大高度 int[] rowHeightArrays = new int[lineNumber]; // 临时高度 - 保存行中最高的列高度 int tempHeight; // 循环每一行绘制每个 Item 并保存 Bitmap for (int i = 0; i < lineNumber; i++) { // 清空高度 tempHeight = 0; // 循环列数 for (int j = 0; j < numColumns; j++) { // 获取对应的索引 int position = i * numColumns + j; // 如果大于总数据则跳过 if (position < itemCount) { View childView = listAdapter.getView(position, null, gridView); WidgetUtils.measureView(childView, childWidth); int itemHeight = childView.getMeasuredHeight(); // 保留最大高度 tempHeight = Math.max(itemHeight, tempHeight); } // 记录高度并累加 if (j == numColumns - 1) { height += tempHeight; rowHeightArrays[i] = tempHeight; } } } // 追加子项间分隔符占用的高度 height += (verticalSpacing * (lineNumber - 1)); // 是否需要设置高度 if (setParams) { ViewGroup.LayoutParams params = gridView.getLayoutParams(); params.height = height; gridView.setLayoutParams(params); } return height; } catch (Exception e) { LogPrintUtils.eTag(TAG, e, "calcGridViewHeight"); } return 0; }
Example 9
Source File: ListViewHeight.java From MissZzzReader with Apache License 2.0 | 4 votes |
/** * 计算gridview的高度 * @param gridView */ public static void setGridViewHeightBasedOnChildren(GridView gridView) { // 获取GridView对应的Adapter ListAdapter listAdapter = gridView.getAdapter(); if (listAdapter == null) { return; } int rows; int columnWidths; int columns=0; int horizontalBorderHeight = 0; int verticalBorderHeight = 0; Class<?> clazz=gridView.getClass(); try { //利用反射,取得横向分割线高度 Field vertical=clazz.getDeclaredField("mVerticalSpacing"); vertical.setAccessible(true); verticalBorderHeight =(Integer)vertical.get(gridView); //利用反射,取得纵向分割线高度 Field horizontalSpacing=clazz.getDeclaredField("mRequestedHorizontalSpacing"); horizontalSpacing.setAccessible(true); horizontalBorderHeight=(Integer)horizontalSpacing.get(gridView); //利用反射,取得每行显示的个数 Field column=clazz.getDeclaredField("mRequestedNumColumns"); column.setAccessible(true); columns=(Integer)column.get(gridView); if(columns == -1 || columns == 0){ Field columnWidth = clazz.getDeclaredField("mRequestedColumnWidth"); columnWidth.setAccessible(true); columnWidths = (Integer)columnWidth.get(gridView); int pad = gridView.getPaddingLeft() * 2; int width = BaseActivity.width - pad * 2; columns = width / (columnWidths /*+ horizontalBorderHeight*/); if(horizontalBorderHeight * (columns - 1) + columnWidths * columns > width){ columns --; } } } catch (Exception e) { // TODO: handle exception 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 + gridView.getPaddingTop() + + gridView.getPaddingBottom() + verticalBorderHeight * (rows-1);//最后加上分割线总高度 gridView.setLayoutParams(params); }
Example 10
Source File: DynamicColorPicker.java From dynamic-support with Apache License 2.0 | 2 votes |
/** * Set selected color for the grid views containing colors. * * @param gridView The grid view to select the color. * @param color The color to be selected. */ private void setSelectedColor(@NonNull GridView gridView, @ColorInt int color) { if (gridView.getAdapter() != null) { ((DynamicColorsAdapter) gridView.getAdapter()).setSelectedColor(color); } }