开发者

Android :how to set Fixed no(5 ) of Rows shows in ListView then after Scroll?

how to set Fixed no of Rows shows in ListView ? i want to set 5 Rows only show in Listview no开发者_开发问答t all Rows. so how can i achive this Goal?


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.


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.


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 !


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 !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