将viewGroup添加到ListView吗? [英] Adding a viewGroup to a ListView?

查看:62
本文介绍了将viewGroup添加到ListView吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我的公司做一些研发.我们正在尝试拥有一个包含imageview的listView,以及listview中每个条目的两个编辑框.

Doing some R&D for my company. We are trying to have a listView that contains an imageview, and two edit boxes for each entry in the listview.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:isScrollContainer="true"
  android:focusableInTouchMode="false"
  android:focusable="false">
  <ImageView android:id="@+id/imgView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:maxWidth="100dp"
  android:maxHeight="100dp"/>
  <LinearLayout 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">
    <EditText android:id="@+id/img_title"
    android:hint="Title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="150dp"/>
    <EditText android:id="@+id/img_desc"
    android:hint="Description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="150dp"/>
  </LinearLayout>
</LinearLayout>

这是我要用于列表视图中的项目的布局文件.在我们的ImageAdapter的getView中(扩展了ArrayAdapter),我试图使用LayoutInflater来膨胀xml,然后将其存储到ViewGroup中.我找到了ViewViewByID()来获取xml中的imageView,并设置我们想要的imageView的属性.

This is the layout file I am attempting to use for the item that will be within the listview. Within the getView of our ImageAdapter (which extends ArrayAdapter), I am attempting to use the LayoutInflater to inflate the xml, which I then store into a ViewGroup. I findViewByID() to get the imageView in the xml, and set the properties of the imageView that we would like.

如果我们继续膨胀该xml文件,则所有ID都将相同.这是我们的问题.

If we keep inflating this xml file, all the id's will be the same. Here is our issues.

  1. 如果我们删除带有上下文菜单,它实际上会删除不正确的一个.测试显示这主要是最后添加的一个,但并非总是如此.
  2. EditText不响应键盘输入.有时,它们存储一些数据,通常总是"bbb".

我们还有更多问题,但在解决了这些更严重的错误之后,我们会回发.

We have more issues, but we'll post back after we get these more serious errors fixed.

public View getView(int position, View convertView, ViewGroup parent)
    {
        final ImageView imageView;
        //InputStream is = null;
        final Context mContext = super.getContext();
        final AdapterItem item = super.getItem(position);
        final Uri imageUri = item.getUri();
        ViewGroup viewGroup = null;

        try
        {
            //-- If the view has not been created yet.
            if (convertView == null)
            {
                /*
                 * Build the ImageView from the URI with some custom
                 * view settings.
                 */

                viewGroup = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.mediauploadobject, null);
                imageView = (ImageView) viewGroup.findViewById(R.id.imgView);
                //imageView.setLayoutParams(new GridView.LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT));
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setPadding(IMAGE_PADDING_LEFT, IMAGE_PADDING_TOP,
                        IMAGE_PADDING_RIGHT, IMAGE_PADDING_BOTTOM);
                imageView.setDrawingCacheEnabled(true);
                imageView.setClickable(true);
                imageView.setFocusable(true);
                imageView.setFocusableInTouchMode(true);
                imageView.setSaveEnabled(false);
                //the following two lines are required for the menu to popUp
                imageView.setOnLongClickListener(new LongClickListener());
                imageView.setOnCreateContextMenuListener(new LongClickMenu());
                imageView.setOnClickListener(new ShortClickListener());

                //the following two lines are required to put a boarder around the images
                imageView.setOnTouchListener(new PictureOnTouchListener());
                imageView.setOnFocusChangeListener(new PictureOnFocusChangeListener());

                //-- Keep a reference to the ImageView by tagging it.
                imageView.setTag(imageUri);
            }else
            {
                //-- R-E-C-Y-C-L-E recycle!
                viewGroup = (ViewGroup) convertView;
                imageView = (ImageView) viewGroup.findViewById(R.id.imgView);
            }

            //-- Lazy load the images so the user doesn't have to wait for all of the querying non-sense
            //   that happens behind the scenes.
            imageView.setImageResource(android.R.drawable.gallery_thumb);
            imageView.post(new ImageLoader(imageUri, imageView));           

            //-- Be VERY careful when changing this code. Due to heap size issues,
            //   the size of the bitmap image MUST be modified with the
            //   provided BitmapFactory.Options or the program will
            //   crash often and frequent.
            //post
            //-- AJG 7/1/2010 added this assignment so we aren't always setting these preferences every
            //   iteration
            convertView = viewGroup;
        }catch(Throwable t)
        {
            Log.e(TAG, t.toString());
            return null;            
        }finally
        {
            //try{if(is != null)is.close();}catch(Exception squish){}
        }

        return viewGroup;
    }

推荐答案

默认情况下,子视图在列表视图中不能集中显示.这是为了防止轨迹球/非触摸导航行为异常.这可能就是为什么您的edittexts不响应输入的原因.确保您正在调用ListView.setItemsCanFocus(true)方法.

Subviews aren't focusable in a listview by default. This is to prevent odd trackball/non-touch navigation behavior. That might be why your edittexts aren't responding to input. Make sure you're calling the ListView.setItemsCanFocus(true) method.

这篇关于将viewGroup添加到ListView吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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