如何使动态生成的表格布局可滚动?

时间:2019-01-23 07:55:46

标签: java android android-layout tablelayout android-tablelayout

我在一个Activity中有3个片段,在两个片段中,我在TableLayout中有一个硬编码的ConstraintLayout,表的标题和行是使用MySQL服务器的信息动态生成的。鉴于其动态性质,该表能够使页面溢出,并且我希望它是可滚动的。

在StackOverflow上已经有多个问题,人们想要一个可滚动的表,但是在这些问题中,表及其数据都是硬编码的(不知道这是否相关)。我已经尝试过在这些问题中提出的解决方案,但是它们对我不起作用。解决方案通常是将TableLayout包装在ScrollView中,或者使用android:isScrollContainer="1",但这两种方法都不适合我。

TableLayout中的

ScrollViewScrollView在这里进行测试,通常只是TableLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".ViewProductLocFragment">
    <!-- Rest of the fragment -->

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:layout_weight="1"
        android:scrollbars="none"
        app:layout_constraintBottom_toTopOf="@+id/menuBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view">

        <TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginBottom="5dp"
            android:stretchColumns="*"
            app:layout_constraintBottom_toTopOf="@+id/menuBar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view">
        </TableLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

这是填表的代码。

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_view_product_main, container, false);
    initializeTableLayout(view);
    fillTable(((ViewProductActivity)getActivity()).pallets);

    return view;
}

private void fillTable(PalletsResult pallets)
{
    Integer count = 0;
    for(Pallet pallet : pallets.getPallets()) {

        String barcode = pallet.getBarcode();
        String location = pallet.getCode();
        Integer quantity = pallet.getQuantity();

        TableRow tableRow = new TableRow(getContext());
        if (count % 2 != 0) {
            tableRow.setBackgroundColor(getResources().getColor(R.color.lightGrey));
        }
        tableRow.setId(View.generateViewId());
        tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        TextView labelBarcode = new TextView(getContext());
        labelBarcode.setId(View.generateViewId());
        labelBarcode.setGravity(Gravity.CENTER);
        labelBarcode.setTextColor(getResources().getColor(R.color.standardTextColor));
        labelBarcode.setTextSize(18);
        labelBarcode.setText(barcode);
        tableRow.addView(labelBarcode);

        TextView labelLocation = new TextView(getContext());
        labelLocation.setId(View.generateViewId());
        labelLocation.setGravity(Gravity.CENTER);
        labelLocation.setTextColor(getResources().getColor(R.color.standardTextColor));
        labelLocation.setTextSize(18);
        labelLocation.setText(location);
        tableRow.addView(labelLocation);

        TextView labelQuantity = new TextView(getContext());
        labelQuantity.setId(View.generateViewId());
        labelQuantity.setGravity(Gravity.CENTER);
        labelQuantity.setTextColor(getResources().getColor(R.color.standardTextColor));
        labelQuantity.setTextSize(18);
        labelQuantity.setText(quantity.toString());
        tableRow.addView(labelQuantity);

        tableLayout.addView(tableRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        count++;
    }
}

private void initializeTableLayout(View view)
{
    tableLayout = view.findViewById(R.id.tableLayout);
    TableRow tr_head = new TableRow(getContext());
    tr_head.setId(View.generateViewId());
    tr_head.setBackgroundColor(getResources().getColor(R.color.lightGrey));
    tr_head.setLayoutParams(new LayoutParams(
        LayoutParams.MATCH_PARENT,
        LayoutParams.WRAP_CONTENT
        ));

    TextView label_barcode = new TextView(getContext());
    label_barcode.setId(View.generateViewId());
    label_barcode.setText("BARCODE");
    label_barcode.setTextSize(20);
    label_barcode.setTextColor(Color.BLACK);
    label_barcode.setGravity(Gravity.CENTER);
    tr_head.addView(label_barcode);// add the column to the table row here

    TextView label_location = new TextView(getContext());
    label_location.setId(View.generateViewId());// define id that must be 
unique
    label_location.setText("LOCATION"); // set the text for the header
    label_location.setTextSize(20);
    label_location.setTextColor(Color.BLACK); // set the color
    label_location.setGravity(Gravity.CENTER);
    tr_head.addView(label_location); // add the column to the table row here

    TextView label_quantity = new TextView(getContext());
    label_quantity.setId(View.generateViewId());// define id that must be 
unique
    label_quantity.setText("QTY"); // set the text for the header
    label_quantity.setTextSize(20);
    label_quantity.setTextColor(Color.BLACK); // set the color
    label_quantity.setGravity(Gravity.CENTER);
    tr_head.addView(label_quantity); // add the column to the table row here

    tableLayout.setScrollContainer(true);
    tableLayout.addView(tr_head, new TableLayout.LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT));
}

