我想在Android中創建一個ListView。當我點擊一個項目時,我想讓它滾動到頂部。我怎樣才能做到這一點?下面是我想出來的活動類,項目選擇工作正常,但它不會在滾動到頂部Android的ListView如何滾動選定的項目頂部
public class MyListActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
//l.setSelection(2);
Toast.makeText(this, position + " selected", Toast.LENGTH_LONG).show();
//l.smoothScrollToPosition(5);
}
}
在這裏看到我的回答,http://stackoverflow.com/a/29345217/1881527 –