android中ListView嵌套GridView,其中某个GridView的内容无法显示?
阿神
阿神 2017-04-17 15:53:02
0
2
623

描述

我在ListView中嵌套了一个GridView,即每个item中都有一个GridView。
GridView中显示一组图片。

代码

ListView的适配器

下面是
ListView的适配器中的getView。
其中的list里面是一组图片的Uri。
我进行判断了一下,如果list里没有Uri,就隐藏GridView;如果有才给GridView设置适配器显示出来。

 @Override
        public View getView(int arg0, View convertView, ViewGroup arg2) {

            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.listview_main_item, null);
                viewHolder = new ViewHolder();

                viewHolder.mImgGridView = (GridView) convertView
                        .findViewById(R.id.gv_listView_main_gridView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            ArrayList<String> list = (ArrayList<String>) initData().get(arg0).get("urls");
            if (list.size() == 0) {
                viewHolder.mImgGridView.setVisibility(View.GONE);
            } else {
                Log.d("PromotionalMaterialActi", "" + list.size());
                viewHolder.mImgGridView.setAdapter(new MyGridViewAdapter(context, list));
            }
            return convertView;
        }

数据

ArrayList<Map<String, Object>> mDatas = new ArrayList<Map<String, Object>>();

            //没图片
            Map<String, Object> map = new HashMap<>();
            ArrayList<String> urls = new ArrayList<String>();
            map.put("urls", urls);
            mDatas.add(map);

            //一张图片
            Map<String, Object> map2 = new HashMap<>();
            ArrayList<String> urls2 = new ArrayList<String>();
            urls2.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
            map2.put("urls", urls2);
            mDatas.add(map2);

            //三张图片
            Map<String, Object> map3 = new HashMap<>();
            ArrayList<String> urls3 = new ArrayList<String>();
            urls3.add("http://img.my.csdn.net/uploads/201410/19/1413698867_8323.jpg");
            urls3.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
            urls3.add("http://img.my.csdn.net/uploads/201410/19/1413698837_5654.jpg");
            map3.put("urls", urls3);
            mDatas.add(map3);

            //六张图片
            Map<String, Object> map4 = new HashMap<>();
            ArrayList<String> urls4 = new ArrayList<String>();
            urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698837_7507.jpg");
            urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698865_3560.jpg");
            urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698867_8323.jpg");
            urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698837_5654.jpg");
            urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
            urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698839_2302.jpg");
            map4.put("urls", urls4);
            mDatas.add(map4);

            //六张图片
            Map<String, Object> map5 = new HashMap<>();
            ArrayList<String> urls5 = new ArrayList<String>();
            urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698837_7507.jpg");
            urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698865_3560.jpg");
            urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698867_8323.jpg");
            urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698837_5654.jpg");
            urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
            urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698839_2302.jpg");
            map.put("urls", urls5);
            mDatas.add(map5);

现象描述

第四个item上的GridView图片无法显示


解决方法

viewHolder.mImgGridView.setVisibility(View.GONE);
这样第四个item上的GridView图片就会显示出来了。
但是这是为什么?

阿神
阿神

闭关修行中......

reply all(2)
巴扎黑
if (list.size() == 0) {
                viewHolder.mImgGridView.setVisibility(View.GONE);
            } else {
                Log.d("PromotionalMaterialActi", "" + list.size());
                viewHolder.mImgGridView.setAdapter(new MyGridViewAdapter(context, list));
            }

Modified to

if (list.size() == 0) {
                viewHolder.mImgGridView.setVisibility(View.GONE);
            } else {
                Log.d("PromotionalMaterialActi", "" + list.size());
                viewHolder.mImgGridView.setVisibility(View.VISIBLE);
                viewHolder.mImgGridView.setAdapter(new MyGridViewAdapter(context, list));
            }

The reason for this modification is: in some Items, the list is empty, so you will set the mImgGridView property to GONE. But when the ListView retrieves this View from the Holder and reuses it, the mImgGridView is always GONE, so you need to manually set it to VISIBLE.

黄舟

This is a problem of item reuse. All operations of setting status must be carefully considered, that is, if conditional statements are written, default else also needs to set default operations.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template