这将创建一个从页面末尾开始并且无法滚动的表。

关于活动包含片段和不包含的片段,我还没有发布很多其他代码。让我知道您是否想要任何一个。

感谢您的帮助。

更新:我在应用的其他地方实现了一个类似的表,该表与示例中发布的代码配合得很好。这意味着我遇到的问题是表中的一个片段而不是一个活动。稍后我将发布一些片段代码。

2 个答案:

答案 0 :(得分:0)

我尝试通过活动实现您的代码。我正在发布我的“活动”和XML代码供您参考,您可以查看完整代码中缺少的内容,以便有所帮助。

MainActivity.java

public class MainActivity extends Activity {

TableLayout tableLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initializeTableLayout();

}


@Override
protected void onResume() {
    super.onResume();

    fillTable();
}

private void fillTable() {
    Integer count = 0;
    for (int i = 0; i < 100; i++) {

        String barcode = "#0000000000";
        String location = "Location";
        Integer quantity = 1;

        TableRow tableRow = new TableRow(MainActivity.this);
        if (count % 2 != 0) {
            tableRow.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
        }
        tableRow.setId(View.generateViewId());
        tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        TextView labelBarcode = new TextView(MainActivity.this);
        labelBarcode.setId(View.generateViewId());
        labelBarcode.setGravity(Gravity.CENTER);
        labelBarcode.setTextColor(getResources().getColor(R.color.colorAccent));
        labelBarcode.setTextSize(18);
        labelBarcode.setText(barcode);
        tableRow.addView(labelBarcode);

        TextView labelLocation = new TextView(MainActivity.this);
        labelLocation.setId(View.generateViewId());
        labelLocation.setGravity(Gravity.CENTER);
        labelLocation.setTextColor(getResources().getColor(R.color.colorAccent));
        labelLocation.setTextSize(18);
        labelLocation.setText(location);
        tableRow.addView(labelLocation);

        TextView labelQuantity = new TextView(MainActivity.this);
        labelQuantity.setId(View.generateViewId());
        labelQuantity.setGravity(Gravity.CENTER);
        labelQuantity.setTextColor(getResources().getColor(R.color.colorAccent));
        labelQuantity.setTextSize(18);
        labelQuantity.setText(quantity.toString());
        tableRow.addView(labelQuantity);

        tableLayout.addView(tableRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        count++;
    }
}

private void initializeTableLayout() {
    tableLayout = this.findViewById(R.id.tableLayout);
    TableRow tr_head = new TableRow(MainActivity.this);
    tr_head.setId(View.generateViewId());
    tr_head.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
    tr_head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    TextView label_barcode = new TextView(MainActivity.this);
    label_barcode.setId(View.generateViewId());
    label_barcode.setText("BARCODE");
    label_barcode.setTextSize(20);
    label_barcode.setTextColor(Color.BLACK);
    label_barcode.setGravity(Gravity.CENTER);
    tr_head.addView(label_barcode);// add the column to the table row here

    TextView label_location = new TextView(MainActivity.this);
    label_location.setId(View.generateViewId());// define id that must be         unique
    label_location.setText("LOCATION"); // set the text for the header
    label_location.setTextSize(20);
    label_location.setTextColor(Color.BLACK); // set the color
    label_location.setGravity(Gravity.CENTER);
    tr_head.addView(label_location); // add the column to the table row here

    TextView label_quantity = new TextView(MainActivity.this);
    label_quantity.setId(View.generateViewId());// define id that must be         unique
    label_quantity.setText("QTY"); // set the text for the header
    label_quantity.setTextSize(20);
    label_quantity.setTextColor(Color.BLACK); // set the color
    label_quantity.setGravity(Gravity.CENTER);
    tr_head.addView(label_quantity); // add the column to the table row here

    tableLayout.setScrollContainer(true);
    tableLayout.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}

}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:layout_weight="1"
        android:scrollbars="none">

        <TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginBottom="5dp"
            android:stretchColumns="*"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"></TableLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

我已经手动添加了100个虚拟行来测试其滚动行为,并想通知您它确实可以正常工作。您的“活动”或“片段”父代码中可能存在问题,导致该表无法滚动。

答案 1 :(得分:0)

您可以使用 TableView库使表可滚动并包含动态数据。链接如下:-
https://github.com/evrencoskun/TableView