2012-02-20 186 views
4

所以我想知道如何去填充空行ListViewListView通過SQLite db填充,所以說例如列表中只有3個項目,我想用空行填充屏幕的其餘部分。這是我的意思的截圖。是的,我知道它從iPhone的,但它證明了我的意思:在Android listview顯示空行

image 1

回答

2

當您創建將被綁定到ListView控件數組,你只需要在數組的末尾添加幾行與空串。

+0

嗯,但我只是想以填充屏幕的空白區域(如果存在的話),像上面如果我只是空白sqlite的條目,他們仍顯示空行滾動時 – Paul 2012-02-20 15:01:09

+0

你能放置的ListView。在RelativeLayout中設置RelativeLayout height =「fill_parent」? – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 2012-02-20 15:10:42

+0

你也可以在你的java文件中找到屏幕的高度並確定e要添加多少「空白」行。 – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 2012-02-20 15:11:50

0

在列表視圖下面使用textview似乎給我想要的幻覺。

+0

cn你粘貼ur代碼片段? – Android 2013-05-28 05:48:33

3

其他人已經躲過了,但我認爲這可能有助於張貼代碼片段。如果有更好的技術,如果我學到了一些東西,我會很樂意做出反對票。

便宜的方法是在佈局xml中添加額外的「行」。任何額外的行將被屏幕截斷。這可能會變得麻煩:更高分辨率的屏幕可能需要添加許多額外的TextView。既然它們被切斷了,我想你可以添加儘可能多的你想要的。它看起來像這樣:

<LinearLayout> 

<ListView></ListView> 

<TextView/> 
<TextView/> 
<TextView/> 
... 

</LinearLayout> 

另一種選擇是添加額外的行到您的ListView,如前所述。如果綁定到一個數組,請將多餘的空白行添加到數組中並進行適當的處​​理。巧合的是,如果你使用的是光標,你可以使用本文描述的MaxtrixCursor & MergeCursor:https://stackoverflow.com/a/16440093/103131

我最終使用了這些方法的組合。我盡我所能來計算我想添加到ListView中的行數,但是我在警告方面出現錯誤,並且在我的ListView下面有幾個TextView,這些都使得它們全都聚集在一起。

1
just make sure  android:layout_height="match_parent" 

<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
      <your.app.package.CustomListView 
       android:id="@+id/editableTourListView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 
      </your.app.package.CustomListView> 
     </RelativeLayout> 





    public class CustomListView extends ListView { 
     private Paint mPaint = new Paint(); 
     private Paint mPaintBackground = new Paint(); 
     public CustomListView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      mPaint.setColor(Color.BLACK); 
      mPaintBackground.setColor(Color.WHITE); 
     }  
     @Override 
     protected void dispatchDraw(Canvas canvas) { 
      super.dispatchDraw(canvas); 
      // ListView's height 
      final int currentHeight = getMeasuredHeight(); 
      // this will let you know the status for the ListView, fitting/not 
      // fitting content 
      final int scrolledHeight = computeVerticalScrollRange(); 
      //  if (scrolledHeight >= currentHeight || scrolledHeight == 0) { 
      // there is no need to draw something(for simplicity I assumed that 
      // if the adapter has no items i wouldn't draw something on the 
      // screen. If you still do want the lines then pick a decent value 
      // to simulate a row's height and draw them until you hit the 
      // ListView's getMeasuredHeight) 
      //  } else { 
      final View lastChild = getChildAt(getChildCount() - 1); 
      if(lastChild==null) return; 
      // values used to know where to start drawing lines 
      final int lastChildBottom = lastChild.getBottom(); 
      // last child's height(use this to determine an appropriate value 
      // for the row height) 
      final int lastChildHeight = lastChild.getMeasuredHeight(); 
      // determine the number of lines required to fill the ListView 
      final int nrOfLines = (currentHeight - lastChildBottom) 
        /lastChildHeight; 
      // I used this to simulate a special color for the ListView's row background 
      Rect r = new Rect(0, lastChildBottom, getMeasuredWidth(), 
        getMeasuredHeight());   
      canvas.drawRect(r, mPaintBackground); 
      canvas.drawLine(0, lastChildBottom , 
        getMeasuredWidth(), lastChildBottom, mPaint); 
      for (int i = 0; i < nrOfLines; i++) { 
       canvas.drawLine(0, lastChildBottom + (i + 1) * lastChildHeight, 
         getMeasuredWidth(), lastChildBottom + (i + 1) 
         * lastChildHeight, mPaint); 
      } 
      return; 
      //  } 
    } 
    } 
