listview 平滑移动到最后一条
mChatList.smoothScrollToPosition(mChatList.getCount() - 1);
mChatList.smoothScrollToPositionFromTop(mChatListgetCount() - 1,0);
次两种方法 可以平滑移动 情况是item高度基本一致的情况
如果一个item很高 效果是没有的
mChatList.setSelection(mChatList.getCount() - 1);
次方法可以马上定位到最后一条 但是没有平滑的动作 很生硬
平滑移动方法一
@SuppressLint("NewApi")
public void DeterminedScrollTo(final ListView listView, final int index, int attempts ) {
if (listView.getFirstVisiblePosition() != index && attempts < 10) {
attempts++;
listView.smoothScrollToPositionFromTop (index, 1, 100);
final int att = attempts;
listView.postDelayed(new Runnable() {
@Override
public void run() {
DeterminedScrollTo (listView, index, att);
}
}, 100);
}
}
平滑移动方法二
@SuppressLint("NewApi")
public static void smoothScrollToPosition(final AbsListView view, final int position, final int offset) {
final View child = getChildAtPosition(view, position);
if ((child != null) && ((child.getTop() == offset) ||
((child.getTop() > offset) && !view.canScrollVertically(1))
|| ((child.getTop() < offset) && !view.canScrollVertically(-1)))) {
/
//Optional row animation to highlight the row when scrolling finishes
// Animation animation = new ScaleAnimation(1, 1, 0, 1, 0, child.getHeight()/2);
// animation.setDuration(400);
// child.startAnimation(animation);
/
return;
}
view.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
view.setOnScrollListener(null);
smoothScrollToPosition(view, position, offset);
}
}
@Override
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount,
final int totalItemCount) {
}
});
// Perform scrolling to position
new Handler().post(new Runnable() {
@Override
public void run() {
view.smoothScrollToPositionFromTop(position, offset);
}
});
}
public static View getChildAtPosition(final AdapterView view, final int position) {
final int index = position - view.getFirstVisiblePosition();
if ((index >= 0) && (index < view.getChildCount())) {
return view.getChildAt(index);
} else {
return null;
}
}
mChatList.smoothScrollToPosition(mChatList.getCount() - 1);
mChatList.smoothScrollToPositionFromTop(mChatListgetCount() - 1,0);
次两种方法 可以平滑移动 情况是item高度基本一致的情况
如果一个item很高 效果是没有的
mChatList.setSelection(mChatList.getCount() - 1);
次方法可以马上定位到最后一条 但是没有平滑的动作 很生硬
平滑移动方法一
@SuppressLint("NewApi")
public void DeterminedScrollTo(final ListView listView, final int index, int attempts ) {
if (listView.getFirstVisiblePosition() != index && attempts < 10) {
attempts++;
listView.smoothScrollToPositionFromTop (index, 1, 100);
final int att = attempts;
listView.postDelayed(new Runnable() {
@Override
public void run() {
DeterminedScrollTo (listView, index, att);
}
}, 100);
}
}
平滑移动方法二
@SuppressLint("NewApi")
public static void smoothScrollToPosition(final AbsListView view, final int position, final int offset) {
final View child = getChildAtPosition(view, position);
if ((child != null) && ((child.getTop() == offset) ||
((child.getTop() > offset) && !view.canScrollVertically(1))
|| ((child.getTop() < offset) && !view.canScrollVertically(-1)))) {
/
//Optional row animation to highlight the row when scrolling finishes
// Animation animation = new ScaleAnimation(1, 1, 0, 1, 0, child.getHeight()/2);
// animation.setDuration(400);
// child.startAnimation(animation);
/
return;
}
view.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
view.setOnScrollListener(null);
smoothScrollToPosition(view, position, offset);
}
}
@Override
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount,
final int totalItemCount) {
}
});
// Perform scrolling to position
new Handler().post(new Runnable() {
@Override
public void run() {
view.smoothScrollToPositionFromTop(position, offset);
}
});
}
public static View getChildAtPosition(final AdapterView view, final int position) {
final int index = position - view.getFirstVisiblePosition();
if ((index >= 0) && (index < view.getChildCount())) {
return view.getChildAt(index);
} else {
return null;
}
}