Android 开发中,pullToRefreshListView 的刷新,加载

pullToRefreshListView控件本身自带listview,应用此控件需用添加一个包

MainActivity:

package animtest.com.example.e531.pulltorefreshtest_demo;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private PullToRefreshListView pullToRefreshListView;
    private Handler myHandler=new Handler();
    private List lists=new ArrayList();
    private MyAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_lv);

        initLv();

        initData();

        setAdapter();

    }

    public void initData(){
        for(int i=0;i<20;i++){
            lists.add("这是第"+i+"条数据");
        }
    }

    public void setAdapter(){
        if(adapter==null){
            adapter=new MyAdapter();
            pullToRefreshListView.setAdapter(adapter);

        }else{
            adapter.notifyDataSetChanged();
        }

    }

    public void addtoTop(){

        List listss=new ArrayList();
        for(int i=0;i<20;i++){
            listss.add("up up up");
        }


        lists.addAll(0,listss);

    }

    public void addtoBottom(){
        List listss=new ArrayList();
        for(int i=0;i<20;i++){
            listss.add("bottom bottom bottom");
        }


        lists.addAll(listss);

    }





    public void initLv(){
        //设置刷新模式 ,both代表支持上拉和下拉,pull_from_end代表上拉,pull_from_start代表下拉
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);


/*这里通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态
如果想区分上拉和下拉状态的不同,可以分别设置*/

        ILoadingLayout startLabels = pullToRefreshListView.getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新");
        startLabels.setRefreshingLabel("正在拉");
        startLabels.setReleaseLabel("放开刷新");


        ILoadingLayout endLabels = pullToRefreshListView.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新");
        endLabels.setRefreshingLabel("正在载入...");
        endLabels.setReleaseLabel("放开刷新...");


/*如果Mode设置成Mode.BOTH,需要设置刷新Listener为OnRefreshListener2,并实现onPullDownToRefresh()、onPullUpToRefresh()两个方法。
  如果Mode设置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要设置刷新Listener为OnRefreshListener,同时实现onRefresh()方法。
  当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只调用onPullDownToRefresh()方法,Mode.PULL_FROM_END的时候只调用onPullUpToRefresh()方法.

  加载数据完成后 必须 调用下 onRefreshComplete() 完成关闭 header,footer视图
*/
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {//下拉刷新的回调
                //下拉刷新的数据,显示在listview列表的最上面
                addtoTop();
                myHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //刷新完成,必须在异步下完成
                        pullToRefreshListView.onRefreshComplete();
                        //刷新适配器
                        setAdapter();

                    }
                },1000);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {//上拉加载的回调
                //加载更多的数据,添加到集合列表的最后面
                addtoBottom();
                myHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //刷新完成,必须在异步下完成
                        pullToRefreshListView.onRefreshComplete();
                        //刷新适配器
                        setAdapter();
                    }
                },1000);
            }
        });

    }


    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView=new TextView(MainActivity.this);
            textView.setText(lists.get(position));
            return textView;
        }
    }
}
activity_main.xml:




    
    

    








你可能感兴趣的:(Android,开发)