下拉刷新

来源:互联网 发布:java看什么书好 编辑:程序博客网 时间:2024/05/17 06:03

一、导入Library

下载源码后(https://github.com/chrisbanes/Android-PullToRefresh),里面有个Library工程,添加工程到项目中;

代码奉上

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <com.handmark.pulltorefresh.library.PullToRefreshListView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/ptrlv_pull"        ></com.handmark.pulltorefresh.library.PullToRefreshListView></LinearLayout>



package com.example.g160828_android08_listview;import android.os.AsyncTask;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import com.handmark.pulltorefresh.library.PullToRefreshWebView;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/7/22 0022. */public class PullActivity extends AppCompatActivity{    private PullToRefreshListView ptrlv_pull;    private List<String> date;    private ArrayAdapter adapter;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_pull);        //获取控件        ptrlv_pull = (PullToRefreshListView) findViewById(R.id.ptrlv_pull);        //定义数据        date = new ArrayList<>();        for (int i = 0; i <10 ; i++) {date.add("傻子"+i);        }        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, date);        ptrlv_pull.setAdapter(adapter);        //设置下拉的监听        ptrlv_pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {            @Override            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {              //  Toast.makeText(PullActivity.this, "啦了", Toast.LENGTH_SHORT).show();new MyTask().execute();            }            @Override            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {            }        });    }    class  MyTask extends AsyncTask{//获取数据        @Override        protected Object doInBackground(Object[] objects) {            for (int i = 0; i <3 ; i++) {                date.add(0,"爱你"+i);            }            try {
//模拟网络                Thread.sleep(200);            } catch (InterruptedException e) {                e.printStackTrace();            }            return null;        }        @Override        protected void onPostExecute(Object o) {            super.onPostExecute(o);            //通知发生改变            adapter.notifyDataSetChanged();            //通知啦完了            ptrlv_pull.onRefreshComplete();        }    }}






原创粉丝点击