如何修复ScrollView中的运行时动态表布局UI错误

时间:2019-09-10 10:28:46

标签: java android user-interface android-tablelayout

我是一名小型开发人员。我正在为B2b开发产品。我使用了动态表结构,该结构列出了存储库中的产品数量。我使用可滚动的布局,因为表中的产品与屏幕尺寸不匹配。但是,在滚动产品后,我注意到滚动时表格的单元格不可见。我搜索了但没有结果。我该如何解决这个问题?

滚动前:

enter image description here

滚动后:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/TablePage_VerticalScroll"
    android:background="@color/AppColor_Gray">
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/TablePage_Layout"
            android:orientation="vertical">

        </TableLayout>
</ScrollView>
public class TableResultActivity extends Activity {
    ProgressDialog dialog;

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

        final TableLayout layout = findViewById(R.id.TablePage_Layout);
        String itemCode = getIntent().getStringExtra("ItemCode");


        dialog = new ProgressDialog(this);
        dialog.setButton("iptal", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        dialog.setCancelable(false);
        dialog.setMessage("İşleminiz Yapılıyor Lütfen Bekleyiniz");
        dialog.show();

        RemoteConnection connection = new RemoteConnection(this);
        RestInterface restInterface = connection.getClient().create(RestInterface.class);

        restInterface.getTable(itemCode).enqueue(new Callback<StockResult_Class>() {
            @Override
            public void onResponse(Call<StockResult_Class> call, Response<StockResult_Class> response) {
                if (response.code() == 200){
                    StockResult_Class result_class = response.body();
                    ArrayToTable converter = new ArrayToTable(TableResultActivity.this);
                    TableLayout tableLayout = new TableLayout(TableResultActivity.this);
                    for (List<StockResponse_Class> responseList : result_class.getStockResponse()){
                        tableLayout.addView(converter.HeaderRow(responseList.get(0).getWarehouseCode(),responseList.get(0).getItemCode(),responseList.get(0).getWarehouseName()));
                        tableLayout.addView(converter.BodyHeaderRow());
                        for (StockResponse_Class response_value : responseList){
                            tableLayout.addView(converter.BodyRow(response_value));
                        }
                        tableLayout.addView(converter.Footer(responseList.get(0).getCount()));
                        tableLayout.setPadding(10,10,10,10);
                        layout.addView(tableLayout);
                        tableLayout = new TableLayout(TableResultActivity.this);
                    }
                    tableLayout.addView(converter.FinalFooter(response.body().getAllCount()));
                    layout.addView(tableLayout);
                }
                dialog.cancel();
            }

            @Override
            public void onFailure(Call<StockResult_Class> call, Throwable t) {
                Log.d("Table Request Error",t.getMessage());
                dialog.cancel();
            }
        });
    }
}
public class ArrayToTable {
    public Context context;

    public ArrayToTable(Context context) {
        this.context = context;
    }

    public TableRow HeaderRow(String warehouseCode, String ıtemCode,String WarehouseName) {
        String[] arr = {ıtemCode, warehouseCode,WarehouseName};
        TableRow row = new TableRow(context);

        for (String value : arr) {
            CustomTableCell headerCell = CreateCell.createView(context);
            headerCell.setText(value);
            headerCell.setPadding(6, 6, 6, 6);
            headerCell.setTextColor(Color.WHITE);
            headerCell.setBackgroundColor(Color.BLACK);
            headerCell.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            row.addView(headerCell);
        }
        return row;
    }

