代码下载地址:http://download.youkuaiyun.com/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;
@Override
protected 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() {
@Override
public 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;
}
});
}
}
ScrollView上拉刷新的小例子
最新推荐文章于 2024-08-22 11:53:20 发布