网友通过本文主要向大家介绍了pulltorefresh使用,pulltorefresh,pulltorefresh下载,pulltorefresh.jar,pulltorefresh.js等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android pulltorefresh使用,androidpulltorefresh
pulltorefresh插件可以轻松实现上拉下拉刷新,github.com上直接搜索进行下载。
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ptr="http://schemas.android.com/apk/res-auto" 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.PullToRefreshListView android:id="@+id/pull_to_refresh_listview" android:layout_height="fill_parent" android:layout_width="fill_parent" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="@android:color/transparent" ptr:ptrHeaderTextColor="#919191" /> </RelativeLayout>
核心代码如下,这里用到了Android当中不少的知识点,其中主要的是异步任务处理、自定义适配器。
package com.example.pulltorefresh; import java.util.ArrayList; import com.handmark.pulltorefresh.library.ILoadingLayout; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MainActivity extends Activity { private PullToRefreshListView pullToRefreshView; private ArrayList<Music> musics=new ArrayList<Music>(); private DataAdapter dataAdapter; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pullToRefreshView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview); pullToRefreshView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() { @Override public void onPullDownToRefresh(PullToRefreshBase refreshView) { // TODO 自动生成的方法存根 new DataAsyncTask(MainActivity.this).execute(); } @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { // TODO 自动生成的方法存根 new DataAsyncTask(MainActivity.this).execute(); } }); pullToRefreshView.setMode(PullToRefreshBase.Mode.BOTH); ILoadingLayout startLabels = pullToRefreshView .getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新...");// 刚下拉时,显示的提示 startLabels.setRefreshingLabel("正在载入...");// 刷新时 startLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时,显示的提示 ILoadingLayout endLabels = pullToRefreshView.getLoadingLayoutProxy( false, true); endLabels.setPullLabel("上拉刷新...");// 刚下拉时,显示的提示 endLabels.setRefreshingLabel("正在载入...");// 刷新时 endLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时,显示的提示 loadData(); dataAdapter=new DataAdapter(this, musics); pullToRefreshView.setAdapter(dataAdapter); } @Override 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 int count; private void loadData(){ for(int i=0;i<10;i++){ musics.add(new Music("歌曲-"+count,"歌手-"+count)); count++; } } static class DataAsyncTask extends AsyncTask<Void, Void, String>{ private MainActivity mainActivity; public DataAsyncTask(MainActivity mainActivity){ this.mainActivity=mainActivity; } @Override protected String doInBackground(Void... arg0) { try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } mainActivity.loadData(); return "success"; } @Override protected void onPostExecute(String result) { // TODO 自动生成的方法存根 super.onPostExecute(result); if("success".equals(result)){ mainActivity.dataAdapter.notifyDataSetChanged(); mainActivity.pullToRefreshView.onRefreshComplete(); } } } static class DataAdapter extends BaseAdapter{ private Context ctx; private ArrayList<Music> musics;