在没给大家介绍正文之前,先给大家介绍展示下运行图,如果大家感觉还不错,请继续往下阅读:
相关阅读:分享Android中pullToRefresh的使用心得
项目已同步至:https://github.com/nanchen2251/pullToRefreshDemo
简单使用详情:
1)studio可以直接在app的module设置中直接进行搜索,但是有-的必须添上,而不能用空格代替,为了更加了解这个东西,我还是推荐大家去这里看看,奉上网址:
https://github.com/chrisbanes/Android-PullToRefresh
所以去git上下载了后通过studio的导入Module功能,导入library,修改library的gradle文件和自己的项目一致
apply plugin: 'com.androID.library'androID {compileSdkVersion 23buildToolsversion "24.0.0"defaultConfig {minSdkVersion 18targetSdkVersion 23}buildTypes {release {MinifyEnabled falseproguardfiles getDefaultProguardfile('proguard-androID.txt'),'proguard-rules.txt'}}}
2)然后添加依赖,将app与其进行关联。
3)下面打开library可以看到很多的东西,这个不仅可以支持ListVIEw,还可以支持GrIDVIEw和ScollVIEw等等,可谓相当全面,不过是否能和当前火热的RecyclerVIEw一起使用楼主还没试过。
4)下面在我们的xml布局中布局,这里我用了自定义控件的属性,所以添加了一个xmlns:app=http://schemas.android.com/apk/res-auto
<?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="com.example.nanchen.pulltorefreshdemo.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>
5)由于我们这里使用的是它的ListVIEw,所以我们需要一个java Bean 和一个Item的layout进行自定义布局。
package com.example.nanchen.pulltorefreshdemo;/** * Created by 南尘 on 16-7-20. */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; }}
还有List_item.xml
<?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"><TextVIEwandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:ID="@+ID/item_Title"androID:text="歌曲1"androID:textSize="20sp"androID:layout_alignParentleft="true"/><TextVIEwandroID: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>
6)使用非常简单,在Activity中,代码注释已经比较全面了,这里模拟了异步下载数据任务,并且重写了Adaper,至于其中为什么用静态内部类,这个好处很多,大家可以百度科普。
package com.example.nanchen.pulltorefreshdemo;import androID.content.Context;import androID.os.AsyncTask;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.LayoutInflater;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 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); 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) { new LoadDataAsyncTask(MainActivity.this).execute(); } @OverrIDe public voID onPullUpToRefresh(PullToRefreshBase<ListVIEw> refreshVIEw) { new LoadDataAsyncTask(MainActivity.this).execute(); } }); loadData(); adapter = new DataAdapter(this,List); refresh_lv.setAdapter(adapter); } private int count = 1; private voID loadData(){ for (int i = 0; i < 10; i++) { List.add(new Music("歌曲"+count,"歌手"+count)); count++; } } /** * 异步下载任务 */ private static class LoadDataAsyncTask extends AsyncTask<VoID,VoID,String>{ private MainActivity mainActivity; public LoadDataAsyncTask(MainActivity mainActivity) { this.mainActivity = mainActivity; } @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();//刷新完成 } } } /** * 自定义适配器 */ private static 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; } }}
以上所述是小编给大家介绍的AndroID使用PullToRefresh实现上拉加载和下拉刷新效果的代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android使用PullToRefresh实现上拉加载和下拉刷新效果的代码全部内容,希望文章能够帮你解决Android使用PullToRefresh实现上拉加载和下拉刷新效果的代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)