本文主要是介绍android Listview分批加载+自动加载(改进版)(附源码下载),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这次在代码上比上一次改进了一些,并且加载完全部数据后会在lisview底部显示一个“已加载全部“的textview。大家可以对比我的上一篇博客
android Listview分批加载+自动加载(附源码下载)看看代码有啥不同
直接上代码:
public class TestForListviewActivity extends Activity implementsOnScrollListener {private View mFooterView;private LinearLayout mloadingLinear;//正在加载时显示的viewprivate TextView mLoadFinishTextView;//加载全部数据后显示的viewprivate final int LOAD_STATE_IDLE=0;//没有在加载,并且服务器上还有数据没加载private final int LOAD_STATE_LOADING=1;//正在加载状态private final int LOAD_STATE_FINISH=2;//表示服务器上的全部数据都已加载完毕private int loadState=LOAD_STATE_IDLE;//记录加载的状态private final int MAX_COUNT=15;//表示服务器总共有MAX_COUNT条数据private final int EACH_COUNT=10;//表示每次加载的条数private ListView mListview = null;private PaginationAdapter mAdapter;private Handler handler=new Handler();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mFooterView = getLayoutInflater().inflate(R.layout.loadmore, null);mloadingLinear=(LinearLayout) mFooterView.findViewById(R.id.loading_linear);mLoadFinishTextView=(TextView) mFooterView.findViewById(R.id.load_finish_textview);mListview = (ListView) findViewById(R.id.listview);mListview.addFooterView(mFooterView);// 设置列表底部视图List<News> news=new ArrayList<News>();mAdapter = new PaginationAdapter(news);;mListview.setAdapter(mAdapter);//设置setOnScrollListener会自动调用onscroll方法。mListview.setOnScrollListener(this);}public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {Log.i("onS", "firstVisibleItem"+firstVisibleItem+" visibleItemCount"+visibleItemCount+" totalItemCount"+totalItemCount);if (firstVisibleItem+visibleItemCount==totalItemCount) {if(loadState==LOAD_STATE_IDLE){Log.i("onScroll", "firstVisibleItem"+firstVisibleItem+" visibleItemCount"+visibleItemCount+" totalItemCount"+totalItemCount);loadState=LOAD_STATE_LOADING;loadMore();}}}public void onScrollStateChanged(AbsListView arg0, int scrollState) {Log.i("onScrollStateChanged", scrollState+"");}private void loadMore(){//等待2秒之后才加载,模拟网络等待时间为2shandler.postDelayed(new Runnable() {public void run() {loadData();mAdapter.notifyDataSetChanged();if (loadState==LOAD_STATE_FINISH) {//加载全部数据后在底部显示一个“已加载全部”的textviewmloadingLinear.setVisibility(View.GONE);mLoadFinishTextView.setVisibility(View.VISIBLE);}}},2000);}private void loadData() {int dataIndex;//要加载的数据的index(从0开始)int count = mAdapter.getCount();//如果服务端还有数据,则继续加载更多for (dataIndex = count; dataIndex < Math.min(count+EACH_COUNT, MAX_COUNT); dataIndex++) {News item = new News();item.setTitle("Title" + dataIndex);item.setContent("This is News Content" + dataIndex);mAdapter.addNewsItem(item);}//如果服务器上的全部数据都已加载完毕if (dataIndex==MAX_COUNT) {loadState=LOAD_STATE_FINISH;}else {loadState=LOAD_STATE_IDLE;}}class PaginationAdapter extends BaseAdapter {List<News> newsItems;public PaginationAdapter(List<News> newsitems) {this.newsItems = newsitems;}public int getCount() {return newsItems==null?0:newsItems.size();}public Object getItem(int position) {return newsItems.get(position);}public long getItemId(int position) {return position;}public void addNewsItem(News newsitem) {newsItems.add(newsitem);}public View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {convertView = getLayoutInflater().inflate(R.layout.list_item,null);}// 新闻标题TextView tvTitle = (TextView) convertView.findViewById(R.id.newstitle);tvTitle.setText(newsItems.get(position).getTitle());// 新闻内容TextView tvContent = (TextView) convertView.findViewById(R.id.newscontent);tvContent.setText(newsItems.get(position).getContent());return convertView;}}
}
改进版demo下载:源码下载
这篇关于android Listview分批加载+自动加载(改进版)(附源码下载)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!