【发布时间】:2012-02-20 14:54:52
【问题描述】:
所以我想知道如何用空行填充ListView。 ListView 是通过 SQLite db 填充的,例如,列表中只有 3 个项目我想用空行填充屏幕的其余部分。这是我的意思的屏幕截图。是的,我知道它来自 iPhone,但它证明了我的意思:
【问题讨论】:
所以我想知道如何用空行填充ListView。 ListView 是通过 SQLite db 填充的,例如,列表中只有 3 个项目我想用空行填充屏幕的其余部分。这是我的意思的屏幕截图。是的,我知道它来自 iPhone,但它证明了我的意思:
【问题讨论】:
其他人已经避开了它,但我认为发布代码 sn-p 可能会有所帮助。如果有更好的技术,如果我学到了一些东西,我很乐意投反对票。
便宜的方法是在布局 xml 中添加额外的“行”。任何多余的行都会被屏幕截断。这可能会变得一团糟:更高分辨率的屏幕可能需要您添加许多额外的 TextView。既然它们被切断了,我想你可以添加任意数量的东西。它看起来像这样:
<LinearLayout>
<ListView></ListView>
<TextView/>
<TextView/>
<TextView/>
...
</LinearLayout>
另一个选项是向您的 ListView 添加额外的行,如前所述。如果您绑定到一个数组,请向该数组添加额外的空白行并适当地处理它。巧合的是,如果您使用光标,则可以使用 MaxtrixCursor 和 MergeCursor,如本文所述:https://stackoverflow.com/a/16440093/103131
我最终使用了这些方法的组合。我尽力计算要添加到 ListView 中的行数,但出于谨慎考虑,我犯了错误,并且在我的 ListView 下方有几个 TextView 将它们组合在一起。
【讨论】:
当您创建数组时,它将被绑定到 ListView,您只需在数组末尾添加几行空字符串即可。
【讨论】:
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;
// }
}
}
【讨论】:
在列表视图下方使用文本视图似乎给人一种我想要的错觉。
【讨论】:
@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;
}
我忘记了这两种油漆颜色(就像@kabir 声明的颜色一样):
alternateColorView1.setColor(....);
alternateColorView1.setStyle(Paint.Style.FILL);
alternateColorView2.setColor(....);
alternateColorView2.setStyle(Paint.Style.FILL);
【讨论】: