PullToRefreshListView下拉刷新与上拉载入
生活随笔
收集整理的這篇文章主要介紹了
PullToRefreshListView下拉刷新与上拉载入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【聲明】轉載請注明出處。此文出自指尖飛落的博客:http://blog.csdn.net/huntersnail
——尊重作者。知識無價。交流無限!
前言:下拉刷新是Android項目中常常要用到的東西,對于沒有經驗的開發人員來說。第一次接觸確實比較頭疼。
網上有非常多可供參考的框架,我用的是GitHub那里下載的一個框架handmark點擊打開鏈接。
這也是我在網上看到大家用得最多的一個框架,當然,非常多大神會不屑一顧,分分鐘就自己寫出來了。咱不去比那玩意兒,畢竟自己寫還須要一點功底的。我想還有非常多是和筆者一樣剛入行的。僅供交流。望各大神指教。以下就貼下代碼。說一下怎樣去用!
1、導庫
右鍵點擊項目——>點擊Properties——>點擊Android——>點擊Add加入庫——>Ok
2、Activity
package com.example.refreshdemo;import java.util.ArrayList; import java.util.List;import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; import com.handmark.pulltorefresh.library.PullToRefreshListView;import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.Gravity; 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.PullToRefreshBase.OnRefreshListener2;public class MainActivity extends Activity {private PullToRefreshListView mPtrlv;private List<String> list;private MyAdapter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mPtrlv=(PullToRefreshListView) findViewById(R.id.ptrlv);//設置模式,BOTH:上拉下拉//DISABLED:取消上拉下拉//PULL_FROM_END:僅僅上拉//PULL_FROM_START:僅僅下拉mPtrlv.setMode(Mode.BOTH);list=new ArrayList<String>();initData();setListener();mAdapter=new MyAdapter();mPtrlv.setAdapter(mAdapter);}/*** 初始化模擬數據*/private void initData() {for (int i = 0; i < 10; i++) {list.add("模擬數據"+i);}}private void setListener() {mPtrlv.setOnRefreshListener(new OnRefreshListener2<ListView>() {/*** 下拉刷新* 假設須要下拉載入上一頁,在這種方法onPullDownToRefresh()做處理*/int x=1;@Overridepublic void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {list.add(0, "下拉刷新<<<上一頁:"+x++);//通知數據改變mAdapter.notifyDataSetChanged();new MyTask().execute();}/*** 上拉載入* 假設須要下拉載入上一頁,在這種方法onPullUpToRefresh()做處理*/int y=1;@Overridepublic void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {list.add("上拉載入>>>下一頁:"+y++);//通知數據改變mAdapter.notifyDataSetChanged();new MyTask().execute();}});}/*** 自己定義適配器* @author Endoon* 2015-7-24 09:23:19*/class MyAdapter extends BaseAdapter{@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {TextView mView=new TextView(getApplicationContext());mView.setText(list.get(position));mView.setTextColor(Color.RED);mView.setGravity(Gravity.CENTER);return mView;}}/*** 異步載入數據* @author Endoon* 2015-7-24 09:23:12*/class MyTask extends AsyncTask<Void, Void, Void>{@Overrideprotected Void doInBackground(Void... params) {return null;}@Overrideprotected void onPostExecute(Void result) {super.onPostExecute(result);//Mark the current Refresh as complete. Will Reset the UI and hide the Refreshing ViewmPtrlv.onRefreshComplete();//刷新UI}}; }3、Xml文件
<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"tools:context=".MainActivity" ><com.handmark.pulltorefresh.library.PullToRefreshListViewandroid:id="@+id/ptrlv"android:layout_width="fill_parent"android:layout_height="fill_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView></RelativeLayout>4、效果圖
源代碼和庫下載地址:點擊打開鏈接
O(∩_∩)O哈哈~歡迎各種吐槽、歧視、不吝賜教、交流......
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆轉載請注明出處?指尖飛落的博客☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
轉載于:https://www.cnblogs.com/jzdwajue/p/7095902.html
總結
以上是生活随笔為你收集整理的PullToRefreshListView下拉刷新与上拉载入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 残疾人证后两位是什么意思(残疾证后两位代
- 下一篇: 出口退税账务处理会计分录进出口企业退税流