當前位置:
首頁 >
android下拉刷新和上拉加载的一个简单库
發布時間:2025/3/21
35
豆豆
生活随笔
收集整理的這篇文章主要介紹了
android下拉刷新和上拉加载的一个简单库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
介紹一個android下拉刷新、上拉加載的庫:
https://github.com/chrisbanes/Android-PullToRefresh
使用方式,創建好一個Android項目,導入library庫即可:
布局文件:
<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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><com.handmark.pulltorefresh.library.PullToRefreshListViewandroid:id="@+id/pull_refresh_list"android:layout_width="fill_parent"android:layout_height="fill_parent" ></com.handmark.pulltorefresh.library.PullToRefreshListView></RelativeLayout>主要Activity: package com.example.pulltofresh;import java.util.Arrays; import java.util.LinkedList;import android.app.Activity; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast;import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; import com.handmark.pulltorefresh.library.PullToRefreshListView;public class MainActivity extends Activity {private PullToRefreshListView mPullToRefreshListView;private LinkedList<String> mItemList;private ArrayAdapter<String> adapter;private int first = 0;private int end = 0;private Context context;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);context = this;initData();adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mItemList);//初始化控件mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_list);ListView mListView = mPullToRefreshListView.getRefreshableView();mListView.setAdapter(adapter);mPullToRefreshListView.setMode(Mode.BOTH);mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>(){@Overridepublic void onRefresh(PullToRefreshBase<ListView> refreshView) {new GetDataTask(refreshView,adapter).execute();} }); }private class GetDataTask extends AsyncTask<Void, Void, String[]> {private PullToRefreshBase<ListView> refreshView;private ArrayAdapter<String> adapter;public GetDataTask(PullToRefreshBase<ListView> refreshView,ArrayAdapter<String> adapter) {this.refreshView = refreshView;this.adapter = adapter;}@Overrideprotected String[] doInBackground(Void... params) {// Simulates a background job.try {Thread.sleep(1000);} catch (InterruptedException e) {}return data;}@Overrideprotected void onPostExecute(String[] result) {if (refreshView.isHeaderShown()){Toast.makeText(context, "下拉刷新",Toast.LENGTH_SHORT).show();mItemList.addFirst("first" + first);first++;}else{Toast.makeText(context, "上拉加載更多",Toast.LENGTH_SHORT).show();mItemList.addLast("end" + end);end++;} adapter.notifyDataSetChanged();mPullToRefreshListView.onRefreshComplete();super.onPostExecute(result);}}public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}private void initData(){//初始化數據mItemList = new LinkedList<String>();mItemList.addAll(Arrays.asList(data));}private String[] data = new String[]{"data1","data2","data3","data4","data5","data6","data1","data2","data3","data4","data5","data6"};protected String[] doInBackground(Void... params) {// TODO Auto-generated method stubreturn null;} }運行結果:
總結
以上是生活随笔為你收集整理的android下拉刷新和上拉加载的一个简单库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python脚本在命令行中传递参数(附字
- 下一篇: dos拼接字符串以及截取字符串