ScrollView上拉刷新的小例子

本文介绍了一种解决Android中ScrollView嵌套ListView时出现滚动冲突的方法。通过自定义工具类调整ListView的高度,并监听ScrollView的触摸事件来动态更新数据,确保了良好的滚动体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码下载地址: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;
			}
		});
	}
	    
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值