Java 在Android listview中显示空行
所以我想知道如何用空行填充Java 在Android listview中显示空行,java,android,listview,Java,Android,Listview,所以我想知道如何用空行填充列表视图。ListView是通过SQLitedb填充的,比如说,列表中只有3项我想用空行填充屏幕的其余部分。这是我的意思的截图。是的,我知道它来自iPhone,但它说明了我的意思: 创建数组时,将绑定到ListView,您只需在数组末尾添加几行空字符串。在ListView下方使用textview似乎给人一种我想要的幻觉。其他人对此避而不谈,但我认为发布代码片段可能会有所帮助。如果有更好的技巧,如果我学到了一些东西,我会很乐意投反对票 廉价的方法是在布局xml中添加额外的
列表视图。ListView
是通过SQLite
db填充的,比如说,列表中只有3项我想用空行填充屏幕的其余部分。这是我的意思的截图。是的,我知道它来自iPhone,但它说明了我的意思:
创建数组时,将绑定到ListView,您只需在数组末尾添加几行空字符串。在ListView下方使用textview似乎给人一种我想要的幻觉。其他人对此避而不谈,但我认为发布代码片段可能会有所帮助。如果有更好的技巧,如果我学到了一些东西,我会很乐意投反对票
廉价的方法是在布局xml中添加额外的“行”。任何额外的行都将被屏幕切断。这可能会变得混乱:更高分辨率的屏幕可能需要添加许多额外的文本视图。既然它们被切断了,我想你可以随意添加。它看起来像这样:
<LinearLayout>
<ListView></ListView>
<TextView/>
<TextView/>
<TextView/>
...
</LinearLayout>
...
另一个选项是向ListView添加额外的行,如前所述。如果绑定到数组,请向数组中添加额外的空行,并对其进行适当处理。巧合的是,如果您使用的是游标,则可以使用MaxtrixCursor&MergeCursor,如本文所述:
我最终使用了这些方法的组合。我尽力计算要添加到ListView的行数,但出于谨慎,我犯了一个错误,在我的ListView下面有两个文本视图,使所有的行都聚集在一起 只需确保android:layout\u height=“match\u parent”
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;
// }
}
}
公共类CustomListView扩展了ListView{
私有油漆mPaint=新油漆();
private Paint mPaintBackground=new Paint();
公共CustomListView(上下文、属性集属性){
超级(上下文,attrs);
mPaint.setColor(Color.BLACK);
mpainbackground.setColor(Color.WHITE);
}
@凌驾
受保护的void dispatchDraw(画布){
super.dispatchDraw(画布);
//ListView的高度
最终int currentHeight=getMeasuredHeight();
//这将让您知道ListView的状态,fitting/not
//合适的内容
final int scrolledHeight=computeVerticalScrollRange();
//如果(scrolledHeight>=currentHeight | | scrolledHeight==0){
//没有必要画东西(为了简单起见,我假设
//如果适配器没有项目,我就不会在屏幕上画东西
//屏幕。如果您仍然需要这些行,请选择一个合适的值
//模拟行的高度并绘制它们,直到到达
//ListView的getMeasuredHeight)
//}其他{
最终视图lastChild=getChildAt(getChildCount()-1);
if(lastChild==null)返回;
//用于知道从何处开始绘制线的值
final int lastChildBottom=lastChild.getBottom();
//最后一个孩子的身高(使用此值确定适当的值
//(用于行高)
final int lastChildHeight=lastChild.getMeasuredHeight();
//确定填充ListView所需的行数
最终整数nrOfLines=(当前高度-最后一个孩子底部)
/最后儿童身高;
//我用它来模拟ListView行背景的特殊颜色
Rect r=new Rect(0,lastChildBottom,getMeasuredWidth(),
getMeasuredHeight());
canvas.drawRect(r,mPaintBackground);
画布.抽绳(0,底部,
getMeasuredWidth(),lastChildBottom,mPaint);
对于(int i=0;i
@kabir的解决方案有效。现在,如果你像我一样(想要在背景中有两种不同的颜色,这是他重写(或者说编辑)的dispached方法)
HM,但是我只想填充屏幕的空白区域(如果它存在),如果我只有空白的SQLite条目,滚动时仍然显示空行。你可以将ListVIEW放到ReleLayayOUT中,并设置ReleLayOUT高度=“fILIL PrimeNead”?你也可以在java文件中找到屏幕的高度并确定多少个要添加的“空白”行。我想到了一个替代解决方案,该解决方案不涉及添加空项。我只是包装了listview的内容,并在下面添加了一个视图,可以在其中绘制线条。将线条向下……现在只需获得与listview相匹配的行距。
@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;
}
alternateColorView1.setColor(....);
alternateColorView1.setStyle(Paint.Style.FILL);
alternateColorView2.setColor(....);
alternateColorView2.setStyle(Paint.Style.FILL);