Android下拉刷新ListView设计

来源:互联网 发布:网狐手机棋牌游戏源码 编辑:程序博客网 时间:2024/05/17 03:12

首先我们需要导入一个开源库,利用它里面的自定义控件

到:https://github.com/chrisbanes/Android-PullToRefresh下载Zip包

导入zip包中的library文件夹到工作空间,方法,第一,解压libiray文件夹到磁盘某一位置,然后导入此文件夹的源码

在本项目中导入外部项目作为库


在项目文件夹上右键,properties-android,点add,选择库项目,然后点apply即可

导入之后,就可以用这个开源项目中的类文件和图片了

新建一个activity,在里面放个自定义控件


 <com.handmark.pulltorefresh.library.PullToRefreshListView        android:id="@+id/pull_refresh_list"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:cacheColorHint="#00000000"        android:divider="#19000000"        android:dividerHeight="4dp"        android:fadingEdge="none"        android:fastScrollEnabled="false"        android:footerDividersEnabled="false"        android:headerDividersEnabled="false"        android:smoothScrollbar="true" /> 

然后按照下例来写就好了,代码不长

package com.handmark.pulltorefresh.samples;import java.util.ArrayList;import java.util.Arrays;import java.util.LinkedList;import java.util.List;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import android.app.ListActivity;import android.os.AsyncTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.format.DateUtils;import android.view.Menu;import android.view.MenuItem;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;public class TestActivity extends ListActivity {private LinkedList<String> mListItems;private PullToRefreshListView mPullRefreshListView;private ArrayAdapter<String> mAdapter;//每行显示的数据 private String[] mStrings = { "这是第一个", "这是第二个", "这是第三个", "这是第四个", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre","Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre","这是最后一个" };public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_ptr_list);                //抓出自定义控件mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);                <pre name="code" class="java">                //一个linkedList<String>,相当于arrayList,用于向适配器传输数据mListItems = new LinkedList<String>();mListItems.addAll(Arrays.asList(mStrings));//把一个上面那个数据数组转换成list        //生成一个数组适配器        mAdapter = new ArrayAdapter<String>(TestActivity.this, android.R.layout.simple_list_item_1, mListItems);        //从自定义控件里抽出他的listView        ListView actualListView = mPullRefreshListView.getRefreshableView();        // 需要真实的listView when registering for Context Menu        registerForContextMenu(actualListView);        // 添加一个list结尾监听器                mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {                    @Override                    public void onLastItemVisible() {                        //显示已经到了最后一行了                        Toast.makeText(TestActivity.this, "到底啦!", Toast.LENGTH_SHORT).show();                        //在底部再加一条                        new Thread(){                            public void run() {                                try {                                    Thread.sleep(4000);                                } catch (InterruptedException e) {                                }                                mListItems.addLast("刷新后出现在结尾");//一个linkedList,再刷新后在头部加一条                                handler.sendEmptyMessage(0);                            };                        }.start();                    }                });                actualListView.setAdapter(mAdapter);//给内置listView设置适配器    }        


但是我们并不常用listView做下拉刷新,因为这样会让头部变得很不灵活,解决方法是给scrollView设置下拉刷新,方法和listView差不多

首先使用自定义scrollView

 <com.handmark.pulltorefresh.library.PullToRefreshScrollView        xmlns:ptr="http://schemas.android.com/apk/res-auto"        android:id="@+id/pull_refresh_scrollview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        ptr:ptrAnimationStyle="flip"        ptr:ptrMode="both" >        <TextView            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:padding="8dp"            android:text="@string/filler_text"            android:textSize="16sp" />    </com.handmark.pulltorefresh.library.PullToRefreshScrollView>

进入activity进行配置

/******************************************************************************* * Copyright 2011, 2012 Chris Banes. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/package com.handmark.pulltorefresh.samples;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.widget.ScrollView;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import com.handmark.pulltorefresh.library.PullToRefreshScrollView;public final class PullToRefreshScrollViewActivity extends Activity {PullToRefreshScrollView mPullRefreshScrollView;ScrollView mScrollView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_ptr_scrollview);mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview);mPullRefreshScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {@Overridepublic void onRefresh(PullToRefreshBase<ScrollView> refreshView) {new GetDataTask().execute();}});mScrollView = mPullRefreshScrollView.getRefreshableView();}private class GetDataTask extends AsyncTask<Void, Void, String[]> {@Overrideprotected String[] doInBackground(Void... params) {// Simulates a background job.try {Thread.sleep(4000);} catch (InterruptedException e) {}return null;}@Overrideprotected void onPostExecute(String[] result) {// Do some stuff here// Call onRefreshComplete when the list has been refreshed.mPullRefreshScrollView.onRefreshComplete();super.onPostExecute(result);}}}



0 0