在Android列表视图中显示空行

所以我想知道如何用空行填充ListViewListView是通过SQLite db填充的,所以说例如列表中只有3个项目我想用空行填充屏幕的其余部分。 这是我的意思的屏幕截图。 是的,我知道它来自iPhone,但它certificate了我的意思:

图片1

其他人已经躲过了,但我认为发布一个代码片段可能会有所帮助。 如果有更好的技巧,如果我学到了什么,我会很乐意投票。

便宜的方法是在布局xml中添加额外的“行”。 任何额外的行都将被屏幕截断。 这可能会变得混乱:更高分辨率的屏幕可能需要您添加许多额外的TextView。 由于它们被切断了,我想你可以根据自己的喜好添加多少。 它看起来像这样:

      ...  

另一个选项是向ListView添加额外的行,如前所述。 如果绑定到数组,请向该数组添加额外的空行并适当地处理它。 巧合的是,如果您使用Cursor,您可以使用MaxtrixCursor和MergeCursor,如本文所述: https ://stackoverflow.com/a/16440093/103131

我最终使用了这些方法的组合。 我尽力计算我想要添加到ListView的行数,但是我在注意方面有错误,并且在我的ListView下面有一些TextViews,它们都会聚集在一起。

当你创建数组时,它将被绑定到ListView,你只需要在数组的末尾添加几行空字符串。

 just make sure android:layout_height="match_parent"     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; // } } } 

使用listview下面的textview似乎给出了我想要的幻想。

@ kabir的解决方案有效。 现在,如果你像我一样(想要在背景中有两种替代颜色,这是他重写的方法(或者说编辑后)

  @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; } 

我忘记了这两种Paint颜色(就像@kabir声明颜色一样):

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