0

Hey I am using Listview under a scroll view. I want to scroll the ListView upto a position dynamically, am trying to do it with the following code but its not scrolling. can anybody please give me a solution.

listView.setSmoothScrollbarEnabled(true); listView.smoothScrollToPosition(position);

Shruti
  • 55
  • 7

3 Answers3

0

Instead of using the ScrollToPosition try using the setSelection method of the listView

example:

listView.setSelection(position);
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

you should be use only listview and remove scrollview from its parent view and put above item of list view as header of listview and same below of listview set as footer of list view.

Pratyesh
  • 686
  • 5
  • 9
0
You can do this just use this class

package com.whathappened.utils;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Helper {
//  public static void getListViewSize(DragSortListView myListView) {
//      
//      ListAdapter myListAdapter = myListView.getAdapter();
//      if (myListAdapter == null) {
//          //do nothing return null
//          return;
//      }
//      //set listAdapter in loop for getting final size
//      int totalHeight = 0;
//      for (int size = 0; size < myListAdapter.getCount(); size++) {
//          View listItem = myListAdapter.getView(size, null, myListView);
//          if(listItem!=null){
//          listItem.measure(0, 0);
//          totalHeight += listItem.getMeasuredHeight();
//          }else{
//              
//          }
//      }
//      //setting listview item in adapter
//      ViewGroup.LayoutParams params = myListView.getLayoutParams();
//      params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
//      myListView.setLayoutParams(params);
//      // print height of adapter on log
//      Log.i("height of listItem:", String.valueOf(totalHeight));
//  }



    public static void getListViewSizeGroup(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            if(listItem!=null){
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
            }else{

            }
        }
        //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
}


After setting the adapter just call Helper.getListViewSizeGroup(yourlistviewobject);
it will scroll..
user
  • 245
  • 1
  • 4
  • 12