zoukankan      html  css  js  c++  java
  • PullToRefresh的使用

    主界面↓

      1 package com.wangzhen.pulltorefresh;
      2 
      3 import java.util.ArrayList;
      4 import java.util.List;
      5 
      6 import com.handmark.pulltorefresh.library.PullToRefreshBase;
      7 import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
      8 import com.handmark.pulltorefresh.library.PullToRefreshListView;
      9 import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
     10 import com.lidroid.xutils.ViewUtils;
     11 import com.lidroid.xutils.view.annotation.ContentView;
     12 import com.lidroid.xutils.view.annotation.ViewInject;
     13 import com.wangzhen.adapter.MessageAdapter;
     14 import com.wangzhen.bean.NewsBean;
     15 import com.wangzhen.listener.HttpDataListener;
     16 import com.wangzhen.tasks.LoadNewsTask;
     17 
     18 import android.support.v7.app.ActionBarActivity;
     19 import android.text.format.DateUtils;
     20 import android.widget.ListView;
     21 import android.content.Context;
     22 import android.os.Bundle;
     23 
     24 @ContentView(R.layout.activity_main)
     25 public class MainActivity extends ActionBarActivity {
     26 
     27     @ViewInject(R.id.ListView_PullToRefresh)
     28     private PullToRefreshListView ListView_PullToRefresh;
     29 
     30     Context mContext;
     31     MessageAdapter adapter;
     32     private HttpDataListener mDataListener;
     33 
     34     @Override
     35     protected void onCreate(Bundle savedInstanceState) {
     36         super.onCreate(savedInstanceState);
     37         ViewUtils.inject(this);
     38         mContext = this;
     39 
     40         Setup();
     41         InitListener();
     42 
     43         List<NewsBean> list = new ArrayList<NewsBean>();
     44         for (int i = 0; i < 5; i++) {
     45             NewsBean bean = new NewsBean();
     46             bean.setNewsImageURL("");
     47             bean.setNewsTitle("标题" + (i + 1));
     48             bean.setNewsContent("内容" + (i + 1));
     49             list.add(bean);
     50         }
     51         adapter = new MessageAdapter(mContext, list);
     52         ListView_PullToRefresh.setAdapter(adapter);
     53 
     54         ListView_PullToRefresh
     55                 .setOnRefreshListener(new OnRefreshListener<ListView>() {
     56 
     57                     @Override
     58                     public void onRefresh(
     59                             PullToRefreshBase<ListView> refreshView) {
     60                         String time = DateUtils.formatDateTime(mContext,
     61                                 System.currentTimeMillis(),
     62                                 DateUtils.FORMAT_SHOW_TIME
     63                                         | DateUtils.FORMAT_SHOW_DATE
     64                                         | DateUtils.FORMAT_ABBREV_ALL);
     65 
     66                         // 下拉刷新
     67                         if (refreshView.isShownHeader()) {
     68                             // 设置刷新标签
     69                             ListView_PullToRefresh.getLoadingLayoutProxy()
     70                                     .setRefreshingLabel("正在刷新");
     71                             // 设置刷新标签
     72                             ListView_PullToRefresh.getLoadingLayoutProxy()
     73                                     .setPullLabel("下拉刷新");
     74                             // 设置刷新标签
     75                             ListView_PullToRefresh.getLoadingLayoutProxy()
     76                                     .setReleaseLabel("释放开始刷新");
     77                             // 设置上一次刷新的提示标签
     78                             refreshView.getLoadingLayoutProxy()
     79                                     .setLastUpdatedLabel("最后更新时间:" + time);
     80                         }
     81                         // 上拉加载
     82                         if (refreshView.isShownFooter()) {
     83                             // 设置刷新标签
     84                             ListView_PullToRefresh.getLoadingLayoutProxy()
     85                                     .setRefreshingLabel("正在加载");
     86                             // 设置刷新标签
     87                             ListView_PullToRefresh.getLoadingLayoutProxy()
     88                                     .setPullLabel("上拉加载更多");
     89                             // 设置刷新标签
     90                             ListView_PullToRefresh.getLoadingLayoutProxy()
     91                                     .setReleaseLabel("释放开始加载");
     92                             // 设置上一次刷新的提示标签
     93                             refreshView.getLoadingLayoutProxy()
     94                                     .setLastUpdatedLabel("最后加载时间:" + time);
     95                         }
     96                         new LoadNewsTask(ListView_PullToRefresh, mDataListener)
     97                                 .execute();
     98                     }
     99                 });
    100     }
    101 
    102     /**
    103      * 初始化监听器
    104      */
    105     private void InitListener() {
    106         mDataListener = new HttpDataListener() {
    107 
    108             @Override
    109             public void ParseResponse(List<NewsBean> list) {
    110                 if (list == null) {
    111                     return;
    112                 }
    113                 adapter.AddData(list);
    114                 adapter.notifyDataSetChanged();
    115             }
    116         };
    117     }
    118 
    119     /**
    120      * 对下拉刷新和上拉加载进行设置
    121      */
    122     private void Setup() {
    123         /**
    124          * PullToRefresh刷新模式 
    125          * Both:上拉刷新和下拉刷新都支持 
    126          * DIABLED:禁用上拉下拉刷新
    127          * PULL_FROM_START:仅支持下拉刷新 
    128          * PULL_FROM_END:仅支持上拉加载
    129          * MANUAL_REFRESH_ONLY:只允许手动触发
    130          */
    131         ListView_PullToRefresh.setMode(Mode.BOTH);
    132     }
    133 }

     HTTP接口↓

     1 package com.wangzhen.listener;
     2 
     3 import java.util.List;
     4 
     5 import com.wangzhen.bean.NewsBean;
     6 
     7 /**
     8  * HTTP请求接口
     9  * @author Administrator
    10  *
    11  */
    12 public interface HttpDataListener {
    13 
    14     /***
    15      * 解析服务器返回结果
    16      * 
    17      * @param list
    18      */
    19     public abstract void ParseResponse(List<NewsBean> list);
    20 }

    异步加载类↓

     1 package com.wangzhen.tasks;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import com.handmark.pulltorefresh.library.PullToRefreshListView;
     7 import com.wangzhen.bean.NewsBean;
     8 import com.wangzhen.listener.HttpDataListener;
     9 
    10 import android.os.AsyncTask;
    11 
    12 /**
    13  * 异步加载新闻
    14  * 
    15  * @author Administrator
    16  * 
    17  */
    18 public class LoadNewsTask extends AsyncTask<String, Void, List<NewsBean>> {
    19 
    20     PullToRefreshListView mListView;
    21     HttpDataListener mListener;
    22 
    23     public LoadNewsTask(PullToRefreshListView listView,
    24             HttpDataListener listener) {
    25         mListView = listView;
    26         mListener = listener;
    27     }
    28 
    29     @Override
    30     protected void onPostExecute(List<NewsBean> result) {
    31         super.onPostExecute(result);
    32         mListView.onRefreshComplete();
    33         mListener.ParseResponse(result);
    34     }
    35 
    36     @Override
    37     protected List<NewsBean> doInBackground(String... params) {
    38         try {
    39             Thread.sleep(2000);
    40         } catch (InterruptedException e) {
    41             e.printStackTrace();
    42         }
    43         return GenerateData();
    44     }
    45 
    46     /**
    47      * 生成数据
    48      * 
    49      * @return
    50      */
    51     private List<NewsBean> GenerateData() {
    52         List<NewsBean> list = new ArrayList<NewsBean>();
    53         for (int i = 0; i < 5; i++) {
    54             NewsBean bean = new NewsBean();
    55             bean.setNewsImageURL("");
    56             bean.setNewsTitle("添加标题" + (i + 1));
    57             bean.setNewsContent("添加内容" + (i + 1));
    58             list.add(bean);
    59         }
    60         return list;
    61     }
    62 
    63 }

    消息适配器↓

     1 package com.wangzhen.adapter;
     2 
     3 import java.util.List;
     4 
     5 import com.wangzhen.bean.NewsBean;
     6 import com.wangzhen.pulltorefresh.R;
     7 
     8 import android.content.Context;
     9 import android.view.LayoutInflater;
    10 import android.view.View;
    11 import android.view.ViewGroup;
    12 import android.widget.BaseAdapter;
    13 import android.widget.ImageView;
    14 import android.widget.TextView;
    15 
    16 /**
    17  * 新闻适配器
    18  * 
    19  * @author Administrator
    20  * 
    21  */
    22 public class MessageAdapter extends BaseAdapter {
    23 
    24     Context mContext;
    25     LayoutInflater mInflater;
    26 
    27     List<NewsBean> mData = null;
    28 
    29     public MessageAdapter(Context context, List<NewsBean> list) {
    30         mContext = context;
    31         mInflater = LayoutInflater.from(mContext);
    32         mData = list;
    33     }
    34 
    35     /**
    36      * 增加数据源
    37      * 
    38      * @param data
    39      */
    40     public void AddData(List<NewsBean> list) {
    41         for (NewsBean bean : list) {
    42             this.mData.add(bean);
    43         }
    44     }
    45 
    46     @Override
    47     public int getCount() {
    48         // TODO Auto-generated method stub
    49         return mData.size();
    50     }
    51 
    52     @Override
    53     public NewsBean getItem(int position) {
    54         // TODO Auto-generated method stub
    55         return mData.get(position);
    56     }
    57 
    58     @Override
    59     public long getItemId(int position) {
    60         // TODO Auto-generated method stub
    61         return position;
    62     }
    63 
    64     @Override
    65     public View getView(int position, View convertView, ViewGroup parent) {
    66         ViewHolder holder;
    67         if (convertView == null) {
    68             holder = new ViewHolder();
    69             convertView = mInflater.inflate(R.layout.item_layout, null);
    70             holder.mImageView_Icon = (ImageView) convertView
    71                     .findViewById(R.id.ImageView_Icon);
    72             holder.mTextView_Title = (TextView) convertView
    73                     .findViewById(R.id.TextView_Title);
    74             holder.mTextView_Content = (TextView) convertView
    75                     .findViewById(R.id.TextView_Content);
    76             convertView.setTag(holder);
    77 
    78         } else {
    79             holder = (ViewHolder) convertView.getTag();
    80         }
    81 
    82         holder.mImageView_Icon.setImageResource(R.drawable.ic_launcher);
    83         holder.mTextView_Title.setText(mData.get(position).getNewsTitle());
    84         holder.mTextView_Content.setText(mData.get(position).getNewsContent());
    85 
    86         return convertView;
    87     }
    88 
    89     class ViewHolder {
    90         ImageView mImageView_Icon;
    91         TextView mTextView_Title;
    92         TextView mTextView_Content;
    93     }
    94 
    95 }

    实体类↓

     1 package com.wangzhen.bean;
     2 
     3 /**
     4  * 实体类
     5  * 
     6  * @author Administrator
     7  * 
     8  */
     9 public class NewsBean {
    10 
    11     private String NewsImageURL;
    12     private String NewsTitle;
    13     private String NewsContent;
    14 
    15     public String getNewsImageURL() {
    16         return NewsImageURL;
    17     }
    18 
    19     public void setNewsImageURL(String newsImageURL) {
    20         NewsImageURL = newsImageURL;
    21     }
    22 
    23     public String getNewsTitle() {
    24         return NewsTitle;
    25     }
    26 
    27     public void setNewsTitle(String newsTitle) {
    28         NewsTitle = newsTitle;
    29     }
    30 
    31     public String getNewsContent() {
    32         return NewsContent;
    33     }
    34 
    35     public void setNewsContent(String newsContent) {
    36         NewsContent = newsContent;
    37     }
    38 
    39 }

     在PullToRefreshBase文件里要添加以下代码:

    1 // 判别头部是否展示出来,如果展示出来代表下拉使得头部展示。true为下拉
    2     public boolean isShownHeader() {
    3         return getHeaderLayout().isShown();
    4     }
    5 
    6     // 判别低部是否展示出来,如果展示出来代表上拉使得低部展示。true为上拉
    7     public boolean isShownFooter() {
    8         return getFooterLayout().isShown();
    9     }
  • 相关阅读:
    工厂模式之简单工厂模式
    JavaMail(二):利用JavaMail发送复杂邮件
    JavaMail(一):利用JavaMail发送简单邮件
    浅谈Java中静态代码块和非静态代码块
    MySQL中常用转换函数介绍
    jquery easyui datagrid 分页 详解
    tomcat出现的PermGen Space问题(bat,或者eclipse启动。)
    把Blob 转化为字符串查询。
    日期相关---SimpleDateFormat的setLenient(true/false)-----自动计算日期
    处理同一个字段,多种日期格式
  • 原文地址:https://www.cnblogs.com/lavalike/p/4662961.html
Copyright © 2011-2022 走看看