我想在布局的底部添加一个按钮。当我向上滚动时,在列表视图中向下,我希望按钮消失,反之亦然。就像在twitter应用程序中一样。在主页的底部有一个textView用于快速推文,当向上滚动时,视图变得不可见。这样做最简单的代码是什么?
答案 0 :(得分:1)
您可以使用http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html
并在每次滚动时检查listView.getChildAt(0).getTop()
位置。
如果最后一个顶部位置和当前顶部位置之间的差异为正,则向上滚动。之后,您可以设置按钮的可见性VISIBLE / INVISIBLE
答案 1 :(得分:1)
试试这个, 创建自定义滚动类,
public class CustomScrollView extends ScrollView {
private OnArrowChangeListener onArrowChangeListener = null;
private int id = 0;
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
id = getId();
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
id = getId();
}
public CustomScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
id = getId();
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
if (getChildCount() == 0) {
// no child
} else {
View view = (View) getChildAt(getChildCount() - 1);
if (view == null) {
super.onScrollChanged(l, t, oldl, oldt);
return;
}
int diff = (view.getBottom() - (getHeight() + getScrollY() + view.getTop()));
// Utility.log(VIEW_LOG_TAG, "@" + diff);
if (diff <= 10) {
// bottom reached
if (onArrowChangeListener != null) {
onArrowChangeListener.onReachedBottom(id);
}
} else if (t <= 10) {
// top reached
if (onArrowChangeListener != null) {
onArrowChangeListener.onReachedTop(id);
}
} else {
// middle
if (onArrowChangeListener != null) {
onArrowChangeListener.onVisibleBoth(id);
}
}
}
super.onScrollChanged(l, t, oldl, oldt);
// Utility.log(VIEW_LOG_TAG, "v ->" + t);
}
public OnArrowChangeListener getOnArrowChangeListener() {
return onArrowChangeListener;
}
public void setOnArrowChangeListener(OnArrowChangeListener onArrowChangeListener) {
this.onArrowChangeListener = onArrowChangeListener;
}
public void computeInitialScroll() {
CustomScrollView.this.post(new Runnable() {
@Override
public void run() {
if (CustomScrollView.this.getChildCount() > 0) {
View view = CustomScrollView.this.getChildAt(0);
if (view != null) {
int amount = CustomScrollView.this.getMaxScrollAmount();
int height = view.getBottom() - view.getTop();
if (height < amount || getHeight() > height) {
if (onArrowChangeListener != null) {
onArrowChangeListener.onInVisibleBoth(id);
}
}
}
}
}
});
}
}
把它放在你的XMl
中 <com.app.ui.CustomScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:paddingTop="10dp" >
在您的活动或片段上实现OnScrollListener,
private int pos = 0; listview.setSelection(POS);
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
pos = firstVisibleItem;
int first = firstVisibleItem;
int last = firstVisibleItem + visibleItemCount;
int adapterSize = totalItemCount;
if (visibleItemCount >= adapterSize) {
// Scroll center of items
return;
}
if (first > 0 && last < adapterSize) {
// middle
// both visible
btnUp.setVisibility(View.VISIBLE);
btnDown.setVisibility(View.VISIBLE);
return;
}
if (last == adapterSize) {
//Scroll to bottom
return;
}
if (first == 0) {
//Scroll to top
return;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}