2013-03-20 144 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(需要做更多工作)。