2

My ListView is pretty simple, uses ViewHolder approach, but "rubber" scroll does not work. If i run the list by finger it just down/up for 4-5 rows and stops (all the list has about 50 rows).

adapter getView and getHolder methods

private ViewHolder getHolder(View vi) {
    ViewHolder holder = new ViewHolder();
    holder._id = (TextView) vi.findViewById(R.id._id);
    holder.creator = (TextView) vi.findViewById(R.id.creator);
    holder.message = (TextView) vi.findViewById(R.id.message);
    holder.updated = (TextView) vi.findViewById(R.id.updated);
    holder.userid = (TextView) vi.findViewById(R.id.userid);

    return holder;
}         

@Override 
public View getView(int position, View convertView, ViewGroup parent) {
    // return super.getView(position, convertView, parent);
    ViewHolder holder;

    View vi = convertView;
        if (vi == null || vi.getTag() == null) {
            vi = inflater.inflate(itemLayoutIdCommon, null);
            holder = getHolder(vi);
            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }
    }

    holder._id.setText((CharSequence) mValues.get(position).get("_id"));
    holder.creator.setText((CharSequence) mValues.get(position).get("creator"));
    holder.updated.setText((CharSequence) mValues.get(position).get("updated"));
    holder.userid.setText((CharSequence) mValues.get(position).get("userid"));
    holder.message.setText((CharSequence) mValues.get(position).get("title"));

    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.message.getLayoutParams();
    holder.message.setBackgroundResource(layoutChooser.getBackground(mValues.get(position)));
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
    layoutParams.addRule(layoutChooser.getAlign(mValues.get(position))); // ALIGN_PARENT_RIGHT / LEFT etc.
    holder.message.setLayoutParams(layoutParams);
    return vi;
}

public static class ViewHolder {
    TextView _id;
    TextView userid;
    TextView message;
    TextView creator;
    TextView updated;
}

and row_layout.xml (itemLayoutIdCommon)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
    android:id="@+id/_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="_id"
    android:visibility="gone" />

<TextView
    android:id="@+id/userid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="userid"
    android:visibility="gone" />

<TextView
    android:id="@+id/creator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/updated"
    android:tag="creator"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white"
    android:textSize="14sp"
    android:visibility="invisible" />

<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="30dp"
    android:enabled="false"
    android:gravity="center_vertical|center_horizontal"
    android:tag="title"
    android:textColor="@color/black" >
</TextView>

<TextView
    android:id="@+id/updated"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:tag="updated"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/blue_border"
    android:textSize="14sp" />

</RelativeLayout>

** lisview.xml **

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dim_foreground_disabled_holo_dark"
android:orientation="vertical" >

<com.handmark.pulltorefresh.library.PullToRefreshListView
    xmlns:ptr="http://schemas.android.com/apk/res-auto"
    android:id="@+id/chat_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/tableRowMessage"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/actionBar1"
    android:layout_gravity="fill"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:background="@drawable/chatshape"
    android:dividerHeight="0dp"
    android:fillViewport="true"
    ptr:ptrAnimationStyle="flip"

    android:cacheColorHint="#00000000"
    android:fadingEdge="none"
    android:fastScrollEnabled="false"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="false"
    android:smoothScrollbar="true" 

    ptr:ptrMode="both" />

</RelativeLayout>
Oleg Karakoz
  • 532
  • 1
  • 6
  • 18
  • Try not setting layout params every time (just when you inflate) and you could also replace all the `mValues.get(position)` calls with a local variable so it does not need to lookup that value 10 times. You also shouldn't need to do `setLayoutParams` if that is the same param instance you got before via `getLayoutParams`. – zapl Nov 19 '12 at 13:13
  • Removing layout code does not help. Scrolling is still the same. No smooth. – Oleg Karakoz Nov 19 '12 at 13:18
  • Why are you modifying layout params in your getView method of the adapter? – Radu Dan Nov 19 '12 at 13:20
  • you can try to find the slow part using ddms tracing http://developer.android.com/tools/debugging/debugging-tracing.html - If you have 2 different layouts in a listview you can use the view types http://stackoverflow.com/questions/4777272/android-listview-with-different-layout-for-each-row - that way you'll be able to reuse views without the need to adjust the same convert view for every row – zapl Nov 19 '12 at 13:21
  • x.get(position) is just a getter. I'm sure it does not cost too much to make the code heavy and scrolling bad. Nevertheless, i tried to change it to one call and then to use local variable. It does not help. – Oleg Karakoz Nov 19 '12 at 13:22
  • @OlegPreobrazhenskyy the cost would be quite huge in case `mValues` is a `LinkedList`. `ArrayList` is cheap but caching the value locally is certainly the fastest. – zapl Nov 19 '12 at 13:26
  • zapl, I used two layouts approach for even/odd. But after the moment I noticed the list is not scrolling right I rewrited the code to use one layout but setting layout params for even/odd rows. No change both, with two layouts and with single one. – Oleg Karakoz Nov 19 '12 at 13:27
  • @zapl, rewrites to call with a local variable - does not help. removing all the lines related to LayoutParams - does not affect. – Oleg Karakoz Nov 19 '12 at 13:31
  • @zapl, also i used DDMS to profile the code before posting this question. Adapter produce a bit of load. It is not slow. – Oleg Karakoz Nov 19 '12 at 13:33
  • @OlegPreobrazhenskyy Okay, assuming `get("_id")` etc is fast would mean that`getView` is not the problem. Maybe: The layout you created for each item takes too much time to render / measure (see hierarchyviewer http://developer.android.com/tools/debugging/debugging-ui.html - you could try to use a simple test layout to verify), or the `PullToRefreshListView` implementation is slow (test with a regular `ListView`). Last and least probable: the device graphic hardware / software could be unable to produce smooth scrolling lists. But you would easily see that if no other app manages to do so. – zapl Nov 19 '12 at 13:53
  • @zapl, i did the same. Removed all the row fields of holder but one. Simplified the layout to... and smooth is still does not work. The hardware, be sure, too cute. This is not the first my application. All rest works good.. But there is little bug I can't see.. fta.. – Oleg Karakoz Nov 19 '12 at 14:05
  • seems i found a problem. that is not in adapter and not in its speed. all scrollings works as need but it happened the only after long-click tap... – Oleg Karakoz Nov 19 '12 at 16:09

2 Answers2

0

As Document says

The key to a smoothly scrolling ListView is to keep the application’s main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread.

you can get more details from Making ListView Scrolling Smooth

Hope it will help you.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
  • Mate, you haven't seen the code posted? Do you ensure that it does any disk access, network access, or SQL access? Thanks for the link i read it many times before. – Oleg Karakoz Nov 19 '12 at 13:15
0

Found an error. My activity used own onSwipe controller that prevented smooth scrolling returning true value from its motion event handler. Changing it to false (pass event to others) fixed the behavior.

grebulon
  • 7,697
  • 5
  • 42
  • 66
Oleg Karakoz
  • 532
  • 1
  • 6
  • 18