【问题标题】:How can I make a dynamically generated tablelayout scrollable?如何使动态生成的表格布局可滚动?
【发布时间】:2019-06-16 18:10:58
【问题描述】:

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

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

TableLayoutScrollView 内。 ScrollView 用于测试,通常它只是 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));
}

这会创建一个超出页面末尾且无法滚动的表格。

关于包含片段的 Activity 以及不包含的内容,我没有发布相当多的其他代码。如果你想要这些,请告诉我。

感谢您的帮助。

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

【问题讨论】:

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


    【解决方案1】:

    我已尝试通过 Activity 实现您的代码。我发布了我的 Activity 和 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 个虚拟行来测试它的滚动行为,并想通知您它完全可以正常工作。您的 Activity 或 Fragment 父代码中可能存在阻止表格滚动的问题。

    【讨论】:

    • 你认为这可能与我是一个片段而你的是一个活动有关吗?
    • 可能有错误。您可以尝试发布您的完整代码,或者如果您希望社区详细了解它,也可以发布 Git 链接。
    • 我将您的代码放入一个全新的应用程序中进行测试,它可以正常工作。然后我将该代码放入我的片段中,但它没有工作,所以我猜还有其他问题。如果您不介意快速浏览一下我的 GitHub,我将不胜感激。它是一个私人仓库,所以只需给我发你的电子邮件,我会加你的。
    • 找不到与该电子邮件地址关联的 github 帐户。
    • @Zac 这是我的 GitHub 个人资料的链接github.com/atishagrawal
    【解决方案2】:

    您可以使用 TableView 库 让您的 Table 可滚动 使用动态数据。链接如下:-
    https://github.com/evrencoskun/TableView

    【讨论】:

    • 感谢您的建议。我喜欢这个样子。如果一切都失败了,我会开始学习它。
    猜你喜欢
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多