深入理解ScrollView
来源:互联网 发布:手机网络直播 编辑:程序博客网 时间:2024/06/06 04:58
在网上看到一些网页在实现分页加载的时候,能够自动判断是否到达底部,然后自动加载下一页的内容,开始用的是ListView实现的,但是这个效果并不是很好,因为图片比较多出现了有些卡卡的现象,用了另外一种方式去实现了主要是为了对比一下速度的问题,找了很多最后发现可以使用ScrollView,查了很多ScrollView的文档但是没有多少能够使用到的的东西,可能也是水平有限吧,没有仔细的深入看源码,在捕获Y值得时候出现了问题。对于判断是否停止和是否到达底部的问题,网上的都是使用每个几十个毫秒去判断是否停止(网上可以搜到),通过两次捕获然后对比是否到达了底部。再接着去加载下一页的内容,这种方式会出现的问题就是用户的体验不太好,只有滑动了两次之后才能够加载下一页的内容。后来找到了一种方法可以一次判断是否到达底部。下面我把两种实现方式全部都列出来,主要的一个关键点就是写了一个类继承了scrollView,在这个类中有一个监听scrollView是否变化的方法,在这个监听方法中调用了computeVerticalScrollRange方法。
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:orientation="vertical" android:paddingBottom="4dip" android:layout_width="fill_parent"> <ImageView android:layout_height="wrap_content" android:id="@+id/ItemImage" android:layout_width="wrap_content" android:layout_centerHorizontal="true"> </ImageView> <TextView android:layout_width="wrap_content" android:layout_below="@+id/ItemImage" android:layout_height="wrap_content" android:text="TextView01" android:layout_centerHorizontal="true" android:id="@+id/ItemText"> </TextView> </LinearLayout>
item.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:orientation="vertical" android:paddingBottom="4dip" android:layout_width="fill_parent"> <ImageView android:layout_height="wrap_content" android:id="@+id/ItemImage" android:layout_width="wrap_content" android:layout_centerHorizontal="true"> </ImageView> <TextView android:layout_width="wrap_content" android:layout_below="@+id/ItemImage" android:layout_height="wrap_content" android:text="TextView01" android:layout_centerHorizontal="true" android:id="@+id/ItemText"> </TextView> </LinearLayout>
java代码public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); }private void init() {LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);for (int i = 0; i < 10; i++) {LinearLayout linearLayoutItem = (LinearLayout) getLayoutInflater().inflate(R.layout.item, null);TextView textView = (TextView) linearLayoutItem.findViewById(R.id.ItemText);textView.setText("垂直滚动");ImageView imageView = (ImageView) linearLayoutItem.findViewById(R.id.ItemImage);imageView.setImageResource(R.drawable.item1);linearLayout.addView(linearLayoutItem);}}}
上边的是一个有关scrollView的小例子
下边一个才是关键的哦
java代码
public class MySeccondListViewActivity extends Activity {LinearLayout linearLayout;TestScrollView scrollView;Button button;LinearLayout linearLayoutShowMore; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.category_project_list);linearLayout = (LinearLayout) findViewById(R.id.linearlayout);scrollView = (com.damai.mobile.TestScrollView) findViewById(R.id.scrollView);System.out.println("first scrollView= " + scrollView.getScrollY());init();scrollView.onS(new TestOnScrollChanged(){@Overridepublic void down() {super.down();linearLayoutShowMore.setVisibility(View.INVISIBLE);init(); }@Overridepublic void up() {super.up();Toast.makeText(MySeccondListViewActivity.this, "到达顶部了",Toast.LENGTH_SHORT).show();}});scrollView.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { /** 滑动到顶部和底部做处理 **/ scrollView.istouch=true; } return false; } }); }private void init() {for (int i = 0; i < 5; i++) {final int postion = i ;LinearLayout linearLayout2 = (LinearLayout) getLayoutInflater().inflate(R.layout.item, null);LinearLayout linearLayoutRight = (LinearLayout) linearLayout2.findViewById(R.id.linearlayoutRight);LinearLayout linearLayoutLeft = (LinearLayout) linearLayout2.findViewById(R.id.linearlayoutLeft);TextView textView1 = (TextView)linearLayout2.findViewById(R.id.ItemTextLeft);ImageView imageView1 = (ImageView)linearLayout2.findViewById(R.id.ItemImageLeft);imageView1.setImageResource(R.drawable.item1);textView1.setText("第二列");TextView textView = (TextView) linearLayout2.findViewById(R.id.ItemText);ImageView imageView = (ImageView)linearLayout2.findViewById(R.id.ItemImage);textView.setText("布局方式");imageView.setImageResource(R.drawable.item2);linearLayout.addView(linearLayout2);linearLayoutRight.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsetTitle("您选择了Left第" + postion + "个");}});linearLayoutLeft.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsetTitle("您选择了Right第" + postion + "个");}});}linearLayoutShowMore = (LinearLayout) getLayoutInflater().inflate(R.layout.show_more_info, null);linearLayout.addView(linearLayoutShowMore);}}继承自scrollView的类package com.damai.mobile;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.util.AttributeSet;import android.view.KeyEvent;import android.widget.ScrollView;public class TestScrollView extends ScrollView {public TestScrollView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public TestScrollView(Context context, AttributeSet attrs) {super(context, attrs);}public TestScrollView(Context context) {super(context);}TestOnScrollChanged _t = null;public void onS(TestOnScrollChanged t) {_t = t;}// 是否正在移动boolean istouch = false;@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {// TODO Auto-generated method stubsuper.onScrollChanged(l, t, oldl, oldt);if (istouch) {if (this.getScrollY() + this.getHeight() >= computeVerticalScrollRange()) {istouch = false;_t.down();}if (t == 0) {istouch = false;_t.up();}}}}
Demo下载地址http://download.csdn.net/detail/walker02/4122519
- 深入理解ScrollView
- Iphone开发(十四)深入理解scrollView的contentOffset等属性,实现下拉刷新功能
- Iphone开发(十四)深入理解scrollView的contentOffset等属性,实现下拉刷新功能
- Iphone开发(十四)深入理解scrollView的contentOffset等属性,实现下拉刷新功能
- UIScrollView scrollView.contentInset属性深入
- 深入理解
- 深入理解布局容器绘制,解决ListView嵌套listview,或者ScrollView嵌套listview,gridview的高度问题解决方法
- 深入理解scrollView的contentOffset等属性转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/deta
- MapReduce理解-深入理解MapReduce
- 有关ScrollView的contentOffset的理解
- android ScrollView smoothScrollTo源码的一点理解
- 深入理解动态库
- 深入理解sizeof
- 深入理解MIDlet类
- 深入理解私有成员
- 深入理解动态库
- TCP协议深入理解
- TCP协议深入理解
- Android应用自动添加桌面图标
- 什么叫显示动力学,什么叫隐式动力学分析!
- 基于mini2440的ov9650摄像头裸机测试
- 非禁用validateRequest=false使用Page_Error()错误处理
- 二栈共享空间静态储存简例
- 深入理解ScrollView
- ArcGIS版本压缩(Compress)报ORA-00001: unique constraint 的解决方法
- 内核同步
- 自动生成Makefile的全过程详解
- 内核寻址
- 查看 SQL Server 连接
- 几个有意思的C语言小函数
- 进程
- .NET平台开源JSON库LitJSON的使用方法