如何添加向上滚动时消失(在列表视图中向下)并向下滚动时出现的视图?
How to add a view that disappears when scroll up (go down in listview) and appears when scroll down?
我想在布局底部添加一个按钮。当我向上滚动时,在列表视图中向下滚动时,我希望按钮消失,反之亦然。就像在推特应用程序中一样。主页底部有一个用于快速推文的 textView,向上滚动时视图变得不可见。最简单的代码是什么?
您可以使用http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html
并在每次滚动时检查 listView.getChildAt(0).getTop()
位置。
如果上一个顶部位置和当前顶部位置之间的差异为正,则您正在向上滚动。之后你可以设置按钮的可见性 VISIBLE/INVISIBLE
试试这个,
创建自定义卷轴 class,
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" >
在您的 activity 或片段上实施 OnScrollListener,
私人int pos = 0;
listview.setSelection(排名);
@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
}
我想在布局底部添加一个按钮。当我向上滚动时,在列表视图中向下滚动时,我希望按钮消失,反之亦然。就像在推特应用程序中一样。主页底部有一个用于快速推文的 textView,向上滚动时视图变得不可见。最简单的代码是什么?
您可以使用http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html
并在每次滚动时检查 listView.getChildAt(0).getTop()
位置。
如果上一个顶部位置和当前顶部位置之间的差异为正,则您正在向上滚动。之后你可以设置按钮的可见性 VISIBLE/INVISIBLE
试试这个, 创建自定义卷轴 class,
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" >
在您的 activity 或片段上实施 OnScrollListener,
私人int pos = 0; listview.setSelection(排名);
@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
}