我有一个简单的GridView / Fragement
public class member_list_image_fragment extends Fragment实现AdapterView.OnItemClickListener {
//Current member list (array of member details from backend)
public static final String KEY_MEMBER_LIST = "member_list";
//Application's context
Context activity_context;
private ArrayAdapter<String> gridAdapter;
// gridView gridAdapter;
private GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//get the activity context so it can be passed to the adapter
activity_context = getActivity().getApplicationContext();
return;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//define the view (home_screen_member_list_photo) that needs to be displayed when this fragment is called
View view = inflater.inflate(R.layout.home_screen_member_list_photo, container, false);
GridView grid = (GridView) view.findViewById(R.id.member_list_image_fragment_gridview);
grid.setAdapter(new member_list_image_grid_adapter(activity_context));
this.gridView = grid;
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
GridView memberGridView = this.gridView;
GridView mygridview = (GridView) getView().findViewById(R.id.member_list_image_fragment_gridview);
GridView myactgridview = (GridView) getActivity().findViewById(R.id.member_list_image_fragment_gridview);
Log.w("1", String.valueOf(memberGridView.getFirstVisiblePosition()));
Log.w("2", String.valueOf(memberGridView.getLastVisiblePosition()));
Log.w("child",String.valueOf(memberGridView.getChildCount()));
Log.w("child - mygrid",String.valueOf(mygridview.getChildCount()));
Log.w("child - myactgrid", String.valueOf(myactgridview.getChildCount()));
for(int visiblePosition = memberGridView.getFirstVisiblePosition(); visiblePosition <= memberGridView.getLastVisiblePosition(); visiblePosition++) {
View member_row_view = memberGridView.getChildAt(visiblePosition);
// Reset the ImageView
Log.w("image", "reset done");
}
return;
}
}
我试图遍历GridView中的所有项目并更改图像图标(我有理由这样做)。但无论我如何尝试在onActivityCreated中获取GridView和相关的getChildCount,它们都返回0.
我在onActivityCreated-
中尝试了以下内容Log.w("child",String.valueOf(memberGridView.getChildCount()));
Log.w("child - mygrid",String.valueOf(mygridview.getChildCount()));
Log.w("child - myactgrid",String.valueOf(myactgridview.getChildCount()));
它们都返回0.在模拟器上,我能够看到屏幕完全填充了GridView,并显示了所有元素。我做错了什么?
答案 0 :(得分:0)
这是因为只要网格没有显示给用户,您的网格就不会有孩子。 getChildCount将返回可见子项的数量。