问题
I'm trying to use Android Annotations and dynamically add layout components when my createNewRow button is clicked. The app runs and displays the default rows defined in activity_main.xml and, after clicking createNewButton I see children attached to my dynamicTable in the debugger but the new children are not displayed. Here is my 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 simply a linear layout with a button and then a table container. Clicking createNewRow should add a new InflatedRow. Here is the XML for the row I want to 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 my subclass for a 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 by 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 finally my 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 I use the inflater service directly it works.
What am I missing?
many thanks.
回答1:
Don't use the annotated class when you're inflating the view and make sure you're calling the build method that inflates it.
@Click
void createNewRow() {
log.info("clicked");
InflatedRow inflatedRow = new InflatedRow_.build(this);
dynamicTable.addView(inflatedRow,0);
}
回答2:
instead of 'View inflatedRow= new InflatedRow_(this);' I use 'View inflatedRow=InflatedRow_.build(this);'. This is documented for custom controls and I needed to do it for a merged control group too. So mea culpa to a degree!
@Click
void createNewRow() {
log.info("clicked");
InflatedRow_ inflatedRow=InflatedRow_.build(this);
dynamicTable.addView(inflatedRow,0);
}
Alternatively (and no idea if this would continue to be supported:
@Click
void createNewRow() {
log.info("clicked");
/*View inflatedRow = InflatedRow_.build(this);*/
InflatedRow_ inflatedRow = new InflatedRow_(this);
inflatedRow.onFinishInflate();
dynamicTable.addView(inflatedRow, 0);
}
来源:https://stackoverflow.com/questions/22538117/is-it-possible-to-dynamically-add-androidannotation-eviewgroups-instead-of-usin