- 布局文件
<RelativeLayout
android:id="@+id/ll_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_bottom"
android:layout_below="@id/ll_top" >
<GridView
android:id="@+id/gv_classify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eee"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:verticalSpacing="10dp"
android:visibility="visible" >
</GridView>
<ListView
android:id="@+id/lv0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
</ListView>
<ImageButton
android:id="@+id/btn_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:background="@null"
android:src="@drawable/top"
android:visibility="gone" />
</RelativeLayout>
- 主要代码
/* 回到顶端 */
btn_top.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
lv.setSelection(0);
btn_top.setVisibility(View.GONE);
}
});
/* listview滚动监听事件 */
lv.setOnScrollListener(new OnScrollListener() {
/**
* 滚动状态改变时调用
*/
@SuppressLint("NewApi")
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 不滚动时保存当前滚动到的位置
if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
if (lv.getFirstVisiblePosition() < 10) {//获取可见的在最顶端的item
btn_top.setVisibility(View.GONE);
} else {
btn_top.setVisibility(View.VISIBLE);
}
}
}
/**
* 滚动时调用
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
https://www.xuebuyuan.com/613381.html
posted @ 2014-07-24 17:28