ScrollView上拉刷新的小例子

来源:互联网 发布:国家干部网络培训 编辑:程序博客网 时间:2024/05/02 05:05
代码下载地址:http://download.csdn.net/detail/u011057161/7299693代码献上//布局文件<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".ScrollTest" >    <ScrollView         android:id="@+id/scroll"        android:layout_height="fill_parent"        android:layout_width="fill_parent">                <LinearLayout             android:layout_height="fill_parent"            android:layout_width="fill_parent"            android:orientation="vertical">                        <ListView                android:id="@+id/listview"                android:layout_height="fill_parent"                android:layout_width="fill_parent"                android:cacheColorHint="#ffffff" />        </LinearLayout>            </ScrollView></RelativeLayout>//工具类,避免scrollview嵌套ListView出现问题       public static void setLvHeight(ListView list) {ListAdapter adapter = list.getAdapter();if (adapter == null) {return;}int totalHeight = 0;for (int i = 0; i < adapter.getCount(); i++) {View itemView = adapter.getView(i, null, list);itemView.measure(0, 0);totalHeight += itemView.getMeasuredHeight();}ViewGroup.LayoutParams layoutParams = list.getLayoutParams();layoutParams.height = totalHeight+ (list.getDividerHeight() * (adapter.getCount() - 1));// 总行高+每行的间距list.setLayoutParams(layoutParams);}//主要类package com.example.scroll;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.ScrollView;public class ScrollTest extends Activity {     private ScrollView scrollView;     private ListView listView;     private String[] strings={"hello","hello","hello","hello",     "hello","hello","hello","hello","hello","hello","hello", "hello","hello","hello","hello","hello","hello","hello",};     private int i=0;     @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_scroll_test);scrollView=(ScrollView)this.findViewById(R.id.scroll);listView=(ListView)this.findViewById(R.id.listview);ArrayAdapter<String> adapter=new ArrayAdapter<String>(ScrollTest.this,android.R.layout.simple_list_item_1, strings);listView.setAdapter(adapter);UIHelper.setLvHeight(listView);//重新計算ListView的高度,否則只顯示lisview一行scrollView.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_UP) {View view = ((ScrollView) v).getChildAt(0);Log.e("getMeasuredHeight()------->", view.getMeasuredHeight()+"");Log.e("getScrollY()------->", v.getScrollY()+"");Log.e("getHeight()------->", v.getHeight()+"");//v.getHeight()可看見的控件高度//v.getScrollY()在y軸方向的偏移量//整個控件的高度(包括不可見的如ScrollView)if (view.getMeasuredHeight() <= v.getScrollY()+ v.getHeight()) {               ++i;               Log.i("i------->", i+"");               for (int i = 0; i <10; i++) {    }}}return false;}});}    }

                                             
0 0
原创粉丝点击