Java 我是否正确地膨胀了自定义视图?

Java 我是否正确地膨胀了自定义视图?,java,android,Java,Android,我有一个自定义视图,它只是一个图像,当点击时,显示一些文本作为覆盖 我不认为我把它夸大了,因为当我这样做的时候: ImageTapView imageTapView = new ImageTapView(context); ImageView imageView = (ImageView) imageTapView.findViewById(R.id.image); 这里,imageView返回为null,即使它是在my layout.xml文件中定义的 这是我的自定义类: public cl

我有一个自定义视图,它只是一个图像,当点击时,显示一些文本作为覆盖

我不认为我把它夸大了,因为当我这样做的时候:

ImageTapView imageTapView = new ImageTapView(context);
ImageView imageView = (ImageView) imageTapView.findViewById(R.id.image);
这里,
imageView
返回为null,即使它是在my layout.xml文件中定义的

这是我的自定义类:

public class ImageTapView extends View {
    public ImageTapView(Context context){
        super(context);
        init(context);
    }
    public ImageTapView(Context context, AttributeSet attrs){
        super(context, attrs);
        init(context);
    }
    private void init(Context context){
        inflate(context, R.layout.image_tap_view, null);
    }
}
这是我的
图像\u tap\u view.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Test Image~"/>
        </RelativeLayout>
</LinearLayout>

将其作为
inflate
方法
inflate中的第三个参数
ViewGroup
(context,R.layout.image\u tap\u view,this)传递

将其作为
充气
方法
充气中的第三个参数
视图组
(上下文,R.layout.image\u点击视图,此)

这是视图的辅助方法。它的实施非常困难

public static View inflate(Context context, int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
}
因此,将
null
提供为root将使其返回视图。您应该通过
addView
,将其添加到视图的层次结构中,这不仅适用于
ViewGroup
。回到你的问题上来,你使用它的方式是中立的

这是视图的辅助方法。它的实施非常困难

public static View inflate(Context context, int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
}

因此,将
null
提供为root将使其返回视图。您应该通过
addView
,将其添加到视图的层次结构中,这不仅适用于
ViewGroup
。回到您的问题,您使用它的方式是中立的

您必须在适配器getView()中执行此操作

LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 if(convertView==null){
  convertView = inflater.inflate(
                    R.layout.image_tap_view, parent, false);}

必须在适配器getView()中执行此操作
第一条语句应该是

LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 if(convertView==null){
  convertView = inflater.inflate(
                    R.layout.image_tap_view, parent, false);}

首先,删除ImageTapView类。这是多余的。无论如何,你是对的:视图没有正确地膨胀。相反,请执行以下操作:

public View getView(int position, View convertView, ViewGroup parent) {
   if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.image_tap_view, parent, false);
   }
   ImageView imageView = (ImageView) convertView.findViewById(R.id.image);

   try {
       String url = "http://www.example.com/lionshop/" + images.get(position);
       DownloadPicasso(url, imageView);
   } catch (Exception e){
       Log.d("LoadImage", e.getLocalizedMessage());
   }

   imageView.setAdjustViewBounds(true);
   imageView.setScaleType(ImageView.ScaleType.FIT_XY);
   return convertView;
}

首先,删除ImageTapView类。这是多余的。无论如何,你是对的:视图没有正确地膨胀。相反,请执行以下操作:

public View getView(int position, View convertView, ViewGroup parent) {
   if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.image_tap_view, parent, false);
   }
   ImageView imageView = (ImageView) convertView.findViewById(R.id.image);

   try {
       String url = "http://www.example.com/lionshop/" + images.get(position);
       DownloadPicasso(url, imageView);
   } catch (Exception e){
       Log.d("LoadImage", e.getLocalizedMessage());
   }

   imageView.setAdjustViewBounds(true);
   imageView.setScaleType(ImageView.ScaleType.FIT_XY);
   return convertView;
}

太棒了,非常感谢!我创建了
ImageTapView
类,这样我就可以编写一些
onClick
事件等等。这应该用其他方法吗?太棒了,非常感谢!我创建了
ImageTapView
类,这样我就可以编写一些
onClick
事件等等。这应该用别的方法吗?非常感谢。在这种情况下,“中立”是什么意思?这没有什么区别。在这种情况下,中立意味着什么?它没有区别