Android inflates view using existing view object

I have a custom view that I would like to create from a resource template. My custom view designer accepts additional parameters that are set as additional information for the custom view.

The problem is that when I inflate the view, I get a view object that is not a subclass of the user view, since the inflation method is static and returns a general new view instead of an instance of my user view.

I was looking for a way to inflate the view by passing it my own reference to the view object.

  public class MLBalloonOverlayView extends View {
     MiscInfo mMiscInfo;
     public MLBalloonOverlayView (Context context, MiscInfo miscInfo) {
         super (context);
         mMiscInfo = miscInfo;
     }
     public View create (final int resource, final OverlayItem item, 
                         MapView mapView, final int markerID) {
         ViewGroup viewGroup = null;
         View balloon = View.inflate (getContext (), resource, viewGroup);

       // I want to return this object so later I can use its mMiscInfo
       // return this;
         return balloon;
     }
 }
+4
source share
2 answers

Seeing the code https://github.com/galex/android-mapviewballoons I managed to update my code accordingly. The idea is that you create a layout from a resource, and then add a bloated view to an instance of the class that extends the layout (as Marcos suggested above).

public class MLBalloonOverlayView extends FrameLayout { public MLBalloonOverlayView(Context context, final OverlayItem overlayItem) { super(context); mOverlayItem = overlayItem; } public void create(final int resource, MapView mapView, final int markerID) { // inflate resource into this object TableLayout layout = new TableLayout(getContext()); LayoutInflater.from(getContext()).inflate(resource, layout); TableLayout.LayoutParams params = new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.gravity = Gravity.NO_GRAVITY; this.addView(layout, params); } } 
+1
source

Fill it to your object.

 public View create(final int resource, final OverlayItem item, MapView mapView, final int markerID) { LayoutInflater.from(getContext()).inflate(resource, this, true); return this; } 
+1
source

All Articles