    public TableRow BodyHeaderRow() {
        String[] BodyHeaderArr = {"Renk Kodu", "Renk", "Toplam"};
        TableRow row = new TableRow(context);

        for (String value : BodyHeaderArr) {
            CustomTableCell bodyHeaderCell = CreateCell.createView(context);
            bodyHeaderCell.setText(value);
            bodyHeaderCell.setPadding(10,10,10,10);
            bodyHeaderCell.setTextColor(Color.BLACK);
            bodyHeaderCell.setBackgroundColor(Color.WHITE);
            row.addView(bodyHeaderCell);

            if (value == "Renk") {
                ////////
                String[] colorCodes = {"00 ", "01 ", "03 ", "05 ", "07 ", "09 ", "11 ", "13 "};
                TableLayout subTable = new TableLayout(context);
                TableRow subRow = new TableRow(context);
                for (String value1 : colorCodes) {
                    CustomTableCell bodyHeaderSubCell = CreateCell.createView(context);
                    bodyHeaderSubCell.setText(value1);
                    bodyHeaderSubCell.setPadding(10,10,10,10);
                    bodyHeaderSubCell.setTextColor(Color.BLACK);
                    bodyHeaderSubCell.setBackgroundColor(Color.WHITE);
                    bodyHeaderSubCell.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                    subRow.addView(bodyHeaderSubCell,new TableRow.LayoutParams(70,70));
                }
                subTable.addView(subRow);
                row.addView(subTable);

                ////////
            }
        }


        return row;
    }

    public TableRow BodyRow(StockResponse_Class response_value) {
        List<String> bodydata = new ArrayList<>();
        bodydata.add(response_value.getColorCode());
        bodydata.add(response_value.getColorDesc());
        bodydata.add(String.valueOf(response_value.getCount()));
        TableRow row = new TableRow(context);

        for (String value : bodydata) {
            CustomTableCell bodyCell = CreateCell.createView(context);
            bodyCell.setText(value);
            bodyCell.setPadding(10, 10, 10, 10);
            bodyCell.setTextColor(Color.BLACK);
            bodyCell.setBackgroundColor(Color.WHITE);
            row.addView(bodyCell);

            if (bodydata.get(1).equals(value)) {
                ////////
                TableLayout subTable = new TableLayout(context);
                TableRow subRow = new TableRow(context);
                for (double count : response_value.getDimCount()) {
                    CustomTableCell bodySubCell = CreateCell.createView(context);
                    if (count > 0){
                        bodySubCell.setText(String.valueOf((int) count));
                    }
                    bodySubCell.setPadding(10,10,10,10);
                    bodySubCell.setTextColor(Color.BLACK);
                    bodySubCell.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                    bodySubCell.setBackgroundColor(Color.WHITE);
                    subRow.addView(bodySubCell,new TableRow.LayoutParams(70,70));
                }
                subTable.addView(subRow);
                row.addView(subTable);
            }
        }
        return row;
    }
    public TableRow Footer(Double count){
        String[] arr = {"Ara Toplam", String.valueOf(count)};
        TableRow row = new TableRow(context);

        for (String value : arr) {
            CustomTableCell cell = CreateCell.createView(context);
            cell.setText(value);
            cell.setPadding(10, 10, 10, 10);
            cell.setTextColor(Color.WHITE);
            cell.setBackgroundColor(Color.BLACK);
            row.addView(cell);
        }
        return row;
    }
    public TableRow FinalFooter(Double count){
        String[] arr = {"Genel Toplam ", String.valueOf(count)};
        TableRow row = new TableRow(context);

        for (String value : arr) {
            CustomTableCell cell = CreateCell.createView(context);
            cell.setText(value);
            cell.setPadding(10, 10, 10, 10);
            cell.setTextColor(Color.WHITE);
            cell.setBackgroundColor(Color.BLACK);
            row.addView(cell);
        }
        return row;
    }
}
public class CreateCell {
    public static CustomTableCell createView(Context context){
        return new CustomTableCell(context) {
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                Rect rect = new Rect();
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(Color.BLACK);
                paint.setStrokeWidth(2);
                getLocalVisibleRect(rect);
                canvas.drawRect(rect, paint);
            }

        };
    }
}
public class CustomTableCell extends TextView {

    public CustomTableCell(Context context) {
        super(context);
    }
}

0 个答案:

没有答案