PullToRefresh引入依赖,布局,适配器ListView上拉加载下拉刷新

本文主要是介绍PullToRefresh引入依赖,布局,适配器ListView上拉加载下拉刷新,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用PullToRefreshListView前 先在File-New-import Module 导入pulltoRefreshLibrary,
然后给项目 添加 module的依赖,
 
选择pulltoRefreshLibrary

activity_main.xml 
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><com.handmark.pulltorefresh.library.PullToRefreshListViewxmlns:ptr="http://schemas.android.com/apk/res-auto"android:layout_height="match_parent"android:layout_width="match_parent"android:id="@+id/refresh_list_view"ptr:ptrDrawable="@drawable/default_ptr_flip"ptr:ptrAnimationStyle="flip"ptr:ptrHeaderBackground="#383838"ptr:ptrHeaderTextColor="#FFFFFF">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</RelativeLayout>
适配器里面的

public class MyAdapter extends BaseAdapter{List<DBean.NewslistBean> list;Context context;public MyAdapter(List<DBean.NewslistBean> list, Context context) {this.context = context;this.list = list;ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(context);ImageLoader.getInstance().init(configuration);}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int i) {return list.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {ViewHolder holder;if (view==null){view = View.inflate(context,R.layout.list_item,null);holder = new ViewHolder();holder.imageView = view.findViewById(R.id.list_image);holder.textView = view.findViewById(R.id.list_text);view.setTag(holder);}else{holder = (ViewHolder) view.getTag();}holder.textView.setText(list.get(i).getTitle());ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(),holder.imageView);return view;}class ViewHolder{ImageView imageView;TextView textView;}
}
MainActivity里面的
public class MainActivity extends AppCompatActivity { private List<DBean.NewslistBean> list = new ArrayList<>(); private PullToRefreshListView refreshListView;int num = 1;   private MyAdapter myAdapter;    private ILoadingLayout endlabels; private ILoadingLayout startlabels; @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);     refreshListView = (PullToRefreshListView) findViewById(R.id.refresh_list_view);       getDataFromNet();        //设置PullToRefreshListView刷新模式,上拉下拉都可以       refreshListView.setMode(PullToRefreshBase.Mode.BOTH);        //设置刷新显示的状态,可以下拉,不能上拉      startlabels = refreshListView.getLoadingLayoutProxy(true,false);    startlabels.setRefreshingLabel("正在刷新");      startlabels.setPullLabel("下拉刷新");       startlabels.setReleaseLabel("松开刷新");     endlabels = refreshListView.getLoadingLayoutProxy(false,true);      endlabels.setRefreshingLabel("正在加载");       endlabels.setPullLabel("上拉加载");       endlabels.setReleaseLabel("松开加载");       //设置监听事件       refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {   @Override           //下拉刷新调用          public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {             num =1;              //获取数据            getDataFromNetxiala();            setAdapter();           }         @Override        //上拉加载调用     public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {   num++;    getDataFromNet();       setAdapter();         }     });  }   private void setAdapter() {      if (myAdapter==null) {        myAdapter = new MyAdapter(list, MainActivity.this);     refreshListView.setAdapter(myAdapter);      }else{     myAdapter.notifyDataSetChanged();     }   }   private void getDataFromNet() {   AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {  @Override          protected String doInBackground(Void... voids) {   String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+num;        try {                  URL url = new URL(path);                HttpURLConnection connection = (HttpURLConnection) url.openConnection();  connection.setRequestMethod("GET");          connection.setConnectTimeout(5000);                 connection.setReadTimeout(5000);                  int responseCode = connection.getResponseCode();             if (responseCode==200){                    InputStream inputStream = connection.getInputStream();                 String json = streamtoString(inputStream,"utf-8");                 Thread.sleep(2000);                 return json;                }             } catch (Exception e) {  e.printStackTrace();             }              return null;     }         @Override   protected void onPostExecute(String json) {    Gson gson = new Gson();             DBean dBean = gson.fromJson(json, DBean.class);     list.addAll(dBean.getNewslist());              long currentTimeMillis = System.currentTimeMillis();      Date date2= new Date(currentTimeMillis);             SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");      String format = dateFormat.format(date2);              //设置刷新的时间              endlabels.setLastUpdatedLabel(format);        //停止刷新              refreshListView.onRefreshComplete();    setAdapter();      }       };      asyncTask.execute(); }  private void getDataFromNetxiala() {   AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {  @Override        protected String doInBackground(Void... voids) {       String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+num;       try {              URL url = new URL(path);                 HttpURLConnection connection = (HttpURLConnection) url.openConnection();        connection.setRequestMethod("GET");              connection.setConnectTimeout(5000);               connection.setReadTimeout(5000);                 int responseCode = connection.getResponseCode();              if (responseCode==200){                     InputStream inputStream = connection.getInputStream();           String json = streamtoString(inputStream,"utf-8");                    Thread.sleep(2000);                     return json;                   }              } catch (Exception e) {     e.printStackTrace();       }              return null;      }          @Override         protected void onPostExecute(String json) {   Gson gson = new Gson();           DBean dBean = gson.fromJson(json, DBean.class);     list.addAll(0,dBean.getNewslist());             long currentTimeMillis = System.currentTimeMillis();  Date date2= new Date(currentTimeMillis);            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");   String format = dateFormat.format(date2);             //设置刷新的时间            startlabels.setLastUpdatedLabel(format);     //停止刷新               refreshListView.onRefreshComplete();     setAdapter();          }     };      asyncTask.execute();  }   private String streamtoString(InputStream inputStream, String charset) {    try {    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);      String s= null;        StringBuilder builder = new StringBuilder();      while((s=bufferedReader.readLine())!=null){     builder.append(s);           }           return builder.toString();     } catch (Exception e) {   e.printStackTrace();      }        return charset;   }}


这篇关于PullToRefresh引入依赖,布局,适配器ListView上拉加载下拉刷新的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/869310

相关文章

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

Java -jar命令如何运行外部依赖JAR包

《Java-jar命令如何运行外部依赖JAR包》在Java应用部署中,java-jar命令是启动可执行JAR包的标准方式,但当应用需要依赖外部JAR文件时,直接使用java-jar会面临类加载困... 目录引言:外部依赖JAR的必要性一、问题本质:类加载机制的限制1. Java -jar的默认行为2. 类加

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

使用Python获取JS加载的数据的多种实现方法

《使用Python获取JS加载的数据的多种实现方法》在当今的互联网时代,网页数据的动态加载已经成为一种常见的技术手段,许多现代网站通过JavaScript(JS)动态加载内容,这使得传统的静态网页爬取... 目录引言一、动态 网页与js加载数据的原理二、python爬取JS加载数据的方法(一)分析网络请求1

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur

IDEA下"File is read-only"可能原因分析及"找不到或无法加载主类"的问题

《IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题》:本文主要介绍IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题,具有很好的参... 目录1.File is read-only”可能原因2.“找不到或无法加载主类”问题的解决总结1.File

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

CSS引入方式和选择符的讲解和运用小结

《CSS引入方式和选择符的讲解和运用小结》CSS即层叠样式表,是一种用于描述网页文档(如HTML或XML)外观和格式的样式表语言,它主要用于将网页内容的呈现(外观)和结构(内容)分离,从而实现... 目录一、前言二、css 是什么三、CSS 引入方式1、行内样式2、内部样式表3、链入外部样式表四、CSS 选