下面列出了android.widget.ListView#smoothScrollToPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void scrollList(int scrollPosition)
{
if(scrollPosition > 0)
{
ListView lv = getListView();
if(lv.getFirstVisiblePosition() == 0)
{
int num = lv.getCount();
int sp = scrollPosition;
if(scrollPosition >= num)
sp = num - 1;
if(sp >= 0)
//lv.setSelection(sp);
lv.smoothScrollToPosition(sp);
}
}
}
private void init() {
iLanguage();
sendMessage("talk", getIPAddress() + strJoinToChatroom);
app = (ApplicationUtil) this.getApplication();
tvShow = (TextView) findViewById(R.id.tv_show);
tvShow.setText(strShow);
btnBack = (Button) findViewById(R.id.btn_back);
btnBack.setOnClickListener(this);
btnSend = (Button) findViewById(R.id.btn_send);
btnSend.setText(strSend);
btnSend.setOnClickListener(this);
etContent = (EditText) findViewById(R.id.et_content);
lvTalk = (ListView) findViewById(R.id.lv_talk);
lvTalk.setDividerHeight(0);
adapter = new TalkAdapter(this, list);
lvTalk.setAdapter(adapter);
lvTalk.smoothScrollToPosition(adapter.getCount() - 1);
// tHandler.post(tRunnable);
mThreadClient = new Thread(tRunnable);
mThreadClient.start();
}
/**
* Used internally to manage the "What's New" screen
*
* @param adapter
* @param items
* @param more
* @return whether or not more items can be added
*/
protected static boolean addWhatsNewItem(ListView list, ArrayAdapter<String> adapter, Stack<String> items, Button more, boolean fullScroll) {
//Don't try if we're out of items
if (items.empty()) {
more.setVisibility(View.GONE);
return false;
}
//Add another items
adapter.add(items.pop());
if (fullScroll) {
//Scroll all the way down
list.smoothScrollToPosition(adapter.getCount());
}
//Are there more items to add?
if (items.empty()) {
more.setVisibility(View.GONE);
return false;
} else {
more.setVisibility(View.VISIBLE);
return true;
}
}
public static void stopListViewScrollingAndScrollToTop(ListView listView) {
Runnable runnable = JavaReflectionUtility.getValue(listView, "mFlingRunnable");
listView.removeCallbacks(runnable);
listView.setSelection(Math.min(listView.getFirstVisiblePosition(), 5));
listView.smoothScrollToPosition(0);
}
public static void smoothScrollToPositionCompat(ListView listView,
int position, int offset) {
if (android.os.Build.VERSION.SDK_INT >= 11) {
listView.smoothScrollToPositionFromTop(position, offset);
}
{
int firstVisible = listView.getFirstVisiblePosition();
int lastVisible = listView.getLastVisiblePosition();
if (position < firstVisible)
listView.smoothScrollToPosition(position);
else
listView.smoothScrollToPosition(position + lastVisible
- firstVisible - 2);
}
}
public static void stopListViewScrollingAndScrollToTop(ListView listView) {
Runnable runnable = JavaReflectionUtility.getValue(listView, "mFlingRunnable");
listView.removeCallbacks(runnable);
listView.setSelection(Math.min(listView.getFirstVisiblePosition(), 5));
listView.smoothScrollToPosition(0);
}