+0

你是一個天才 – 2017-03-03 15:55:50

0

@ kabir的解決方案的作品。現在,如果你像我一樣(希望有背景的兩種交替的顏色,這是他改寫dispached方法(或讓我們說編輯)

@Override 
protected void dispatchDraw(Canvas canvas) { 
    super.dispatchDraw(canvas); 

    int caseLastChildBottom = -1; 
    int caselastChildHeight = -1; 
    int caseNrOfLines = -1; 

    //makes the colors follow the order (alternateColor1 - alternateColor2 - alternateColor1 - etc.) 
    int plusIndex = 1; 
    if (this.getChildCount() % 2 == 0) 
     plusIndex = 0; 

    // ListView's height 
    final int currentHeight = getMeasuredHeight(); 

    // this will let you know the status for the ListView, fitting/not fitting content 
    final int scrolledHeight = computeVerticalScrollRange(); 

    //empty listview (no item) 
    if (scrolledHeight == 0) { 
     //no childs exist so we take the top 
     caseLastChildBottom = 0; 
     //last child doesn't exist, so we set a default height (took the value in dp in the item row's height) 
     caselastChildHeight = convertDpToPx(DEFAULT_CHILD_ROW_S_HEIGHT); 
     // determine the number of lines required to fill the ListView 
     caseNrOfLines = currentHeight/caselastChildHeight; 
    } 

    //there is a remaining gap to fill 
    else { 
     final View lastChild = getChildAt(getChildCount() - 1); 
     if (lastChild == null) return; 
     // values used to know where to start drawing lines 
     caseLastChildBottom = lastChild.getBottom(); 
     // last child's height(use this to determine an appropriate value for the row height) 
     caselastChildHeight = lastChild.getMeasuredHeight(); 
     // determine the number of lines required to fill the ListView 
     caseNrOfLines = (currentHeight - caseLastChildBottom)/caselastChildHeight; 
    } 
    // values used to know where to start drawing lines 
    final int lastChildBottom = caseLastChildBottom; 
    // last child's height(use this to determine an appropriate value for the row height) 
    final int lastChildHeight = caselastChildHeight; 
    // determine the number of lines required to fill the ListView 
    final int nrOfLines = caseNrOfLines; 

    int i = 0; 
    for (i = 0; i < nrOfLines; i++) { 
     Rect r = new Rect(0, lastChildBottom + i * lastChildHeight, getMeasuredWidth(), lastChildBottom + (i + 1) * lastChildHeight); 
     canvas.drawRect(r, (i + plusIndex) % 2 == 0 ? alternateColorView1 : alternateColorView2); 
    } 

    //is there a gap at the bottom of the list 
    if(currentHeight - (nrOfLines *lastChildHeight) > 0){ 
     Rect r = new Rect(0, lastChildBottom + i * lastChildHeight,getMeasuredWidth(), currentHeight); 
     canvas.drawRect(r, (i + plusIndex) % 2 == 0 ? alternateColorView1 : alternateColorView2); 
    } 
    return; 
} 

我忘了這兩種油漆顏色(就像@kabir聲明的顏色) :

alternateColorView1.setColor(....); 
    alternateColorView1.setStyle(Paint.Style.FILL); 

    alternateColorView2.setColor(....); 
    alternateColorView2.setStyle(Paint.Style.FILL);