Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contact-app like scrollinglist on android

I'm writing my first android app (I'm a noob at android, but decent at java). The first screen of the app consists of a huge list (about 1.5K items) of Manga-objects. The code I use is as following:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <ListView android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:id="@+id/android:list"></ListView>
        <TextView android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:text="@string/list_no_items"
            android:id="@+id/android:empty"></TextView>
</LinearLayout>

row.xml:

<LinearLayout android:id="@+id/LinearLayout01"
              android:layout_width="fill_parent" 
              android:layout_height="?android:attr/listPreferredItemHeight"
              android:padding="6dip"
              xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:src="@drawable/icon" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/toptext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 
            android:id="@+id/bottomtext"
            android:singleLine="true"
            android:ellipsize="marquee"
        />
    </LinearLayout>
</LinearLayout>

Then I have a adapter which basically takes a Manga-object and puts it's name in the rows toptext, and it's latest updated date in the bottomtext. However, (this might be caused by the virtualization of the unit though), the result is really slow. Scrolling the list takes forever and you can only scroll small peaces of the time. How can I make the list work like the one in the contacts-app? So that when you start scrolling a handle pops out at the right side of the screen, and when you drag it letters shows up as of how far you've scrolled (the list is sorted alphabetically), and also, is there a way I could improve the performance of the list?

like image 776
Alxandr Avatar asked Feb 02 '26 06:02

Alxandr


1 Answers

To add the thumb scroller, adjust your ListView in your main.xml like so:

<ListView android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:id="@+id/android:list"
        android:fastScrollEnabled="true"></ListView>

Adding the android:fastScrollEnabled="true" attribute.

https://developer.android.com/reference/android/widget/AbsListView.html#attr_android:fastScrollEnabled

You may also look at using a SectionIndexer.

like image 195
Jim Blackler Avatar answered Feb 04 '26 22:02

Jim Blackler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!