Android —页面下拉刷新(ListView与SwipeRefreshLayout)

  相信大家在使用淘宝的时候应该都看到过下拉刷新的效果。这种效果看起来会感觉比较难做,一起来看下下拉刷新。

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 —页面下拉刷新(ListView与SwipeRefreshLayout))