Frage

I made a viewstub that is inflated and created again many times. The layout it uses includes a "delete" button that hides the viewstub (or deflates it).

When I'm creating a new viewstub I call this code:

ViewStub eventStub = new ViewStub(this);
            eventStub.setLayoutResource(R.layout.event_container);
            eventContainerMain.addView(eventStub);
            eventStub.inflate();

Problem is, in the viewstub layout the delete button is created with the new viewstub. So how do I make the delete button hide the viewstubthats inside?

Most importantly, How do I make other methods affect only the view its inside?

War es hilfreich?

Lösung

You aren't quite using view stubs correctly...

First, point your view to the layout you desire for the view you want to duplicate by using this:

ViewStub stub = new ViewStub(this);
    stub.setLayoutResource(R.layout.viewStubLayout);
    stub.inflate();

Next, go into your XML and make sure the buttons on your layout have the android:onClick option using the correct method, which is declared in your class.

For deleting view stubs, you don't actually delete them... you're supposed to use .setVisibility(GONE) or .setVisibility(VISIBLE) to manage if users can see it or not. This would be used in the method in which onClick is directed to. Also, using "this" when referring to the view stub your objects are in will allow you to manage what happens in the specified view the user clicks in.

Andere Tipps

You do not delete view stubs in the way that you want them to be deleted. When inflated, ViewStubs simply disappear from the parent and are replaced with a View object. If you want to use methods on the view that is created you can use the android:inflatedId attribute in the ViewStub xml file.

You say that the layout you are using has a "delete" button that "deflates" the ViewStub, but in reality what is happening is that you are deleting the View that is created when you inflate the ViewStub. Once a ViewStub is inflated it is deleted from the parent automatically and so after inflation there wouldn't be a Viewstub in existence to delete.

What it sounds like you want is to dynamically delete the View created by the inflated ViewStub. For this I suggest looking at Add and Remove Views in Android Dynamically?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top