2014-07-10 17 views
0

因此,我試圖在Android中創建一個GridView樣式佈局,但實際上並沒有使用自定義Gridview適配器,因爲我不想滾動它。我試過只是關閉滾動,但是因爲我在垂直LinearLayout中添加了其他元素,所以它毀了我的佈局。在Android中不使用GridView創建一個類似於GridView的佈局

我的下一個實驗是使用一個TableLayout,然後添加膨脹的佈局作爲表單元格,但我也有這個問題。下面是我對概念的簡單證明運行測試:

TableRow trackingActivityRow = new TableRow(getActivity()); 
     for(int j = 0; j < trackingActivities.size(); j ++) { 

      TrackingActivity trackingActivity = trackingActivities.get(j); 
      View trackingActivityCell = getActivity().getLayoutInflater() 
        .inflate(R.layout.table_cell_tracking_activity, trackingActivityRow, true); 
      TextView txtDescription = (TextView)trackingActivityCell.findViewById(R.id.txtDescription); 
      txtDescription.setText(trackingActivity.getDescription()); 



     } 
     tableLayout.addView(trackingActivityRow); 

它似乎正確地創建細胞的數量,但並不想設置像它應該是文本。此外,當涉及爲每4個TrackingActivities創建一個新行時,我遇到了一個邏輯問題。 如果任何人有任何意見,將不勝感激。

/更新/ 下面是該問題的圖形。帶有「Walk」的單元格顯示正確,但其他單元格僅在textview中顯示應該被替換的佔位符文本。

enter image description here

+0

你是說TextView的未顯示正常嗎?如果真是這樣,那麼是以哪種方式?同樣的快照將有助於 – humblerookie

+0

*,但它不想設置文本應該是。* - 意思是什麼? * ...爲每4個TrackingActivities創建一個新行。* - 您需要在for循環中爲每個'4'的多個'j'創建一行(也可將行添加到for循環中的表中)。 – Luksprog

+0

添加了圖形 – jwBurnside

回答

0

我創建了一個基於與listadapter一個tablelayout定製GridView控件。

在擴展TableLayout簡化了setAdapterMethod的版本:

int itemPos = -1; 
    int numRows = (int) Math.ceil((double) adapter.getCount()/(double) mNumColumns); 
    for (int yPos = 0; yPos < numRows; yPos++) { 
     //Create new row 
     TableRow tableRow = new TableRow(this.getContext()); 

     for (int xPos = 0; xPos < mNumColumns; xPos++) { 
      itemPos++; 
      View itemView = adapter.getView(itemPos, null, tableRow); 
      tableRow.addView(itemView); 
     } 
     this.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT)); 
    }