GitHub开源下拉刷新Android-PullToRefresh使用总结

最近有从GitHub上Clone【Android-PullToRefresh】这个工程下来学习,感觉效果不错,然后希望能够整合到现在正在Coding的工程中,有遇到几个问题,在这里总结下,希望能给大家一点借鉴。
1、虽然我将Demo工程中用到的几个jar包拷贝到自己的工程libs目录下,但是创建jar包中对象的时候还是报了classNotFound的错误。后面我把Android-PullToRefresh这个工程拷贝到和我当前工程同一级目录下面然后在project.properties中添加如下代码:
[Java]  纯文本查看  复制代码
?
1
2
3
4
target=android- 8
android.library.reference. 1 =../Android-PullToRefresh/library
#android.library.reference. 2 =../Android-PullToRefresh/extras/PullToRefreshListFragment
#android.library.reference. 3 =../Android-PullToRefresh/extras/PullToRefreshViewPager


因为我只用到了PullToRefreshListView 所以我把下面两个注释掉了,还有如果不注释掉下面两个的话,Eclipse无法识别到我当前目录下libs里面所有的jar包,暂时还不知道原因,因为这个开源项目本身是用maven配置的,但是我搜索了1天,还是没有解决maven配置的问题,如果有人知道可以留言给我,告诉我下如何用Eclipse下的maven插件生成合适的pom.xml。十分感谢

上面操作之后可以解决classNotFound的问题。编译通过,将Demo中的代码片拷贝到我自己的工程中也可以顺利跑通。

下面是源码


[Java]  纯文本查看  复制代码
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
public final class PullToRefreshListActivity extends ListActivity {
 
     static final int MENU_MANUAL_REFRESH = 0 ;
     static final int MENU_DISABLE_SCROLL = 1 ;
     static final int MENU_SET_MODE = 2 ;
     static final int MENU_DEMO = 3 ;
 
     private LinkedList<String> mListItems;
     private PullToRefreshListView mPullRefreshListView;
     private ArrayAdapter<String> mAdapter;
 
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_ptr_list);
 
         mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
 
         // Set a listener to be invoked when the list should be refreshed.
         mPullRefreshListView.setOnRefreshListener( new OnRefreshListener<ListView>() {
             @Override
             public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
                         DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
 
                 // Update the LastUpdatedLabel
                 refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
 
                 // Do work to refresh the list here.
                 new GetDataTask().execute();
             }
         });
 
         // Add an end-of-list listener
         mPullRefreshListView.setOnLastItemVisibleListener( new OnLastItemVisibleListener() {
 
             @Override
             public void onLastItemVisible() {
                 Toast.makeText(PullToRefreshListActivity. this , "End of List!" , Toast.LENGTH_SHORT).show();
             }
         });
 
         ListView actualListView = mPullRefreshListView.getRefreshableView();
 
         // Need to use the Actual ListView when registering for Context Menu
         registerForContextMenu(actualListView);
 
         mListItems = new LinkedList<String>();
         mListItems.addAll(Arrays.asList(mStrings));
 
         mAdapter = new ArrayAdapter<String>( this , android.R.layout.simple_list_item_1, mListItems);
 
         /**
          * Add Sound Event Listener
          */
         SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>( this );
         soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
         soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
         soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
         mPullRefreshListView.setOnPullEventListener(soundListener);
 
         // You can also just use setListAdapter(mAdapter) or
         // mPullRefreshListView.setAdapter(mAdapter)
         actualListView.setAdapter(mAdapter);
     }
 
     private class GetDataTask extends AsyncTask<Void, Void, String[]> {
 
         @Override
         protected String[] doInBackground(Void... params) {
             // Simulates a background job.
             try {
                 Thread.sleep( 4000 );
             } catch (InterruptedException e) {
             }
             return mStrings;
         }
 
         @Override
         protected void onPostExecute(String[] result) {
             mListItems.addFirst( "Added after refresh..." );
             mAdapter.notifyDataSetChanged();
 
             // Call onRefreshComplete when the list has been refreshed.
             mPullRefreshListView.onRefreshComplete();
 
             super .onPostExecute(result);
         }
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         menu.add( 0 , MENU_MANUAL_REFRESH, 0 , "Manual Refresh" );
         menu.add( 0 , MENU_DISABLE_SCROLL, 1 ,
                 mPullRefreshListView.isScrollingWhileRefreshingEnabled() ? "Disable Scrolling while Refreshing"
                         : "Enable Scrolling while Refreshing" );
         menu.add( 0 , MENU_SET_MODE, 0 , mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
                 : "Change to MODE_PULL_BOTH" );
         menu.add( 0 , MENU_DEMO, 0 , "Demo" );
         return super .onCreateOptionsMenu(menu);
     }
 
     @Override
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
         AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
 
         menu.setHeaderTitle( "Item: " + getListView().getItemAtPosition(info.position));
         menu.add( "Item 1" );
         menu.add( "Item 2" );
         menu.add( "Item 3" );
         menu.add( "Item 4" );
 
         super .onCreateContextMenu(menu, v, menuInfo);
     }
 
     @Override
     public boolean onPrepareOptionsMenu(Menu menu) {
         MenuItem disableItem = menu.findItem(MENU_DISABLE_SCROLL);
         disableItem
                 .setTitle(mPullRefreshListView.isScrollingWhileRefreshingEnabled() ? "Disable Scrolling while Refreshing"
                         : "Enable Scrolling while Refreshing" );
 
         MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
         setModeItem.setTitle(mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_FROM_START"
                 : "Change to MODE_PULL_BOTH" );
 
         return super .onPrepareOptionsMenu(menu);
     }
 
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
 
         switch (item.getItemId()) {
             case MENU_MANUAL_REFRESH:
                 new GetDataTask().execute();
                 mPullRefreshListView.setRefreshing( false );
                 break ;
             case MENU_DISABLE_SCROLL:
                 mPullRefreshListView.setScrollingWhileRefreshingEnabled(!mPullRefreshListView
                         .isScrollingWhileRefreshingEnabled());
                 break ;
             case MENU_SET_MODE:
                 mPullRefreshListView.setMode(mPullRefreshListView.getMode() == Mode.BOTH ? Mode.PULL_FROM_START
                         : Mode.BOTH);
                 break ;
             case MENU_DEMO:
                 mPullRefreshListView.demo();
                 break ;
         }
 
         return super .onOptionsItemSelected(item);
     }
 
     private String[] mStrings = { "Abbaye de Belloc" , "Abbaye du Mont des Cats" , "Abertam" , "Abondance" , "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" ,
             "Allgauer Emmentaler" };
}


