我正在尝试将TableRows动态添加到TableLayout。 我将TableRow子类化为viewHolder类,然后从XML文件中扩展了View。我只在结果UI中看到最后创建的TableRow
扩展的TableRow类
public class CustomRow extends TableRow {
private TextView columnData1;
private TextView columnData2;
private TextView columnDataN;
public CustomRow(Context context, ViewGroup
parent) {
super(context);
// Inflating TableRow View Here
View view =
LayoutInflater.from(context).
inflate(R.layout.netdata_tablerow,
parent, true);
view.setLayoutParams(parent.getLayoutParams());
columnData1 = view.findViewById(R.id.columnData1);
columnData2 = view.findViewById(R.id.columnData2);
columnDataN = view.findViewById(R.id.columnData1N);
public void bindData(Data data) {
column1.setText(data.text1());
...
}
正在膨胀的TableRow XML文件
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center"
android:id="@+id/columnData1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center"
android:id="@+id/columnData2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center"
android:id="@+id/columnDataN"/>
</TableRow>
Activity_layout.xml包含TableLayout和一些静态标题
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:fillViewport="true">
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:stretchColumns="*"
android:orientation="vertical">
<!--Header Row-->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:text="HEADER1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:text="HEADER2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:text="HEADERN" />
</TableRow>
<!-- Dynamically Add Table Data Here -->
</TableLayout>
MainActivity从列表中提取数据并创建CustomRow对象,并将其添加到TableLayout
public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private Button button;
private TableLayout tableLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tableLayout = (TableLayout) findViewById(R.id.tableLayout);
NetworkDataList list = NetworkDataList.getNetworkList(this);
for (int i = 0; i < list.size(); i++) {
NetworkData item = list.getNetwork(i);
CustomRow newRow = new
CustomRow(this, tableLayout);
newRow.bindData(item);
tableLayout.addView(newRow);
}
}
我只看到显示的CustomRow对象之一,最后一个在静态标题行下方。