ListView not scroll smoothly

Ravi Source

I have a list view in which i have inflating 3 different type of view, and one type of that view contain horizontal scroll view. Inside horizontal scroll view i have linear layout,in which i am inflating my custom xml. All of this working, but scroll of parent list view is not smooth. When i comment my horizontal scroll view code then list is scrolling smoothly. Below is my code :

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    view = convertView;
    int type = getItemViewType(position);
    if(view == null){
         mHolder = new ViewHolder();
    if (type == 0) {
         mHolder.mainLinearType1 = (LinearLayout) view.findViewById(R.id.main_linear);
         mHolder.hsv1 = (HorizontalScrollView) view.findViewById(R.id.hsv);
    }
    if (type == 1) {
    ....
    }
    if (type == 2) {
     ....
    }
    view.setTag(mHolder);
 }
  else{
        mHolder = (ViewHolder) view.getTag();
 }
 if(type==0){
  mHolder.mainLinearType1.removeAllViews();

  for (int i = 0; i <mList.get(position).getEventList().size(); i++) {
      View additionView = mLayoutInflater.inflate(R.layout.activity_type1_subitem_view, null,false);

      FrameLayout innerLinnerLayout=(FrameLayout)additionView.findViewById(R.id.frame_view);

      ImageView activityImage = (ImageView) additionView.findViewById(R.id.activity_iv);

      ProgressBar pb = (ProgressBar) additionView.findViewById(R.id.progress);

      AQueryImageDownloader.loadImage(activityImage, pb, mList.get(position).getEventList().get(i).getEventImagePath(), mContext);

      mHolder.mainLinearType1.addView(innerLinnerLayout);    
    } 
  }

I have try a lot but not able to make scrolling properly. I have also try view pager and horizontal listView. But same issue is occur. Please help in solve this issue.

Thanks in advance.

EDITED

Below is my listview inside xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:id="@+id/container_ll" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent"
   android:background="@color/White">

  <include
       android:id="@+id/header_view" 
       layout="@layout/header_layout" />

<ListView 
        android:id="@+id/list_view"
        android:layout_below="@+id/header_view"
        android:scrollbars="none"
        android:visibility="gone"
        android:divider="@color/LtGreen"
        android:dividerHeight="1dp"
        android:smoothScrollbar="true"
        android:scrollingCache="false"
        android:animationCache="false"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 </RelativeLayout>
androidlistviewhorizontalscrollview

Answers

answered 3 years ago A Honey Bustard #1

Check this :

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

This basically tells you to use an ASyncTask to handle the scrolling.

answered 3 years ago hidro #2

According to your implementation, you are always inflating the horizontal scrollview for each item, regardless of whether it is a recycled one, so every time Android needs to display an item, it needs to reinflating the horizontal scrollview again, which may explain why the scrolling is not smooth.

You can try to make a ListView as part of your item view. This ListView will be inflated once when your convertView == null, and everytime getView(position, ...) of the parent ListView is called you refresh the item's ListView Adapter with values of mList.get(position).getEventList(). Since we only swap data of a recycled view, my guess is that it will not affect scrolling much.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    view = convertView;
    int type = getItemViewType(position);
    if (view == null) {
         mHolder = new ViewHolder();
      if (type == 0) {
           mHolder.mainLinearType1 = (LinearLayout) view.findViewById(R.id.main_linear);
           mHolder.hsv1 = (HorizontalScrollView) view.findViewById(R.id.hsv);
           mHolder.listView = (ListView) view.findViewById(R.id.frame_view); // assume this is your ListView
      }
      if (type == 1) {
      ....
      }
      if (type == 2) {
       ....
      }
      view.setTag(mHolder);
    } else {
        mHolder = (ViewHolder) view.getTag();
    }
    if (type==0) {
      mHolder.mainLinearType1.removeAllViews();
      mHolder.listView.setAdapter(new Adapter() {
        // override adapter with implementation that returns items from mList.get(position).getEventList()
      });
    }
}

comments powered by Disqus