简体   繁体   中英

Am I inflating my custom view correctly?

I have a custom view that is just an image that, when tapped, shows some text as an overlay.

I don't think I'm inflating it right because when I do:

ImageTapView imageTapView = new ImageTapView(context);
ImageView imageView = (ImageView) imageTapView.findViewById(R.id.image);

Here, imageView comes back as null even though it's defined in my layout.xml file.

Here's my custom class:

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

And here's my image_tap_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>

And finally in my list view adapter, here's where I'm creating it:

public View getView(int position, View convertView, ViewGroup parent){
       ImageTapView imageTapView = new ImageTapView(context);
       ImageView imageView = (ImageView) imageTapView.findViewById(R.id.image);

       //ERROR GETS THROWN HERE: imageView = null

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

通过此作为第三个参数ViewGroupinflateinflate(context, R.layout.image_tap_view, this);

  private void init(Context context){
        inflate(context, R.layout.image_tap_view, null);
  }

That's an helper method of View. Its implementation is

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

so, providing null as root will make it return the view inflated. And you have should add it to the View's hierarchy, through addView , that is not available only for ViewGroup . Back to your question, the way you are using it, is neutral

You have to do this
In your adapters getView() the first statement should be

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

First, remove your ImageTapView class. It's redundant. Anyway, you're right: The view is not inflated correctly. Instead, do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM