一般的下拉刷新都只实现了刷新ListView,本demo实现了刷新普通Activity
RefreshScrollActivity.java:
package com.nono.refreshDemo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.Toast; /** * * @author Nono * */ public class RefreshScrollActivity extends Activity implements RefreshableView.RefreshListener { /** Called when the activity is first created. */ private RefreshableView mRefreshableView; private Context mContext; Handler handler = new Handler() { public void handleMessage(Message message) { super.handleMessage(message); mRefreshableView.finishRefresh(); Toast.makeText(mContext, R.string.toast_text, Toast.LENGTH_SHORT).show(); }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mContext = this; init(); } private void init() { // TODO Auto-generated method stub initView(); } private void initView() { // TODO Auto-generated method stub mRefreshableView = (RefreshableView) findViewById(R.id.refresh_root); initData(); } private void initData() { mRefreshableView.setRefreshListener(this); } //实现刷新RefreshListener 中方法 public void onRefresh(RefreshableView view) { //伪处理 handler.sendEmptyMessageDelayed(1, 2000); } }
RefreshableView.java:
package com.nono.refreshDemo; import java.util.Calendar; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.ScrollView; import android.widget.Scroller; import android.widget.TextView; /** * 刷新控制view * * @author Nono * */ public class RefreshableView extends LinearLayout { private static final String TAG = "LILITH"; private Scroller scroller; private View refreshView; private ImageView refreshIndicatorView; private int refreshTargetTop = -60; private ProgressBar bar; private TextView downTextView; private TextView timeTextView; private LinearLayout reFreshTimeLayout;//显示上次刷新时间的layout private RefreshListener refreshListener; private String downTextString; private String releaseTextString; // private Long refreshTime = null; private int lastX; private int lastY; // 拉动标记 private boolean isDragging = false; // 是否可刷新标记 private boolean isRefreshEnabled = true; // 在刷新中标记 private boolean isRefreshing = false; Calendar LastRefreshTime; private Context mContext; public RefreshableView(Context context) { super(context); mContext = context; } public RefreshableView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; init(); } private void init() { // TODO Auto-generated method stub //滑动对象, LastRefreshTime=Calendar.getInstance(); scroller = new Scroller(mContext); //刷新视图顶端的的view refreshView = LayoutInflater.from(mContext).inflate(R.layout.refresh_top_item, null); //指示器view refreshIndicatorView = (ImageView) refreshView.findViewById(R.id.indicator); //刷新bar bar = (ProgressBar) refreshView.findViewById(R.id.progress); //下拉显示text downTextView = (TextView) refreshView.findViewById(R.id.refresh_hint); //下来显示时间 timeTextView = (TextView) refreshView.findViewById(R.id.refresh_time); reFreshTimeLayout=(LinearLayout)refreshView.findViewById(R.id.refresh_time_layout); LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, -refreshTargetTop); lp.topMargin = refreshTargetTop; lp.gravity = Gravity.CENTER; addView(refreshView, lp); downTextString = mContext.getResources().getString(R.string.refresh_down_text); releaseTextString = mContext.getResources().getString(R.string.refresh_release_text); } /** * 设置上次刷新时间 * @param time */ private void setLastRefreshTimeText() { // TODO Auto-generated method stub reFreshTimeLayout.setVisibility(View.VISIBLE); Calendar NowTime=Calendar.getInstance(); long l=NowTime.getTimeInMillis()-LastRefreshTime.getTimeInMillis(); int days=new Long(l/(1000*60*60*24)).intValue(); int hour=new Long(l/(1000*60*60)).intValue(); int min=new Long(l/(1000*60)).intValue(); if(days!=0) { timeTextView.setText(days+"天"); } else if(hour!=0) { timeTextView.setText(hour+"小时"); } else if(min!=0) { timeTextView.setText(min+"分钟"); } //timeTextView.setText(time); } @Override public boolean onTouchEvent(MotionEvent event) { int y= (int) event.getRawY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //记录下y坐标 lastY = y; break; case MotionEvent.ACTION_MOVE: Log.i(TAG, "ACTION_MOVE"); //y移动坐标 int m = y - lastY; if(((m < 6) && (m > -1)) || (!isDragging )){ setLastRefreshTimeText(); doMovement(m); } //记录下此刻y坐标 this.lastY = y; break; case MotionEvent.ACTION_UP: Log.i(TAG, "ACTION_UP"); fling(); break; } return true; } /** * up事件处理 */ private void fling() { // TODO Auto-generated method stub LinearLayout.LayoutParams lp = (LayoutParams) refreshView.getLayoutParams(); Log.i(TAG, "fling()" + lp.topMargin); if(lp.topMargin > 0){//拉到了触发可刷新事件 refresh(); }else{ returnInitState(); } } private void returnInitState() { // TODO Auto-generated method stub LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)this.refreshView.getLayoutParams(); int i = lp.topMargin; scroller.startScroll(0, i, 0, refreshTargetTop); invalidate(); } private void refresh() { // TODO Auto-generated method stub LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)this.refreshView.getLayoutParams(); int i = lp.topMargin; reFreshTimeLayout.setVisibility(View.GONE); refreshIndicatorView.setVisibility(View.GONE); bar.setVisibility(View.VISIBLE); timeTextView.setVisibility(View.GONE); downTextView.setVisibility(View.GONE); scroller.startScroll(0, i, 0, 0-i); invalidate(); if(refreshListener !=null){ refreshListener.onRefresh(this); isRefreshing = true; } } /** * */ @Override public void computeScroll() { // TODO Auto-generated method stub if(scroller.computeScrollOffset()){ int i = this.scroller.getCurrY(); LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)this.refreshView.getLayoutParams(); int k = Math.max(i, refreshTargetTop); lp.topMargin = k; this.refreshView.setLayoutParams(lp); this.refreshView.invalidate(); invalidate(); } } /** * 下拉move事件处理 * @param moveY */ private void doMovement(int moveY) { // TODO Auto-generated method stub LinearLayout.LayoutParams lp = (LayoutParams) refreshView.getLayoutParams(); if(moveY>0){ //获取view的上边距 float f1 =lp.topMargin; float f2 = moveY * 0.3F; int i = (int)(f1+f2); //修改上边距 lp.topMargin = i; //修改后刷新 refreshView.setLayoutParams(lp); refreshView.invalidate(); invalidate(); } else { float f1 =lp.topMargin; int i=(int)(f1+moveY*0.9F); Log.i("aa", String.valueOf(i)); if(i>=refreshTargetTop) { lp.topMargin = i; //修改后刷新 refreshView.setLayoutParams(lp); refreshView.invalidate(); invalidate(); } else { } } timeTextView.setVisibility(View.VISIBLE); // if(refreshTime!= null){ // setRefreshTime(refreshTime); // } downTextView.setVisibility(View.VISIBLE); refreshIndicatorView.setVisibility(View.VISIBLE); bar.setVisibility(View.GONE); if(lp.topMargin > 0){ downTextView.setText(R.string.refresh_release_text); refreshIndicatorView.setImageResource(R.drawable.refresh_arrow_up); }else{ downTextView.setText(R.string.refresh_down_text); refreshIndicatorView.setImageResource(R.drawable.refresh_arrow_down); } } public void setRefreshEnabled(boolean b) { this.isRefreshEnabled = b; } public void setRefreshListener(RefreshListener listener) { this.refreshListener = listener; } /** * 结束刷新事件 */ public void finishRefresh(){ Log.i(TAG, "执行了=====finishRefresh"); LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams)this.refreshView.getLayoutParams(); int i = lp.topMargin; refreshIndicatorView.setVisibility(View.VISIBLE); timeTextView.setVisibility(View.VISIBLE); scroller.startScroll(0, i, 0, refreshTargetTop); invalidate(); isRefreshing = false; LastRefreshTime=Calendar.getInstance(); } /*该方法一般和ontouchEvent 一起用 * (non-Javadoc) * @see android.view.ViewGroup#onInterceptTouchEvent(android.view.MotionEvent) */ @Override public boolean onInterceptTouchEvent(MotionEvent e) { // TODO Auto-generated method stub int action = e.getAction(); int y= (int) e.getRawY(); switch (action) { case MotionEvent.ACTION_DOWN: lastY = y; break; case MotionEvent.ACTION_MOVE: //y移动坐标 int m = y - lastY; //记录下此刻y坐标 this.lastY = y; if(m > 6 && canScroll()){ return true; } break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_CANCEL: break; } return false; } private boolean canScroll() { // TODO Auto-generated method stub View childView; if(getChildCount()>1){ childView = this.getChildAt(1); if(childView instanceof ListView){ int top =((ListView)childView).getChildAt(0).getTop(); int pad =((ListView)childView).getListPaddingTop(); if((Math.abs(top-pad)) < 3&& ((ListView) childView).getFirstVisiblePosition() == 0){ return true; }else{ return false; } }else if(childView instanceof ScrollView){ if(((ScrollView)childView).getScrollY() == 0){ return true; }else{ return false; } } } return false; } /** * 刷新监听接口 * @author Nono * */ public interface RefreshListener{ public void onRefresh(RefreshableView view); } }
main.xml:
<?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:background="@drawable/app_bg" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:gravity="center" android:text="@string/hello" android:textColor="@color/black" /> <com.nono.refreshDemo.RefreshableView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/refresh_root" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroll_view_root" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/linear_root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第一行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第二行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第三行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第四行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第五行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第六行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第七行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第八行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第九行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第十行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第十行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第十行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第十行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第十行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第十行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第十行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@drawable/item_bg" android:gravity="center" android:text="第十行哈哈哈哈哈哈" android:textColor="@color/black" android:textSize="30sp" /> </LinearLayout> </ScrollView> </com.nono.refreshDemo.RefreshableView> </LinearLayout>
refresh_top_item.xml:
<?xml version="1.0" encoding="utf-8"?> <!-- android:layout_marginTop="-50.0dip"隐藏 --> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="50.0dip" android:layout_marginTop="-50.0dip" xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:src="@drawable/refresh_arrow_down" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <ProgressBar android:id="@+id/progress" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateBehavior="repeat" android:max="10000" android:visibility="gone" /> <TextView android:id="@+id/refresh_hint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5.0dip" android:gravity="center_vertical" android:text="松开手指刷新" android:textSize="14.0sp" /> </LinearLayout> <LinearLayout android:id="@+id/refresh_time_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="3dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="上次刷新时间:" android:textSize="10sp" /> <TextView android:id="@+id/refresh_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0分钟" android:textSize="10sp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="前" android:textSize="10sp" /> </LinearLayout> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="5dp" android:layout_alignParentBottom="true" android:background="#ffc8b67a" /> </RelativeLayout> </LinearLayout>
相关推荐
非常多的下拉刷新上拉加载更多的效果,支持各种View!包含源码供学习,导入时,注意依赖关系
android activity选项卡 listview 下拉刷新 仿新浪微博 提供现成demo
进一步优化了 ListView的下拉刷新 和更多 的自定义控件 大家如有需要请下载后详细看代码的注释 你只需要详细阅读Activity中的代码 那里有提供完整的范例使用 如果有需要对控件再扩张可以详细看全部代码。 代码只需...
集成各种刷新,包括listView activity fragment
(网上的很多下拉刷新上拉更多 都会和长按菜单事件冲突) 控件都封装好了 你们不用再费劲力气去写啥了 直接写好OnMore方法 和OnRefresh方法 就OK了 而且控件很灵活 可以让你有选择的使用上拉刷新 或者上拉更多 我有...
Android下拉刷新上拉加载源码 ....... @Override public void onRefresh() { mHandler.postDelayed(new Runnable() { @Override public void run() { start = ++refreshCnt; items.clear(); ...
在当下,列表组件不带下拉刷新的都不好意思...第一次完成列表的下拉刷新功能的时候,直接在Activity中实现,虽然功能上是实现了,总体上感觉很乱。所以第二次用到的时候,就想着封装成一个组件,实现和Activity的解耦。
主要介绍了Android程序开发之Listview下拉刷新上拉(滑动分页)加载更多的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
可能开发安卓的人大多数都用过很多下拉刷新的开源组件,但是今天用了官方v4支持包的SwipeRefreshLayout觉得效果也蛮不错的,特拿出来分享。 简介: SwipeRefreshLayout组件只接受一个子组件:即需要刷新的那个组件...
android 仿google play效果,ListView/GridView下拉刷新,上拉加载更多,自动加载异步请求数据以及分页数据 package net.xinhua.activity; import net.xinhuamm.widget.TouchViewPager; import android.os.Bundle; ...
Activity package com.example.refreshgridview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.GridView; import android....
介绍:一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的RecyclerView。使用方式和RecyclerView完全一致,不需要额外的layout,不需要写特殊的adater。 加载效果内置了AVLoadingIndicatorView上的所有...
Android SwipereFreshLayout下拉刷新 我们都知道现在android5.0以后就提倡使用Material Design设计了。在Material Design设计就有一个非常好的设计SwipereFreshLayout,下面我们就来看看它的使用。既然它来源于...
使用RecyclerView越来越多了,基本可以不用listview了,但是这个新的控件谷歌官方似乎设计的没有想listview那样方便快捷,listview自带有headview和 footerview所有我们很方便的可以实现上拉,下拉刷新,但是我们...
View的onInterceptTouchEvent(MotionEvent ev)和dispatchTouchEvent(MotionEvent ev) 解析
LauncherActivity(github完整项目ListView等下来刷新完整版)
史上最简单的上拉刷新下拉加载。整个工程就一个activity,一个自定义listview,还有三个布局文件。简单整洁易懂,适合新手。
下拉刷新页面天气数据 根据小伙伴的评论,我增加了页面数据的下拉刷新,首先在修改布局, 只要复制上面的刷新布局和样式布局即可 接下来在Activity中 修改的地方也比较简单,这里就不贴代码了。 上图中...
每一个activity都是一种布局效果,其中包括下拉刷新,上啦加载更多,actionBar,模仿手机联系人布局,自定义spinner布局的使用