1、工程加入以下两个文件夹:(参考:https://github.com/jingchenUSTC/PullToRefreshAndLoad) (待会我会将demo打包上传)
2、这个demo只有一个activity,该activity就是显示一个支持上拉加载更多的gridview:
1 package com.example.test_textview; 2 3 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import com.jingchen.pulltorefresh.MyListener; 9 import com.jingchen.pulltorefresh.PullToRefreshLayout; 10 11 import android.app.Activity; 12 import android.content.Context; 13 import android.os.Bundle; 14 import android.os.Handler; 15 import android.widget.GridView; 16 import android.widget.ListView; 17 18 public class MainActivity extends Activity { 19 20 21 public String tag = "MainActivity"; 22 23 public static Context ctxt = null; 24 public static Handler handler = null; 25 26 public GridView gridview = null; 27 public girdviewAdapter adapter = null; 28 public List<Integer> listGridview = null; 29 30 public static int index = 0; 31 32 public static final int UPDATE_GRIDVIEW_LOAD_MORE = 0; 33 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 38 setContentView(R.layout.activity_main); 39 40 init(); 41 42 widgetConfigure(); 43 44 } 45 46 47 public void init() 48 { 49 ctxt = getApplicationContext(); 50 51 gridview = (GridView)findViewById(R.id.gridview_test); 52 53 handler = new Handler() 54 { 55 public void handleMessage(android.os.Message msg) 56 { 57 switch(msg.what) 58 { 59 case UPDATE_GRIDVIEW_LOAD_MORE: 60 61 index++; 62 for(int k=0;k<12;k++) 63 { 64 int t = 12*index+k; 65 listGridview.add(t); } 68 69
adapter.notifyDataSetChanged();
gridview.smoothScrollToPosition(adapter.getCount()); 70 71 break; 72 } 73 }; 74 }; 75 76 listGridview = new ArrayList<Integer>(); 77 for(int k=0;k<12;k++) 78 { 79 listGridview.add(k); 80 } 81 adapter = new girdviewAdapter(listGridview,ctxt,handler); 82 83 84 } 85 86 public void widgetConfigure() 87 { 88 gridview.setAdapter(adapter); 89 90 ((PullToRefreshLayout) findViewById(R.id.refresh_view)).setOnRefreshListener(new MyListener()); 91 92 } 93 }
3、activity_main xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:background="@color/Constrast_3" 5 android:orientation="vertical" 6 android:padding="20dp" > 7 8 <com.jingchen.pulltorefresh.PullToRefreshLayout 9 android:id="@+id/refresh_view" <!-- 这个自定义布局,可以通过检测手势,判断是上拉还是下拉,然后将结果告诉listener--> 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:layout_alignParentTop="true" 13 > 14 15 <include layout="@layout/refresh_head" /> <!-- 这个是下拉刷新的头,引用的1中git的文件--> 16 17 <com.jingchen.pulltorefresh.pullableview.PullableGridView 18 android:id="@+id/gridview_test" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:numColumns="4" 22 android:verticalSpacing="12dp" 23 android:horizontalSpacing="14dp" 24 android:gravity="center" 25 /> 26 27 <include layout="@layout/load_more" /> <!-- 这个是上拉加载更多的尾部,引用的1中git的文件-->
28 29 </com.jingchen.pulltorefresh.PullToRefreshLayout> 30 31 </LinearLayout>
3、refreshView通过检测手势,判断用户操作是上拉还是下拉,进而通过listener来进行相应的数据操作:
1 package com.jingchen.pulltorefresh; 2 3 import android.os.Handler; 4 import android.os.Message; 5 6 import com.example.test_textview.MainActivity; 7 import com.jingchen.pulltorefresh.PullToRefreshLayout.OnRefreshListener; 8 9 public class MyListener implements OnRefreshListener 10 { 11 12 @Override 13 public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) 14 { 15 16 pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED); 17 18 /* // 下拉刷新操作 19 new Handler() 20 { 21 @Override 22 public void handleMessage(Message msg) 23 { 24 // 千万别忘了告诉控件刷新完毕了哦! 25 pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED); 26 } 27 }.sendEmptyMessageDelayed(0, 5000);*/ 28 } 29 30 @Override 31 public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) 32 { 33 34 35 // 加载操作 36 new Handler() 37 { 38 @Override 39 public void handleMessage(Message ms1g) 40 { 41 // 千万别忘了告诉控件加载完毕了哦! 42 pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED); 43 44 Message msg = new Message(); 45 msg.what = MainActivity.UPDATE_GRIDVIEW_LOAD_MORE; 46 MainActivity.handler.sendMessage(msg); 47 } 48 }.sendEmptyMessageDelayed(0, 5000); 49 50 } 51 52 }
需要看demo整个工程的请跳转:http://pan.baidu.com/s/1hqjci6w
时间: 2024-10-11 20:28:57