滚动视图下滚动listView不会发生

时间:2014-06-06 11:06:54

标签: android

嘿我在滚动视图下使用Listview。我想动态地将ListView滚动到一个位置,我尝试使用以下代码但不滚动它。任何人都可以给我一个解决方案。

listView.setSmoothScrollbarEnabled(真); listView.smoothScrollToPosition(位置);

3 个答案:

答案 0 :(得分:0)

而不是使用ScrollToPosition尝试使用listView的setSelection方法

示例:

listView.setSelection(position);

答案 1 :(得分:0)

您应该只使用listview并从其父视图中删除scrollview  将列表视图的项目放在listview的标题上并且相同 列表视图下方设置为列表视图的页脚。

答案 2 :(得分: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..