GridView控件不包含所有适配器 [英] GridView doesn't contain all of adapter

查看:259
本文介绍了GridView控件不包含所有适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个GridView mGridView 和适配器 mAdapter 225次,我适配器设置GridView控件。我还做了一个按钮,当我点击它这样做:

I've made a GridView mGridView and an adapter mAdapter of 225 Views, and I set the adapter to the GridView. I also made a button so when I click on it does this:

Log.d("mAdapter.getCount()","" + mAdapter.getCount());
Log.d("mGridView.getCount()",""+mGridView.getCount());
Log.d("mGridView.getChildCount()",""+mGridView.getChildCount());

和存在的结果是:

D/mAdapter.getCount()﹕ 225
D/mGridView.getCount()﹕ 225
D/mGridView.getChildCount()﹕ 180

这是我的问题,因为我需要使用 mGridView.getChildAt(INT位置) 0 224 。每当我使用 mGridView.getChildAt(180).setBackgroundColor(Color.BLUE)我得到这个错误:

This is a problem for me, because I need to use mGridView.getChildAt(int position) from 0 to 224. Whenever I use mGridView.getChildAt(180).setBackgroundColor(Color.BLUE) I get this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference

它说,孩子是。但我不知道如何解决这个问题,所以我可以访问每一个孩子这是在GridView。

It's saying that the child is null. But I don't know how to fix this so I can access every child that's in the GridView.

推荐答案

mGridView.getChildCount()将返回的视图中显示的数字在窗口时间。所以说,如果我的GridView有5列,并在同一时间只有10个可以显示然后mGridView.getChildCount()将返回50(5×10 =)

mGridView.getChildCount() would return the number of views visible at a time in the window. So say if my GridView has 5 columns and at a time only 10 could be displayed then mGridView.getChildCount() would return 50 (=5x10)

如果您要修改使用getChildCount API在特定的索引视图的属性(比如说,的backgroundColor),则可能可能使用idxFirstVisiblePosition = mGridView.getFirstVisiblePosition(检查的第一个可见位置(比如说,25)),那么(idxFirstVisiblePosition + childCount - 1)会给你,你可以访问和更改属性当前可视窗口的最大子索引

If you are trying to modify properties(say, backgroundColor) of a view at certain index using getChildCount API, you might probably check the first visible position(say, 25) using idxFirstVisiblePosition = mGridView.getFirstVisiblePosition(), then (idxFirstVisiblePosition + childCount - 1) would give you the maximum child index you could access and change properties for the current visible window.

这篇关于GridView控件不包含所有适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