android - 将 viewGroup 添加到 ListView?

标签 android listview

为我的公司做一些研发。我们正在尝试拥有一个包含 ImageView 的 ListView ,以及 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>

这是我试图用于将在 ListView 中的项目的布局文件。在我们的 ImageAdapter(它扩展了 ArrayAdapter)的 getView 中, 我正在尝试使用 LayoutInflater 来膨胀 xml,然后将其存储到 ViewGroup 中。我通过 findViewByID() 获取 xml 中的 imageView,并设置我们想要的 imageView 的属性。

如果我们继续扩充这个 xml 文件,所有的 id 都将相同。这是我们的问题。

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

我们有更多问题,但我们会在修复这些更严重的错误后发回。

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;
    }

最佳答案

默认情况下, subview 在 ListView 中不可聚焦。这是为了防止奇怪的轨迹球/非触摸导航行为。这可能就是您的编辑文本没有响应输入的原因。确保您正在调用 ListView.setItemsCanFocus(true) 方法。

关于android - 将 viewGroup 添加到 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3195585/

相关文章:

android - 单击列表项而不是启动另一个 Activity 后应用程序不断崩溃

javascript - 在线程休眠方面,移动设备网络浏览器是否有任何标准?

android - 带有 AutoCompleteTextView 的 TextInputLayout 的默认文本

android - 为 Android 编译时显示 FFMPEG 错误

android - 使用 ActionBarSherlock 看不到我的 fragment

java - 如何更新 ListView 中的记录以及android中的数据库

java - 包含的 ListView 不会填充

android - EditText 在 ListView 中滚动时丢失内容?

android - R8 : Program type already present: androidx. 数据绑定(bind).library.baseAdapters.BR

android - NPE @ android.support.v7.widget.RecyclerView$LayoutManager.detachViewInternal