对应的布局文件

[XHTML]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent"
     android:orientation = "vertical" >
 
<!--     The PullToRefreshListView replaces a standard ListView widget. -->
 
     < 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" />
 
</ LinearLayout >


2、我是只要实现上拉刷新,然后加载完所有数据后要使上拉刷新失效

根据源码我做了部分修改来适应我的需求,修改了提示字体的颜色,还有左边那个图标,直接在xml 里面修改比较方便所以就这样处理了

[XHTML]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
<? xml version = "1.0" encoding = "UTF-8" ?>
< LinearLayout 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:orientation = "vertical"
     android:background = "@color/activity_white_bk" >
     < include layout = "@layout/titlebar_layout" />
     < com.handmark.pulltorefresh.library.PullToRefreshListView
         xmlns:ptr = "http://schemas.android.com/apk/res-auto"
         ptr:ptrMode = "pullUpFromBottom"
         ptr:ptrHeaderTextColor = "@color/silver_gray"
         ptr:ptrHeaderSubTextColor = "@color/orange"
         ptr:ptrDrawable = "@drawable/share_to_time_line_icon"
         android:id = "@id/order_listview"
         android:cacheColorHint = "@android:color/transparent"
         android:descendantFocusability = "blocksDescendants"
         android:divider = "@color/listitem_content_line"
         android:dividerHeight = "0.5dip"
         android:layout_width = "match_parent"
         android:layout_height = "match_parent" />
</ LinearLayout >


代码部分的修改如下:

在我自己的Activity中添加了

[Java]  纯文本查看  复制代码
?
1
2
//当前默认的滑动模式
private Mode mDefaultMode;


findViewById初始化完成后就调用

[Java]  纯文本查看  复制代码
?
1
2
//获取当前的模式
mDefaultMode=mListview.getMode();


如果加载完所有数据后,我们可以通过

[Java]  纯文本查看  复制代码
?
1
2
3
//这样可以使滑动失效
mListview.setMode(Mode.DISABLED);
//如果需要重新刷新的话 我们可以通过之前保存的Mode对象,重新设置一下就OK

你可能感兴趣的:(开源项目,下拉刷新)