ListView上加了个Header,如何滚动到最顶端
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
listview的一些方法:
1.public void setSelection (int position)
position位置的item置顶(即position位置的item到listview的最顶端),但是,没有从原来位置到顶端的滑动效果。
2.public void setSelectionFromTop (int position, int y)
与setSelection 类似。setSelection(position)就是setSelectionFromTop (position,0)
选中position指定的项目,并将所选项置于距离ListView顶端y像素的位置
参数
position 需要选中的项目的索引(从0开始)
y 距离ListView(包括间隙)顶端的位置
3.public void smoothScrollToPositionFromTop (int position, int offset)
平滑滚动到指定的适配器位置。 指定位置的视图会滚动到相对顶边偏移 offset 像素的位置显示。 如果无法做到(比如该偏移量会使首尾条目超越列表边缘),会滚动到尽量接近的位置。
参数
position 滚动到的位置
offset 滚动结束时,指定 position 条目距离视图顶部的像素数
当offset=0时,可以把position位置的item置顶,而且带有滑动效果,即会从原来位置轻轻滑动到最顶 端位置。
测试: 可以顺利滚动,返回滚动也很顺利,滑动顺畅
4、public void smoothScrollToPositionFromTop(int position)
参数:
position 滚动到的位置
带有滑动效果的滚动到指定位置。
如何判断其内容已滚动到最顶部或者最底部?
getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem==0){
Log.e("log", "滑到顶部");
}
if(visibleItemCount+firstVisibleItem==totalItemCount){
Log.e("log", "滑到底部");
}
}
});
平滑的滚动listview到一个指定位
package com.android.contacts.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class AutoScrollListView extends ListView {
private static final float PREFERRED_SELECTION_OFFSET_FROM_TOP = 0.33f;
private int mRequestedScrollPosition = -1;
private boolean mSmoothScrollRequested;
public AutoScrollListView(Context context) {
super(context);
}
public AutoScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void requestPositionToScreen(int position, boolean smoothScroll) {
mRequestedScrollPosition = position;
mSmoothScrollRequested = smoothScroll;
requestLayout();
}
@Override
protected void layoutChildren() {
super.layoutChildren();
if (mRequestedScrollPosition == -1) {
return;
}
final int position = mRequestedScrollPosition;
mRequestedScrollPosition = -1;
int firstPosition = getFirstVisiblePosition() + 1;
int lastPosition = getLastVisiblePosition();
if (position >= firstPosition && position <= lastPosition) {
return; // Already on screen
}
final int offset = (int) (getHeight() * PREFERRED_SELECTION_OFFSET_FROM_TOP);
if (!mSmoothScrollRequested) {
setSelectionFromTop(position, offset);
// Since we have changed the scrolling position, we need to redo child layout
// Calling "requestLayout" in the middle of a layout pass has no effect,
// so we call layoutChildren explicitly
super.layoutChildren();
} else {
// We will first position the list a couple of screens before or after
// the new selection and then scroll smoothly to it.
int twoScreens = (lastPosition - firstPosition) * 2;
int preliminaryPosition;
if (position < firstPosition) { preliminaryPosition = position + twoScreens; if (preliminaryPosition >= getCount()) {
preliminaryPosition = getCount() - 1;
}
if (preliminaryPosition < firstPosition) {
setSelection(preliminaryPosition);
super.layoutChildren();
}
} else {
preliminaryPosition = position - twoScreens;
if (preliminaryPosition < 0) { preliminaryPosition = 0; } if (preliminaryPosition > lastPosition) {
setSelection(preliminaryPosition);
super.layoutChildren();
}
}
smoothScrollToPositionFromTop(position, offset);
}
}
}
【T,SDM】
1.public void setSelection (int position)
position位置的item置顶(即position位置的item到listview的最顶端),但是,没有从原来位置到顶端的滑动效果。
2.public void setSelectionFromTop (int position, int y)
与setSelection 类似。setSelection(position)就是setSelectionFromTop (position,0)
选中position指定的项目,并将所选项置于距离ListView顶端y像素的位置
参数
position 需要选中的项目的索引(从0开始)
y 距离ListView(包括间隙)顶端的位置
3.public void smoothScrollToPositionFromTop (int position, int offset)
平滑滚动到指定的适配器位置。 指定位置的视图会滚动到相对顶边偏移 offset 像素的位置显示。 如果无法做到(比如该偏移量会使首尾条目超越列表边缘),会滚动到尽量接近的位置。
参数
position 滚动到的位置
offset 滚动结束时,指定 position 条目距离视图顶部的像素数
当offset=0时,可以把position位置的item置顶,而且带有滑动效果,即会从原来位置轻轻滑动到最顶 端位置。
测试: 可以顺利滚动,返回滚动也很顺利,滑动顺畅
4、public void smoothScrollToPositionFromTop(int position)
参数:
position 滚动到的位置
带有滑动效果的滚动到指定位置。
如何判断其内容已滚动到最顶部或者最底部?
getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem==0){
Log.e("log", "滑到顶部");
}
if(visibleItemCount+firstVisibleItem==totalItemCount){
Log.e("log", "滑到底部");
}
}
});
平滑的滚动listview到一个指定位
package com.android.contacts.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class AutoScrollListView extends ListView {
private static final float PREFERRED_SELECTION_OFFSET_FROM_TOP = 0.33f;
private int mRequestedScrollPosition = -1;
private boolean mSmoothScrollRequested;
public AutoScrollListView(Context context) {
super(context);
}
public AutoScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void requestPositionToScreen(int position, boolean smoothScroll) {
mRequestedScrollPosition = position;
mSmoothScrollRequested = smoothScroll;
requestLayout();
}
@Override
protected void layoutChildren() {
super.layoutChildren();
if (mRequestedScrollPosition == -1) {
return;
}
final int position = mRequestedScrollPosition;
mRequestedScrollPosition = -1;
int firstPosition = getFirstVisiblePosition() + 1;
int lastPosition = getLastVisiblePosition();
if (position >= firstPosition && position <= lastPosition) {
return; // Already on screen
}
final int offset = (int) (getHeight() * PREFERRED_SELECTION_OFFSET_FROM_TOP);
if (!mSmoothScrollRequested) {
setSelectionFromTop(position, offset);
// Since we have changed the scrolling position, we need to redo child layout
// Calling "requestLayout" in the middle of a layout pass has no effect,
// so we call layoutChildren explicitly
super.layoutChildren();
} else {
// We will first position the list a couple of screens before or after
// the new selection and then scroll smoothly to it.
int twoScreens = (lastPosition - firstPosition) * 2;
int preliminaryPosition;
if (position < firstPosition) { preliminaryPosition = position + twoScreens; if (preliminaryPosition >= getCount()) {
preliminaryPosition = getCount() - 1;
}
if (preliminaryPosition < firstPosition) {
setSelection(preliminaryPosition);
super.layoutChildren();
}
} else {
preliminaryPosition = position - twoScreens;
if (preliminaryPosition < 0) { preliminaryPosition = 0; } if (preliminaryPosition > lastPosition) {
setSelection(preliminaryPosition);
super.layoutChildren();
}
}
smoothScrollToPositionFromTop(position, offset);
}
}
}
【T,SDM】
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询