Reputation: 5058
how to set Fixed no of Rows shows in ListView ? i want to set 5 Rows only show in Listview not all Rows. so how can i achive this Goal?
Upvotes: 5
Views: 11331
Reputation: 327
Here it is how I did it:
Step 1: Set fixed height to 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 fixed height to the list, more exactly 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"/>
I hope it helps !
Upvotes: 3
Reputation: 1094
You can custom your adapter. But I think your idea is SET MAX ITEM OF LIST VIEW. (I think it will better).
So you will custom your adapter look like:
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 u !
Upvotes: 0
Reputation: 3809
This can be achieved by setting the row item height to fixed dps and the List View height to be 5 times of row height in exact dps.
Upvotes: 2
Reputation: 6037
yes, you can achieve via adapter class, Try with following code in your adapter class.
public int getCount() {
return 5;
}
If you set this, the adapter class load only 5 items.
Upvotes: 3