Pulltorefreshlistview刷新使用

1、首先到Lib包到Project,在依赖上工程

2、xml布局文件

<com.handmark.pulltorefresh.library.PullToRefreshListView
    android:id="@+id/pfl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

3、MainAcitivity代码

 
  

public class MainActivity extends AppCompatActivity {

    private List adlist;
    private PullToRefreshListView pfl;
    private MyAdapter adapter;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 0:
                    adapter.notifyDataSetChanged();
                    pfl.onRefreshComplete();
                    break;
                case 1:
                    adapter.notifyDataSetChanged();
                    Toast.makeText(MainActivity.this,"下拉啦",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    private ViewPager vp;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化组件
        pfl = (PullToRefreshListView) findViewById(R.id.pfl);
        //布局加载器,加载ViewPager布局
        View view = View.inflate(MainActivity.this, R.layout.pager_item, null);
        vp = (ViewPager) view.findViewById(R.id.vp);
        //转换头布局
        listView = pfl.getRefreshableView();
        //开启子线程
        new Thread(){

            private String json;

            @Override
            public void run() {
                super.run();
                String path="http://www.meirixue.com/api.php?c=index&a=index";
                json = NetWork.getJson(path);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        Gson gson = new Gson();
                        Bean bean = gson.fromJson(json, Bean.class);
                        adlist = bean.getData().getAdlist();
                        adapter = new MyAdapter();
                        pfl.setAdapter(adapter);
                        //进行刷新
                        pfl.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
                            @Override
                            public void onRefresh(PullToRefreshBase refreshView) {

                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        adlist.addAll(0,adlist);
                                        handler.sendEmptyMessage(0);
                                    }
                                },2000);

                            }
                        });

                        //PageView适配器
                        MyPagerAdapter pagerAdapter = new MyPagerAdapter();
                        //设置适配器
                        vp.setAdapter(pagerAdapter);
                        //添加头部
                        listView.addHeaderView(vp);
                        //进行上拉加载更多
                        pfl.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
                            @Override
                            public void onLastItemVisible() {
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        adlist.addAll(adlist);
                                        handler.sendEmptyMessage(1);
                                    }
                                },2000);
                            }
                        });


                    }
                });
            }
        }.start();


    }

    /**
     * PagerAdapter适配器
     */
    class MyPagerAdapter extends PagerAdapter {

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            ImageView imageView = new ImageView(MainActivity.this);
            ImageLoader.getInstance().displayImage(adlist.get(position%adlist.size()).getImg(),imageView);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return adlist.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }

    /**
     * BaseAdapter适配器
     */
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return adlist.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(adlist.get(position).getName());
            return textView;
        }
    }


}


你可能感兴趣的:(Android)