package com.example.refreshscrollview; import java.util.Date; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.ScrollView; import android.widget.TextView; public class RefreshScrollView extends ScrollView { private final static int RATIO = 3; /** * ScrollView里面的布局 */ private LinearLayout innerLayout; private LinearLayout refreshLayout; private ImageView ivArrow; private ProgressBar pbRefresh; private TextView tvPullDownTips; private TextView tvLastUpdate; private int headContentWidth; private int headContentHeight; private RotateAnimation animation; // 下拉动画 private RotateAnimation reverseAnimation; // 反向动画 private enum RefreshState { PULL_DOWN, RELEASE_TO_REFRESH, REFRESHING, DONE }; // 松手,下拉,正在刷新,刷新完成 private RefreshState state; private boolean isBack; // 下拉到一定距离后,箭头是否转向 private boolean isRecored; // 是否记录了滑动时的起始位置 private int startY; private OnRefreshListener refreshListener = null; public RefreshScrollView(Context context) { super(context); init(context); } public RefreshScrollView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { LayoutInflater inflater = LayoutInflater.from(context); innerLayout = new LinearLayout(context); innerLayout.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); innerLayout.setOrientation(LinearLayout.VERTICAL); refreshLayout = (LinearLayout) inflater.inflate( R.layout.refresh_widget_layout, null); ivArrow = (ImageView) refreshLayout.findViewById(R.id.iv_arrow); pbRefresh = (ProgressBar) refreshLayout.findViewById(R.id.pb_refresh); tvPullDownTips = (TextView) refreshLayout .findViewById(R.id.tv_pulldown_tips); tvLastUpdate = (TextView) refreshLayout .findViewById(R.id.tv_lastupdate); measureView(refreshLayout); headContentHeight = refreshLayout.getMeasuredHeight(); headContentWidth = refreshLayout.getMeasuredWidth(); refreshLayout.setPadding(0, -1 * headContentHeight, 0, 0); refreshLayout.invalidate(); innerLayout.addView(refreshLayout); addView(innerLayout); animation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); animation.setInterpolator(new LinearInterpolator()); animation.setDuration(250); animation.setFillAfter(true); reverseAnimation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); reverseAnimation.setInterpolator(new LinearInterpolator()); reverseAnimation.setDuration(200); reverseAnimation.setFillAfter(true); state = RefreshState.DONE; isRecored = false; } @Override public boolean onTouchEvent(MotionEvent event) { if (refreshListener == null) { return super.onTouchEvent(event); } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (getScrollY() == 0 && !isRecored) { isRecored = true; startY = (int) event.getY(); // 记录起始位置 } break; case MotionEvent.ACTION_UP: // 移动一段距离后,如果是“下拉不完全”,那么改为“结束刷新”,如果是“下拉完全”,那么改为“正在刷新”,进入刷新 if (state == RefreshState.PULL_DOWN) { state = RefreshState.DONE; changeHeaderViewByState(); } if (state == RefreshState.RELEASE_TO_REFRESH) { state = RefreshState.REFRESHING; changeHeaderViewByState(); onRefresh(); } isRecored = false; isBack = false; break; case MotionEvent.ACTION_MOVE: int tempY = (int) event.getY(); // 移动过程中Y的坐标 if (!isRecored && getScrollY() == 0) { isRecored = true; startY = tempY; } if (state == RefreshState.REFRESHING || !isRecored) { break; } // 滑动时状态为“下拉完全”,它可能滑动时进入“下拉不完全”或者向下滑动滚动条 if (state == RefreshState.RELEASE_TO_REFRESH) { if (((tempY - startY) / RATIO < headContentHeight) && (tempY - startY) > 0) { state = RefreshState.PULL_DOWN; changeHeaderViewByState(); } if (tempY - startY <= 0) { state = RefreshState.DONE; changeHeaderViewByState(); } } // "下拉不完成"状态可以转变成“下拉完全”或者滑动滚动条 if (state == RefreshState.PULL_DOWN) { if ((tempY - startY) / RATIO >= headContentHeight) { state = RefreshState.RELEASE_TO_REFRESH; isBack = true; changeHeaderViewByState(); } else if (tempY - startY <= 0) { state = RefreshState.DONE; changeHeaderViewByState(); } } // 无下拉状态可以进入“下拉不完全状态” (滑动滚动条也是无下拉状态) if (state == RefreshState.DONE) { if (tempY - startY > 0) { state = RefreshState.PULL_DOWN; changeHeaderViewByState(); } } // 下拉过程中,设置整个刷新控件的位置;在这两个下拉状态中,事件不会交给其它的view if (state == RefreshState.PULL_DOWN || state == RefreshState.RELEASE_TO_REFRESH) { refreshLayout.setPadding(0, -1 * headContentHeight + (tempY - startY) / RATIO, 0, 0); return true; } break; } return super.onTouchEvent(event); } private void changeHeaderViewByState() { switch (state) { case PULL_DOWN: // 下拉刷新,还没有拉到界限,这时候松手不会刷新 pbRefresh.setVisibility(View.GONE); tvPullDownTips.setVisibility(View.VISIBLE); ivArrow.clearAnimation(); ivArrow.setVisibility(View.VISIBLE); if (isBack) { isBack = false; ivArrow.clearAnimation(); ivArrow.startAnimation(reverseAnimation); tvPullDownTips.setText("下拉刷新"); } else { tvPullDownTips.setText("下拉刷新"); } break; case RELEASE_TO_REFRESH: // 松手刷新,已经拉到界限,这时候松手会刷新 ivArrow.setVisibility(View.VISIBLE); pbRefresh.setVisibility(View.GONE); tvPullDownTips.setVisibility(View.VISIBLE); tvLastUpdate.setVisibility(View.VISIBLE); ivArrow.clearAnimation(); ivArrow.startAnimation(animation); tvPullDownTips.setText("松手刷新"); break; case REFRESHING: // 正在刷新时,显示刷新进度圈,清除箭头 refreshLayout.setPadding(0, 0, 0, 0); tvPullDownTips.setText("正在刷新..."); pbRefresh.setVisibility(View.VISIBLE); ivArrow.clearAnimation(); ivArrow.setVisibility(View.GONE); tvLastUpdate.setVisibility(View.VISIBLE); isRecored = false; break; case DONE: // 刷新完成,隐藏整个刷新控件,刷新进度圈消失 refreshLayout.setPadding(0, -1 * headContentHeight, 0, 0); // 隐藏空间 pbRefresh.setVisibility(View.GONE); ivArrow.clearAnimation(); ivArrow.setImageResource(R.drawable.arrow); tvPullDownTips.setText("刷新完成"); tvLastUpdate.setVisibility(View.VISIBLE); isRecored = false; break; default: break; } } private void measureView(View child) { ViewGroup.LayoutParams layoutParams = child.getLayoutParams(); if (layoutParams == null) { layoutParams = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); } int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0, layoutParams.width); int lpHeight = layoutParams.height; int childHeightSpec; if (lpHeight > 0) { childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } child.measure(childWidthSpec, childHeightSpec); } public void setonRefreshListener(OnRefreshListener refreshListener) { this.refreshListener = refreshListener; } public interface OnRefreshListener { public void onRefresh(); } public void onRefreshComplete() { state = RefreshState.DONE; tvLastUpdate.setText("最新一次更新时间:" + new Date().toLocaleString()); changeHeaderViewByState(); invalidate(); scrollTo(0, 0); } private void onRefresh() { if (refreshListener != null) { refreshListener.onRefresh(); } } public void addChild(View child) { innerLayout.addView(child); } public void addChild(View child, int position) { innerLayout.addView(child, position); } }
package com.example.refreshscrollview; import java.util.Random; import com.example.refreshscrollview.RefreshScrollView.OnRefreshListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { private RefreshScrollView scrollView; private TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scrollView = (RefreshScrollView)findViewById(R.id.scrollview1); View contentview=View.inflate(this,R.layout.content,null); tv=(TextView) contentview.findViewById(R.id.tv); scrollView.addChild(contentview); final Handler handler = new Handler() { public void handleMessage(Message message) { String str = (String)message.obj; tv.setText(str); scrollView.onRefreshComplete(); } }; scrollView.setonRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { Thread thread = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } Message message = handler.obtainMessage(0, ""+new Random().nextInt(100)); handler.sendMessage(message); } }); thread.start(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.example.refreshscrollview.RefreshScrollView android:id="@+id/scrollview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#555555" > </com.example.refreshscrollview.RefreshScrollView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:text="1" > </TextView> </LinearLayout>
相关推荐
然而,ScrollView并不直接支持下拉刷新功能,这是现代移动应用中常见的交互模式,特别是用于加载更多数据或更新内容。因此,为了实现"android scrollview 下拉刷新",开发者通常需要结合其他组件和技巧。 首先,...
"ScrollView实现下拉刷新"这个主题聚焦于如何在滚动视图中添加一个下拉刷新功能,这通常用于列表或者网格视图,使得用户可以更新内容而无需离开当前页面。这种特性在许多应用程序中非常常见,比如社交媒体应用和新闻...
SwipeRefreshLayout作为父布局包裹ListView、GridView或ScrollView,提供下拉刷新的功能。当用户在顶部向下拖动时,SwipeRefreshLayout会显示一个刷新指示器,并触发刷新回调。开发者需要在这个回调中执行更新数据的...
当我们谈论"scrollview下拉刷新"时,我们通常是指在ScrollView顶部添加一个可下拉的刷新头部,以便在用户拉动时更新内容。这种功能常见于新闻应用、社交媒体应用等,用于获取最新数据。 首先,实现scrollview的下拉...
当涉及到下拉刷新功能时,`ScrollView`提供了`refreshControl`属性来实现这一功能。在本文中,我们将探讨如何在React Native中使用`ScrollView`的`refreshControl`属性实现下拉刷新效果。 首先,我们需要创建一个...
"ScrollView实现下拉刷新"这个主题主要关注如何在ScrollView中添加一个自定义视图来实现下拉刷新的功能。这个功能常见于许多应用的顶部,当用户下拉时,会触发数据的更新或加载更多内容。 首先,我们来详细了解一下...
然而,ScrollView本身并不支持下拉刷新(Pull-to-Refresh)功能,这是现代移动应用中常见的交互设计,特别是对于列表或者滚动内容而言。下拉刷新通常用于更新数据,例如加载新的消息或内容。为了实现这个功能,...
而下拉刷新和上拉加载更多的功能则为用户提供了更友好的交互体验,使得用户可以轻松获取更多内容,尤其是在数据流无限或者数量庞大的情况下。本文将详细讲解如何在这些控件中实现这两种功能。 一、下拉刷新 1. ...
定义PullToRefreshView,只要把GirdView ListView ScrollView放到自定义的PullToRefreshView下就可以实现下拉刷新 下拉刷新
本文将详细探讨如何在Android中创建带有下拉刷新和上拉加载功能的ScrollView。 一、基础概念 1. ScrollView:Android系统提供的一个可滚动视图,可以包含一个直接子View,并确保该子View能够滚动以显示所有内容。 2...
通过以上介绍,我们可以看到,尽管ListView、GridView和ScrollView在实现下拉刷新和上拉加载更多时有一些共通之处,但具体实现细节会因组件特性和需求不同而有所差异。理解这些组件的工作原理并掌握相应的库和技巧,...
这个“Android下拉刷新控件(ListView好ScrollView版)”压缩包文件包含的是关于如何在Android中实现这种功能的具体资料,主要集中在ListView和ScrollView这两种常见的滚动视图上。 1. **下拉刷新概念** 下拉刷新...
本教程将介绍如何通过继承ScrollView来实现一个简单的下拉刷新功能,适用于初学者理解基本原理和动手实践。 首先,我们来看"PullToRefresh"这个文件,它是实现下拉刷新的核心部分。在Android中,下拉刷新通常涉及到...
为了实现ScrollView的上拉下拉刷新,开发者通常需要自定义一个刷新头部视图,并监听ScrollView的滑动事件。当用户上拉到ScrollView的底部时,显示刷新头部并加载新数据。缓冲效果可以通过预加载一定量的数据来实现,...
在Android开发中,上拉加载和下拉刷新是常见的组件功能,用于提升用户体验,使得用户在滚动列表到顶部时能够方便地获取更多数据,而在滚动到底部时加载更多内容。本示例“Android自定义上拉加载下拉刷新控件”提供了...
本Demo的核心是将两者结合,并利用EGO(Ego Refresh Table Header View)框架来实现下拉刷新的功能。 首先,EGO是一个开源的iOS库,它提供了下拉刷新和上拉加载更多的功能。它的设计目标是使得任何继承自...
在许多应用中,为了提供更好的用户体验,我们常常需要在ScrollView中实现下拉刷新的功能,这样用户可以轻松获取最新的数据而无需离开当前界面。本篇文章将深入探讨如何在ScrollView中实现下拉刷新。 首先,我们需要...