找回密码
 立即注册
theListViewLoadingonScroll | android开发 2022-05-27 133 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
源代码介绍
安卓市场的软件列表中,每页显示10条记录,并且没有上一页和下一页的按钮。数据通过手动滑动动态加载。向下滚动时,Loading... 显示在底部。数据加载结束后,Loading 底部栏消失。关于ListView的分段显示,有现成的库可用,比如cwac-endless。这个库的缺点是不能自定义底部加载的View。 google code上还有一个androidpageablelistview,可以实现基本的分页,有按钮可以手动显示上一页和下一页。查阅了很多资料,发现ListView其实自带了实现分页加载的方法,使用addFooterView/removeView这两个函数来“添加”或“删除” ListView 页脚。下面是学习ListView分页时做的一个demo。它相对简单。我会把我的源代码分享给你,希望对你有所帮助。

实现思路如下:使用onScroll方法实现“滑动”;后处理检查是否有新记录。如果有,调用addFooterView将记录添加到适配器,适配器调用notifyDataSetChanged更新数据;如果没有记录,则移除自定义的 mFooterView。 onScrollStateChanged 函数这里没有重写,所以需要一个外部变量 mFirstCell 来记录 onScroll 中的滑动位置。
备注:这里只是一个简单的Demo,目的是帮助理解流程和相关方法的使用(主要是onScroll方法的使用),Demo中Adapter中的数据直接使用程序中写的数据,所以向下滑动的方法可能我看到的Loading加载条不是很明显,所以特地写了一个Toast提醒。如果是商业级应用,AsyncTask一般是用来和服务器通信获取数据,所以效果会很明显(可以看各大软件商店,比如:google android store,友易市场(eoemarket) 、爱米软件商店等)。

(Source code introduction
In the software list of the Android Market, 10 records are displayed on each page, and there are no buttons for the previous page and the next page. Data is dynamically loaded by manual sliding. When scrolling down, loading Displayed at the bottom. After data loading, the loading bottom column disappears. For the segmented display of listview, there are ready-made libraries available, such as CWAC endless. The disadvantage of this library is that the view loaded at the bottom cannot be customized. There is also an androidpageablelistview on Google Code, which can realize basic paging. There are buttons to manually display the previous page and the next page. After consulting a lot of materials, I found that listview actually has its own method for page loading. The two functions addfooterview/removeview are used to "add" or "delete" the listview footer. The following is a demo for learning listview paging. It is relatively simple. I will share my source code with you, hoping to help you.
The implementation idea is as follows: use onscroll method to realize "sliding"; Post processing check whether there are new records. If yes, call addfooterview to add the record to the adapter, and the adapter calls notifydatasetchanged to update the data; If there are no records, remove the customized mfooterview. The onscrollstatechanged function is not overridden here, so an external variable mfirstcell is required to record the sliding position in onscroll.
Note: This is just a simple demo to help understand the process and the use of related methods (mainly the use of onscroll method). The data in the adapter in the demo directly uses the data written in the program, so the downward sliding method may not be obvious in the loading bar I see, so a toast reminder is specially written. For commercial applications, asynctask is usually used to communicate with the server to obtain data, so the effect will be obvious (you can see the major software stores, such as Google Android store, eoemarket, Amy software store, etc.).)

[下载]20222185784.rar


202220ryepqj4ct0fpc7qu.jpg


上一篇:Android i-jetty开源项目
下一篇:Android Mp3标签提取器源码