android - Is it possible to dynamically add AndroidAnnotation @EViewGroups instead of using the traditional inflate directly? -
i'm trying use android annotations , dynamically add layout components when createnewrow button clicked. app runs , displays default rows defined in activity_main.xml and, after clicking createnewbutton see children attached dynamictable in debugger new children not displayed. here main activity xml:
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/inflatelayout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="net.richardriley.inflate.app.mainactivity"> <button android:id="@+id/createnewrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/newrowbutton"/> <tablelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dynamictable"> <tablerow> <textview android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </tablerow> </tablelayout> </linearlayout>
so linear layout button , table container. clicking createnewrow should add new inflatedrow. here xml row want dynamically add:
inflatedrow.xml
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" > <textview android:id="@+id/inflatedrowtextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/inflatedrowlabel"/> <button android:id="@+id/inflatedrowbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/defaultnewrowtext" /> </merge>
using aa subclass tablerow is
inflatedrow.java
package net.richardriley.inflate.app; import android.content.context; import android.widget.button; import android.widget.tablerow; import android.widget.textview; import org.androidannotations.annotations.eviewgroup; import org.androidannotations.annotations.viewbyid; /** * inflate : created rgr on 20/03/14. */ @eviewgroup(r.layout.inflatedrow) public class inflatedrow extends tablerow { @viewbyid button inflatedrowbutton; @viewbyid textview inflatedrowtextview; public inflatedrow(context context) { super(context); } }
and main activity java itself:
mainactivity.java
package net.richardriley.inflate.app; import android.support.v7.app.actionbaractivity; import android.widget.linearlayout; import android.widget.tablelayout; import org.androidannotations.annotations.*; import org.slf4j.logger; import org.slf4j.loggerfactory; import static ch.qos.logback.classic.android.basiclogcatconfigurator.configuredefaultcontext; @optionsmenu(r.menu.main) @eactivity(r.layout.activity_main) public class mainactivity extends actionbaractivity { static { configuredefaultcontext(); log = loggerfactory.getlogger(mainactivity.class); } @viewbyid linearlayout inflatelayout; @viewbyid tablelayout dynamictable; public mainactivity() { } protected static logger log; @click void createnewrow() { log.info("clicked"); inflatedrow_ inflatedrow=new inflatedrow_(this); dynamictable.addview(inflatedrow,0); /*layoutinflater layoutinflater= (layoutinflater)getsystemservice(context.layout_inflater_service); view view = layoutinflater.inflate(r.layout.inflatedrow,null); dynamictable.addview(view);*/ } @optionsitem void opensettingsselected(){ log.info("hello"); } }
in createnewrow if use inflater service directly works.
what missing?
many thanks.
don't use annotated class when you're inflating view , make sure you're calling build method inflates it.
@click void createnewrow() { log.info("clicked"); inflatedrow inflatedrow = new inflatedrow_.build(this); dynamictable.addview(inflatedrow,0); }
Comments
Post a Comment