1. android 的ListView中怎麼判斷其內容已滾動到最頂部或者最底部
可以通過判斷流動條的位置得到;
如果在頂部,第零個item必然可見;
如果在底部,最大個數減1 item必須可見;
,具體見代碼:
java">listview.setOnScrollListener(newAbsListView.OnScrollListener(){@Override
(AbsListViewview,intscrollState){
switch(scrollState){
//當不滾動時
caseAbsListView.OnScrollListener.SCROLL_STATE_IDLE:
//判斷滾動到底部
if(listview.getLastVisiblePosition()==(listview.getCount()-1)){
L.e("滾動到底部");
}
//判斷滾動到頂部
if(listview.getFirstVisiblePosition()==0){
}
break;
}
}
@Override
publicvoidonScroll(AbsListViewview,intfirstVisibleItem,intvisibleItemCount,inttotalItemCount){
}
});
2. android listview如何按時加滾動到頂部
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);
}
}
}