Android使用PullToRefresh實(shí)現(xiàn)上拉加載和下拉刷新效果的代碼
在沒給大家介紹正文之前,先給大家介紹展示下運(yùn)行圖,如果大家感覺還不錯(cuò),請(qǐng)繼續(xù)往下閱讀:
相關(guān)閱讀:分享Android中pullToRefresh的使用心得
項(xiàng)目已同步至:https://github.com/nanchen2251/pullToRefreshDemo
簡(jiǎn)單使用詳情:
1)studio可以直接在app的module設(shè)置中直接進(jìn)行搜索,但是有-的必須添上,而不能用空格代替,為了更加了解這個(gè)東西,我還是推薦大家去這里看看,奉上網(wǎng)址:
https://github.com/chrisbanes/Android-PullToRefresh
所以去git上下載了后通過(guò)studio的導(dǎo)入Module功能,導(dǎo)入library,修改library的gradle文件和自己的項(xiàng)目一致
apply plugin: 'com.android.library' android { compileSdkVersion 23 buildToolsVersion "24.0.0" defaultConfig { minSdkVersion 18 targetSdkVersion 23 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } }
2)然后添加依賴,將app與其進(jìn)行關(guān)聯(lián)。
3)下面打開library可以看到很多的東西,這個(gè)不僅可以支持ListView,還可以支持GridView和ScollView等等,可謂相當(dāng)全面,不過(guò)是否能和當(dāng)前火熱的RecyclerView一起使用樓主還沒試過(guò)。
4)下面在我們的xml布局中布局,這里我用了自定義控件的屬性,所以添加了一個(gè)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,所以我們需要一個(gè)java Bean 和一個(gè)Item的layout進(jìn)行自定義布局。
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; } }
還有l(wèi)ist_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"> <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>
6)使用非常簡(jiǎn)單,在Activity中,代碼注釋已經(jīng)比較全面了,這里模擬了異步下載數(shù)據(jù)任務(wù),并且重寫了Adaper,至于其中為什么用靜態(tài)內(nèi)部類,這個(gè)好處很多,大家可以百度科普。
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<>(); //設(shè)置可上拉刷新和下拉刷新 refresh_lv.setMode(PullToRefreshBase.Mode.BOTH); //設(shè)置刷新時(shí)顯示的文本 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++; } } /** * 異步下載任務(wù) */ 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; } /** * 完成時(shí)的方法 */ @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實(shí)現(xiàn)上拉加載和下拉刷新效果的代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多
- android使用Ultra-PullToRefresh實(shí)現(xiàn)下拉刷新自定義代碼
- android使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載
- Android使用PullToRefresh完成ListView下拉刷新和左滑刪除功能
- Android開源項(xiàng)目PullToRefresh下拉刷新功能詳解2
- Android開源項(xiàng)目PullToRefresh下拉刷新功能詳解
- Android下拉刷新控件PullToRefresh實(shí)例解析
- Android實(shí)現(xiàn)簡(jiǎn)單的下拉刷新pulltorefresh
- Android程序開發(fā)之使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載
- Android PullToRefreshLayout下拉刷新控件的終結(jié)者
- Android帶刷新時(shí)間顯示的PullToRefresh上下拉刷新
相關(guān)文章
Android MVP模式ListView中嵌入checkBox的使用方法
這篇文章主要介紹了Android MVP模式ListView中嵌入checkBox的使用方法,如何在ListView中嵌入checkBox配合使用,感興趣的小伙伴們可以參考一下2016-08-08Kotlin啟動(dòng)協(xié)程的三種方式示例詳解
這篇文章主要為大家介紹了Kotlin啟動(dòng)協(xié)程的三種方式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12android獲取當(dāng)前手機(jī)號(hào)示例程序
這篇文章主要介紹了android如何獲取當(dāng)前手機(jī)號(hào)的方法,大家參考使用吧2013-11-11android編程實(shí)現(xiàn)sd卡讀取數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了android編程實(shí)現(xiàn)sd卡讀取數(shù)據(jù)庫(kù)的方法,涉及Android權(quán)限控制及針對(duì)sd卡與數(shù)據(jù)庫(kù)的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android手機(jī)衛(wèi)士之綁定sim卡序列號(hào)
這篇文章主要介紹了Android手機(jī)衛(wèi)士之綁定sim卡序列號(hào)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10ListView異步加載圖片實(shí)現(xiàn)思路(優(yōu)化篇)
關(guān)于listview的異步加載,網(wǎng)上其實(shí)很多示例了,中心思想都差不多,不過(guò)很多版本或是有bug,或是有性能問(wèn)題有待優(yōu)化,下面就讓在下闡述其原理以探索個(gè)中奧秘2013-04-04