• PullToRefresh +ListView 下拉刷新Demo1 (本地数据)


    项目地址  https://github.com/chrisbanes/Android-PullToRefresh     

    通过File>New>Import Module  导入项目

    MainActivity
    package com.example.administrator.pulltorefreshdemo;
    
    
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ListView;
    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 refresh_lv; //声明对象
        private List<Music> list;    //声明箱子
        private DataAdapter adapter;  //声明适配器   为了更新声明在头部
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);    //绑定布局文件
    
            refresh_lv = (PullToRefreshListView) findViewById(R.id.main_pull_refresh_lv);  //初始化listview
            list = new ArrayList<>();  //把声明的箱子实例化
    //-----------------------------------------------------------------------------------------------------------------
            //设置可上拉刷新和下拉刷新
            refresh_lv.setMode(PullToRefreshBase.Mode.BOTH);
    
            //设置刷新时显示的文本
            ILoadingLayout startLayout = refresh_lv.getLoadingLayoutProxy(true,false);
            startLayout.setPullLabel("正在下拉刷新...");
            startLayout.setRefreshingLabel("正在玩命加载中...");
            startLayout.setReleaseLabel("放开以刷新");
    
    
            ILoadingLayout endLayout = refresh_lv.getLoadingLayoutProxy(false,true);
            endLayout.setPullLabel("正在上拉刷新...");
            endLayout.setRefreshingLabel("正在玩命加载中...");
            endLayout.setReleaseLabel("放开以刷新");
    
    
    //--------------------监听下拉上拉-------------------------------------------------------------
            refresh_lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {     //实现这个方法之前创建一个异步类LoadDataAsyncTask
                    new LoadDataAsyncTask(MainActivity.this).execute();
                }
    
                @Override
                public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                    new LoadDataAsyncTask(MainActivity.this).execute();
                }
            });
            //--------------------监听结束-------------------------------------------------------------
    
    
    
            loadData();
    
            adapter = new DataAdapter(this,list);     //给listview 添加之前把适配器adapter初始化
            refresh_lv.setAdapter(adapter);           //给listview 添加适配器adapter
    
    
        }
        //----------------------------模拟数据绑定------------------------------------------------------------
        private int count = 1;
        private void loadData(){
            for (int i = 0; i < 4; i++) {
                list.add(new Music("歌曲"+count,"歌手"+count));
                count++;
            }
        }
    
        /**
         * 异步下载任务
         */
        //----------------------------异步下载任务类------------------------------------------------------------
        private static class LoadDataAsyncTask extends AsyncTask<Void,Void,String>{
    
            private MainActivity mainActivity;           //通过构造方法把activity传进去
    
            public LoadDataAsyncTask(MainActivity mainActivity) {//通过构造方法把activity传进去
                this.mainActivity = mainActivity;//通过构造方法把activity传进去
            }
    
            @Override
            protected String doInBackground(Void... params) {
                try {
                    Thread.sleep(2000);
                    mainActivity.loadData();
                    return "seccess";
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }
    
            /**
             * 完成时的方法
             */
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if (s.equals("seccess")){   //如果数据下载完成  //数据显示在列表
                    mainActivity.adapter.notifyDataSetChanged();  //通知
                    mainActivity.refresh_lv.onRefreshComplete();//刷新完成  合上
                }
            }
        }
        //----------------------------异步下载任务类结束------------------------------------------------------------
    
    
    }
    Music  getter and  setter  ,有参无参构造函数加起来
    package com.example.administrator.pulltorefreshdemo;
    
    /**
     * Created by Administrator on 2018/1/6.
     */
    
    public class Music {
        private String title;
        private String singer;
    
        public Music() {
        }
    
        public Music(String title, String singer) {
            this.title = title;
            this.singer = singer;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getSinger() {
            return singer;
        }
    
        public void setSinger(String singer) {
            this.singer = singer;
        }
    }
    DataAdapter

    package com.example.administrator.pulltorefreshdemo;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import java.util.List;
    
    /**
     * Created by Administrator on 2018/1/6.
     */
    
    public  class DataAdapter extends BaseAdapter{
    
        private Context context;
        private List<Music> list;
    
        public DataAdapter(Context context, List<Music> list) {
            this.context = context;
            this.list = list;
        }
    
        @Override
        public int getCount() {
            if (list != null){
                return list.size();
            }
            return 0;
        }
    
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh;
            if (convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);
                vh = new ViewHolder();
                vh.tv_title = (TextView) convertView.findViewById(R.id.item_title);
                vh.tv_singer = (TextView) convertView.findViewById(R.id.item_singer);
                convertView.setTag(vh);
            }else{
                vh = (ViewHolder) convertView.getTag();
            }
            Music music = (Music) getItem(position);
            vh.tv_title.setText(music.getTitle());
            vh.tv_singer.setText(music.getSinger());
            return convertView;
        }
    
        class ViewHolder{
            TextView tv_title;
            TextView tv_singer;
        }
    }
    activity_main
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <com.handmark.pulltorefresh.library.PullToRefreshListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/main_pull_refresh_lv"
            app:ptrAnimationStyle="flip"
            app:ptrHeaderBackground="@android:color/transparent"
            app:ptrHeaderTextColor="#919191"/>
    
    </RelativeLayout>

    list_item

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_title"
            android:text="歌曲1"
            android:textSize="20sp"
            android:layout_alignParentLeft="true"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_singer"
            android:textSize="20sp"
            android:text="歌手1"
            android:layout_alignParentRight="true"/>
    
    
    </RelativeLayout>
  • 相关阅读:
    HDU 2011 (水)
    HDU 2010 (水)
    HDU 2009 (水)
    HDU 2007 (水)
    mcsscm
    Meow~
    沉沦魔是战5渣
    谈谈和81的过往
    量子力学就像一座破房子 Quantum power is like a broken house.
    能源危机的应对方案(基于Energy不守恒定律)Response to the Energy Crisis (Based on the Law of Mana Non-Conservation)
  • 原文地址:https://www.cnblogs.com/imfutureman/p/8213238.html
Copyright © 2020-2023  润新知