gpt4 book ai didi

android - 带分页的 TableLayout

转载 作者:行者123 更新时间:2023-11-30 03:42:23 28 4
gpt4 key购买 nike

我想实现一个带分页的 TableLayout,在第 1 页显示 10 条记录,然后在第 2 页显示下 10 条记录,依此类推...我正在使用 Table Layout但我没有得到所需的结果。使用本教程,表格只是水平增长而不是垂直增长。我如何更改它以便它也垂直增长,然后当记录大小大于 10 时转移到下一页?

代码:

public class Sample extends Activity {
String companies[] = {"Google","Windows","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung","Google","Windows","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung",
"Google","Windows","iPhone","Nokia","Samsung"};
String os[] = {"Android","Mango","iOS","Symbian","Bada",
"Android","Mango","iOS","Symbian","Bada",
"Android","Mango","iOS","Symbian","Bada","Android","Mango","iOS","Symbian","Bada",
"Android","Mango","iOS","Symbian","Bada",
"Android","Mango","iOS","Symbian","Bada"};
TextView companyTV,valueTV,deviceTv,actionTv,dateTv,timeTv,creditTv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// creates the Pagination Layout
PaginationLayout paginationLayout = new PaginationLayout(this);
paginationLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

// creates content only for sample
TableLayout table = new TableLayout(this);
//table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
table.addView(row);

TableRow row2 = new TableRow(this);
row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
table.addView(row2);
for (int i=0;i<companies.length;i++)
{
deviceTv = new TextView(this);
deviceTv.setText(companies[i]);
deviceTv.setTextColor(Color.RED);
deviceTv.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

deviceTv.setPadding(5, 5, 5, 5);
row2.addView(deviceTv);
actionTv = new TextView(this);
actionTv.setText(os[i]);
actionTv.setTextColor(Color.GREEN);

actionTv.setPadding(5, 5, 5, 5);
actionTv.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
row2.addView(actionTv); // Adding textView to tablerow.
}

/* for(int i = 0; i< 50;i++){
Button button = new Button(this);
button.setText("Button " + i);
if (i%2==0) {
row.addView(button);
} else {
row2.addView(button);
}
}*/

// add the content in pagination
paginationLayout.addView(table);
// set pagination layout
setContentView(paginationLayout);
}

最佳答案

I have already using Table Layout but not getting my required result using this tutorial it just grow horizontally not vertically

对于 github 上的代码,似乎在 PaginationLayout 上使用 addView 将该 View 添加到 Horizo​​ntalScrollView。在您的代码中,您正在创建一个单个 Tablelayout 并向其添加行,因此您的内容垂直增长是正常的。

您很可能需要自己插入页面,像这样:

// creates the Pagination Layout
PaginationLayout paginationLayout = new PaginationLayout(this);
LinearLayout wrapper = new Linearlayout(this);
paginationLayout.addView(wrapper);
// now create a TableLayout, if the content is bigger then your intended number of rows
// then insert another Tablelayout and so on
TableLayout table = new TableLayout(this);
//table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
table.addView(row);
TableRow row2 = new TableRow(this);
row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
table.addView(row2);
wrapper.addView(table); // it will probably don't look good if you don't
// also setup the proper width with LayoutParams

如果您继续添加 TableRows 并且您看到您已经向 table 添加了 10 行,那么创建一个新的 TableLayout 并添加它到包装器 LinearLayout

我建议您放弃该库并实现您自己的分页。如果您不了解用户的直接交互(通过触摸),那么设置一个 ViewFlipper(有两个 View ,两个页面,您将继续翻阅)非常简单,两个按钮位于底部控制分页。如果您还需要触摸交互,请使用 ViewPager(还有更多工作要做)。

关于android - 带分页的 TableLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15522141/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com