GridView 滑动到底部时加载新数据
首先是GridView监听Scoll事件,当到达底部是,做相应的操作。
具体的GridViewScoll事件,网络牛人已经写的很好了,转载地http://blog.csdn.net/hellogv/article/details/6615487
View Code
/** * @author admin * GridView 滑动监听器 */ public class GridScrollListener implements OnScrollListener { /** * 到底部时回调函数 */ public interface AutoLoadCallBack { void execute(); } private int getLastVisiblePosition = 0, lastVisiblePositionY = 0; private AutoLoadCallBack mCallback; public GridScrollListener(AutoLoadCallBack callback) { this.mCallback = callback; } public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) { //滚动到底部 if (view.getLastVisiblePosition() == (view.getCount() - 1)) { View v = (View) view.getChildAt(view.getChildCount() - 1); int[] location = new int[2]; v.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标 int y = location[1]; //MyLog.d("x" + location[0], "y" + location[1]); if (view.getLastVisiblePosition() != getLastVisiblePosition && lastVisiblePositionY != y)//第一次拖至底部 { Toast.makeText(view.getContext(), "已经拖动至底部,再次拖动即可翻页", 500).show(); getLastVisiblePosition = view.getLastVisiblePosition(); lastVisiblePositionY = y; return; } else if (view.getLastVisiblePosition() == getLastVisiblePosition && lastVisiblePositionY == y)//第二次托至底部 { mCallback.execute(); } } //未滚动到底部,第二次托至底部都初始化 getLastVisiblePosition = 0; lastVisiblePositionY = 0; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } }
然后是在到达底部时根据回调函数来进行添加新数据
/** * @author admin */ public class GoodsShowView implements InitLayoutI { private static String TAG = "GoodsView"; private View mView; private Activity mActivity; private GridView gridview; private LinearLayout myLayout; private LinearLayout _layout; private LayoutInflater inflater; private GoodsInfo goodsInfo; private static int NumColumns = 2; private List<GoodsInfo> goodsList; public GoodsShowView(View view, Activity activity, LinearLayout layout) { // TODO Auto-generated constructor stub this.mView = view; this.mActivity = activity; inflater = LayoutInflater.from(mActivity); this.myLayout = layout; initLayout(); } @Override public void initLayout() { // TODO Auto-generated method stub System.out.println(TAG + "-----" + "1"); View _view = inflater.inflate(R.layout.goods_show_layout, null); _layout = (LinearLayout) _view.findViewById(R.id.goods_show_layoutID); initWidget(); myLayout.removeAllViews(); myLayout.addView(_layout); } @Override public void initWidget() { // TODO Auto-generated method stub /* 九宫格 */ gridview = (GridView) _layout.findViewById(R.id.MainActivityGrid); goodsList = getData(); MyGridAdapter adapter = new MyGridAdapter(goodsList, mActivity); gridview.setAdapter(adapter); gridview.setNumColumns(NumColumns); gridview.setOnItemClickListener(new gridViewOnClick()); GridScrollListener autoLoadListener = new GridScrollListener(callBack); gridview.setOnScrollListener(autoLoadListener); } @Override public Object initData() { return goodsInfo; // TODO Auto-generated method stub } private List<GoodsInfo> getData() { System.out.println(TAG + "--------加载数据"); List<GoodsInfo> list = new ArrayList<GoodsInfo>(); for (int i = 0; i < 10; i += 1) { goodsInfo = new GoodsInfo(); Random random = new Random(10000); goodsInfo.setGoodsName("物品"); goodsInfo.setGoodsPrice(10000); goodsInfo.setGoodsInfos("物美价廉"); // goodsInfo.setGoodsImage(R.drawable.small); list.add(goodsInfo); } return list; } /** * 翻页下页数据 */ private void getNextData() { // TODO Auto-generated method stub ListAdapter la = gridview.getAdapter(); //新数据源 List<GoodsInfo> list = new ArrayList<GoodsInfo>(); for (int i = 0; i < 10; i += 1) { goodsInfo = new GoodsInfo(); Random random = new Random(10000); goodsInfo.setGoodsName("物品"); goodsInfo.setGoodsPrice(500); goodsInfo.setGoodsInfos("物美价廉"); //注意!添加到原先适配器的List goodsList.add(goodsInfo); } //核心!通知GridView适配器 数据改变 ((MyGridAdapter) la).notifyDataSetChanged(); } private class BtnOnclick implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(mActivity, "点击" + 0, 0).show(); } } private class gridViewOnClick implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub int index = arg2 + 1; Toast.makeText(mActivity, "点击" + index, 0).show(); Intent intent = new Intent(); intent.setClass(mActivity, GoodsActivity.class); mActivity.startActivity(intent); } } /** * 回调函数 */ AutoLoadCallBack callBack = new AutoLoadCallBack() { public void execute() { Toast.makeText(mActivity, "滚动至底部", 500).show(); getNextData(); } }; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步