我正在为Android 2.2+构建
我正在使用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 :(得分:0)
发现它是Android ICS和Jelly bean的已知错误。 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;
}
}
}
答案 1 :(得分:0)
使用ListView.setSelection(position)从您想要的位置滚动
答案 2 :(得分:0)
使用view.smoothScrollToPositionFromTop(0,0)代替view.smoothScrollToPosition(0);