Android: how to set Fixed List (5) rows in a ListView, and then after scrolling?

How to set a fixed number of rows in a ListView? I want 5 rows to display not all rows only in Listview. so how can i achieve this goal?

+7
source share
4 answers

yes, you can reach through the adapter class, try with the following code in your adapter class.

public int getCount() { return 5; } 

If you set this, the adapter class will load only 5 items.

+3
source

This can be achieved by setting the line position height to a fixed dps and the list height 5 times the line height in the exact dps format.

+2
source

Here is how I did it:

Step 1: Set a fixed height in the item list ITEM_LIST_HEIGHT

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="25dp"> </TextView> 

Step 2: Set a fixed height to the list, more precisely LIST_HEIGHT = NUMBER_OF_ITEMS_TO_DISPLAY x ITEM_LIST_HEIGHT. For example, for 6 items 150;

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:layout_width="wrap_content" android:layout_height="150dp"/> 

Hope this helps!

+2
source

You can configure your adapter. But I think your idea is SET MAX ITEM OF LIST VIEW. (I think it will be better).

So, you will configure your adapter as follows:

 private class CustomAdapter<T> extends ArrayAdapter<T> { private static final int MAX_ROW_DISPLAY = 5; private List<T> mItems; public CustomAdapter(Context context, int resource, List<T> objects) { super(context, resource, objects); mItems = objects; } @Override public int getCount() { if (mItems == null) { return 0; } return Math.min(MAX_ROW_DISPLAY, mItems.size()); } } 

Hope this help helps you!

0
source

All Articles