2013-10-23 39 views
2

我建立的Android 2.2+Android的ListView的平滑滾動到頂部按預期

我使用ListView方法smoothScrollToPosition(0)到返回列表的頂部滾動不工作。它大部分時間都在工作,但如果某些列表項高度變爲可變,那麼smoothScrollToPosition(0)會在到達較長列表項時停止。

這是一個錯誤?或者我做錯了什麼?

這裏是樣品Activity代碼產生的問題:

public class SimpleListViewActivity extends Activity { 
    private ListView mainListView ; 
    private Button btnScrollToTop ; 
    private ArrayAdapter<String> listAdapter ; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mainListView = (ListView) findViewById(R.id.mainListView); 

     // some test data 
     String[] planets = new String[] { "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40", 
            "Mercury","Venus", "Earth", "Mars", 
            "1\n2\n3\n4\n5\n6\n7\n8\n9\n10", 
            "Jupiter", "Saturn","Uranus", 
            "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40", 
            "Neptune", 
            "Ceres", 
            "Pluto", 
            "Haumea", 
            "Makemake", 
            "Eris" 
           }; 
     ArrayList<String> planetList = new ArrayList<String>(); 
     planetList.addAll(Arrays.asList(planets)); 

     listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList); 
     mainListView.setAdapter(listAdapter);  


     //scroll to top 
     btnScrollToTop = (Button) findViewById(R.id.btn_scroll_to_top); 
     btnScrollToTop.setOnClickListener(new OnClickListener(){ 

       @Override 
       public void onClick(View v) { 
       mainListView.smoothScrollToPosition(0); 
       } 

     }); 
    } 
} 

XML主要佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <Button 
     android:id="@+id/btn_scroll_to_top" 
     android:text="Scroll To Top" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
    /> 

    <ListView android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/mainListView"> 
    </ListView> 

</LinearLayout> 

列表行佈局:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rowTextView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:textSize="16sp" > 
</TextView> 

回答

0

發現了它的已知的bug與Android ICS和果凍豆。 http://code.google.com/p/android/issues/detail?id=37278

我現在有以下解決方法。它的工作原理,但並不理想,因爲有明顯的速度變化,同時滾動

代碼頂部滾動:

ScrollToTopListener scrollToTopListener=new ScrollToTopListener(); 
mainListView.setOnScrollListener(scrollToTopListener); 
scrollToTopListener.scrollToTop(); 
messagesList.smoothScrollToPosition(0); 

ScrollToTopListener類:

public class ScrollToTopListener implements OnScrollListener { 

    private boolean scrollToTop=false; 

    public ScrollToTopListener(){ 

    } 


    public void scrollToTop(){ 
     scrollToTop=true; 
    } 


    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, 
     int visibleItemCount, int totalItemCount) { 


     if(scrollToTop){ 
      if(firstVisibleItem != 0) { 
       view.smoothScrollToPosition(0); 
       } 
       else { 
         scrollToTop=false; 
       } 
     } 

    } 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     if(scrollState == SCROLL_STATE_TOUCH_SCROLL){ 
     scrollToTop=false; 
    } 

    } 
} 
0

使用ListView.setSelection(位置)從滾動你想要的位置

0

使用view.smoothScrollToPositionFromTop(0,0)代替view.smoothScrollToPosition(0);