Android —页面下拉刷新(ListView与SwipeRefreshLayout)
2015-09-25 22:36
477 查看
相信大家在使用淘宝的时候应该都看到过下拉刷新的效果。这种效果看起来会感觉比较难做,一起来看下下拉刷新。
![](https://img-blog.csdn.net/20151007191529997)
思路:
通过FrameLayout底层为下拉刷新的Header,上面一层是ListView,监听手指滑动设置动画效果,移动ListView。
使用:
1、编写ListView顶部下拉刷新的header
2、ListView(后面将会进行动态添加)
3、自定义FrameLayout动态添加ListView与header
4、布局引用
5、在MainActivity中使用该布局即可。
![](https://img-blog.csdn.net/20151007173608798)
实际上Android已经为我们实现一种下拉刷新,那就是通过SwipeRefreshLayout,看下新出的SwipeRefreshLayout的实现方式。
使用:
1、布局
注:SwipeRefreshLayout类似于ScrollView,内部只能有一个组件。
2、MainActivity
ListView
思路:
通过FrameLayout底层为下拉刷新的Header,上面一层是ListView,监听手指滑动设置动画效果,移动ListView。
使用:
1、编写ListView顶部下拉刷新的header
<?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:layout_width="match_parent" android:gravity="center" android:layout_height="wrap_content" android:text="下拉刷新" /> </LinearLayout>
2、ListView(后面将会进行动态添加)
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"> </ListView>
3、自定义FrameLayout动态添加ListView与header
public class MyFrameLayout extends FrameLayout{ ListView listview; public MyFrameLayout(Context context) { super(context); } public MyFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); //先创建LayoutInflater 用于获得布局 LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //添加顶部布局 View header=inflater.inflate(R.layout.header, null); //向布局中添加header(下拉加载) addView(header); //ListView的Adapter ArrayAdapter<String> adapter=new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,new String[]{"a","b","c","d","e","f","g","h","i","j","k","l","m","n"}); listview=(ListView) inflater.inflate(R.layout.mylistview, null); listview.setAdapter(adapter); //向布局中添加ListView addView(listview); } //拦截事件 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if(ev.getAction()==MotionEvent.ACTION_DOWN){ } if(listview.getFirstVisiblePosition()==0){ View firstview=listview.getChildAt(listview.getFirstVisiblePosition()); if(firstview.getY()>=0){ return true; } } return super.onInterceptTouchEvent(ev); } //设置动画时使用 private float oldy; //处理事件 @Override public boolean onTouchEvent(MotionEvent event) { Log.d("onTouchEvent", ""+super.onTouchEvent(event)); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //手指落下监听 oldy=event.getY(); case MotionEvent.ACTION_MOVE: float y=event.getY(); float distance=y-oldy; //设置listview移动 listview.setTranslationY(listview.getTranslationY()+distance); oldy=y; invalidate(); return true; case MotionEvent.ACTION_UP: //手指抬起时,设置动画效果,使ListView回到顶部 ObjectAnimator.ofFloat(listview, "translationY", listview.getTranslationY(),0).setDuration(300).start(); break; default: break; } return super.onTouchEvent(event); } }
4、布局引用
<?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" > <com.example.administrator.myapplication2.MyFrameLayout android:layout_width="match_parent" android:layout_height="match_parent" > </com.example.administrator.myapplication2.MyFrameLayout> </LinearLayout>
5、在MainActivity中使用该布局即可。
SwipeRefreshLayout
实际上Android已经为我们实现一种下拉刷新,那就是通过SwipeRefreshLayout,看下新出的SwipeRefreshLayout的实现方式。
使用:
1、布局
注:SwipeRefreshLayout类似于ScrollView,内部只能有一个组件。
<?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" > <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiperefresh" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listview2" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
2、MainActivity
public class MainActivity extends Activity { private SwipeRefreshLayout refresh; private ListView mlistview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); mlistview=(ListView) findViewById(R.id.listview2); refresh=(SwipeRefreshLayout) findViewById(R.id.swiperefresh); ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,new String[]{"a","b","c","d","e","f","g","h","i","j","k","l","m","n"}); mlistview.setAdapter(adapter); //设置监听事件 refresh.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { refresh.setRefreshing(false); } }); } }
相关文章推荐
- Android 基础2
- Android 解析包时候出现错误
- MAC OS X 10.10编译android5.0源码 步骤 以及 遇到的问题
- Android 基础1
- Android结构分析Android智能指针(两)
- Android使用SpannableString实现文本框里插入表情
- Android中MediaButtonReceiver广播监听器的机制分析
- Android平台一日游
- [置顶] 【android】聊天界面的制作-简易版实现
- android 图片的加载保存 与 二级缓存
- [转] Android 命名规范 (提高代码可以读性)
- TouchEvent、dispatchTouch、onInterceptTouchEvent之间的关系概述
- Android 图片缓冲的管理-内存缓存
- android HAL接口的编码小结
- Android开发学习解决程序运行就出错的一个问题
- android 中的 ContentObserver (一)
- AndroidStudio中使用Git发布与更新,同步到本地(github关联)
- Android TextView内容居中和控件居中
- Android 5.1 Contacts源码分析(一):Contacts模块文件结构
- Android深入浅出之Binder机制