Display blank lines in Android list

So, I am wondering how to start populating a ListView blank lines. ListView populated through SQLite db, so let's say, for example, there are only 3 items in the list. I want to fill the rest of the screen with blank lines. Here is a screenshot of what I mean. Yes, I know this from the iPhone, but it demonstrates what I mean:

image 1

+4
source share
5 answers
Others have not escaped this, but I thought it might help post a piece of code. If there is a better technique, I will gladly take a voice if I find out something.

A cheap way is to add extra β€œlines” to your xml layout. Any additional lines will be cropped by the screen. This can become messy: on a higher resolution screen, you may need to add many additional text elements. Since they are cropped, I suppose you could add as much as you want. It will look something like this:

 <LinearLayout> <ListView></ListView> <TextView/> <TextView/> <TextView/> ... </LinearLayout> 

Another option is to add extra rows to the ListView, as mentioned earlier. If you are attached to an array, add extra lines to the array and process it accordingly. By the way, if you use a cursor, you can use MaxtrixCursor and MergeCursor, as described in this post: fooobar.com/questions/185704 / ...

I ended up using a combination of these methods. I try to count the number of rows that I want to add to my ListView, but I am mistaken on the side of caution and have a couple of TextViews under my ListView that make it all merge.

+3
source

When you create an Array, it will be bound to the ListView, you just need to add a few lines to the end of the array with empty lines.

+2
source
 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 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 height and draw them until you hit the // ListView 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 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 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; // } } } 
+1
source

Using the text view below the list seems to give an illusion of what I'm going to do.

0
source

@Kabir solution works. Now, if you look like me (you wanted to have two alternative colors in the background, this is his loose method, rewritten (or even edited)

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

I forgot these two Paint colors (same as @kabir declared colors):

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

Source: https://habr.com/ru/post/1397443/


All Articles