Android —页面下拉刷新(ListView与SwipeRefreshLayout)
来源:互联网 发布:塞规计算软件 编辑:程序博客网 时间:2024/05/07 08:32
相信大家在使用淘宝的时候应该都看到过下拉刷新的效果。这种效果看起来会感觉比较难做,一起来看下下拉刷新。
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); } }); }}
0 0
- Android —页面下拉刷新(ListView与SwipeRefreshLayout)
- android,SwipeRefreshLayout,listview下拉刷新
- Android控件ListView下拉刷新之SwipeRefreshLayout
- 解决listview下拉与SwipeRefreshLayout下拉刷新冲突
- 解决SwipeRefreshLayout下拉刷新与ListView下拉的冲突
- Android下拉刷新-SwipeRefreshLayout
- Android下拉刷新SwipeRefreshLayout
- Android 下拉刷新SwipeRefreshLayout
- Android SwipeRefreshLayout下拉刷新
- 下拉刷新——Android使用SwipeRefreshLayout简单实现下拉刷新与加载跟多
- SwipeRefreshLayout + ListView 实现仿简书 下拉刷新 与 上拉加载
- Android SwipeRefreshLayout 包含ListView 上拉刷新 下拉加载
- Android自定义ListView实现下拉刷新,效果仿SwipeRefreshLayout
- Android官方下拉刷新控件SwipeRefreshLayout+ListView 介绍
- Android listview+SwipeRefreshLayout 下拉刷新,上拉加载.
- Android下拉刷新组件—SwipeRefreshLayout
- Android控件(一)下拉刷新:SwipeRefreshLayout
- SwipeRefreshLayout(Android的下拉刷新组件)
- 【工具】代码注释文档生存工具(Doxygen)
- jquery实现滚轮监听
- iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
- 75道逻辑推理
- 【Java】值得注意的一些语法点2
- Android —页面下拉刷新(ListView与SwipeRefreshLayout)
- 线性表部分操作
- linux双网卡配置
- 类似于微信的在一个Activity中界面的相互切换
- ZOJ-2477 Magic Cube
- 逻辑分析
- HTTP协议响应码
- 单例模式、双检测锁定DCL、volatile
- 【工具】C#代码混淆工具(Dotfuscator)