2013-03-20 174 views
1

我想要分页实现TableLayout,显示10页在1页,然后在10页2页等记录...我使用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); 
} 

回答

1

我已经使用表布局,但使用本教程,它只是成长水平不是垂直

有关GitHub上的代码没有得到我需要的结果 它似乎在PaginationLayout上使用addView将该视图添加到HorizontalScrollView。在您的代码中,您正在创建向其中添加行的单个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,你看,你已经添加了10行然后创建一个新的TableLayout并将其添加到包装LinearLayout

我建议你放弃该库并实现自己的分页。如果您没有直接与用户进行直接互动(通过触摸),那么使用底部的两个按钮来设置ViewFlipper(带有两个视图,您可以继续翻转的页面),非常简单,可以控制分页。如果您还想要触摸交互,请使用ViewPager(需要做更多工作)。