Android实现滑动加载数据的方法
更新时间:2015年07月21日 17:27:45 作者:华宰
这篇文章主要介绍了Android实现滑动加载数据的方法,实例分析了Android通过滑动实现动态加载数据的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了Android实现滑动加载数据的方法。分享给大家供大家参考。具体实现方法如下:
EndLessActivity.java如下:
package com.ScrollListView; import Android.app.ListActivity; import Android.os.Bundle; import Android.view.Gravity; import Android.view.View; import Android.view.ViewGroup; import Android.widget.AbsListView; import Android.widget.BaseAdapter; import Android.widget.LinearLayout; import Android.widget.ProgressBar; import Android.widget.TextView; import Android.widget.AbsListView.OnScrollListener; public class EndLessActivity extends ListActivity implements OnScrollListener { Aleph0 adapter = new Aleph0(); int mProgressStatus = 0 ; ProgressBar progressBar; protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); LinearLayout searchLayout = new LinearLayout( this ); searchLayout.setOrientation(LinearLayout.HORIZONTAL); progressBar = new ProgressBar( this ); progressBar.setPadding( 0 , 0 , 15 , 0 ); searchLayout.addView(progressBar, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT )); TextView textView = new TextView( this ); textView.setText( " 加载中... " ); textView.setGravity(Gravity.CENTER_VERTICAL); searchLayout.addView(textView, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT )); searchLayout.setGravity(Gravity.CENTER); LinearLayout loadingLayout = new LinearLayout( this ); loadingLayout.addView(searchLayout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT )); loadingLayout.setGravity(Gravity.CENTER); getListView().addFooterView(loadingLayout); setListAdapter(adapter); getListView().setOnScrollListener( this ); } public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) { boolean loadMore = /* maybe add a padding */ firstVisible + visibleCount >= totalCount; if (loadMore) { adapter.count += visibleCount; // or any other amount adapter.notifyDataSetChanged(); } } public void onScrollStateChanged(AbsListView v, int s) { } class Aleph0 extends BaseAdapter { int count = 40 ; /* starting amount */ public int getCount() { return count; } public Object getItem( int pos) { return pos; } public long getItemId( int pos) { return pos; } public View getView( int pos, View v, ViewGroup p) { TextView view = new TextView(EndLessActivity. this ); view.setText( " entry " + pos); return view; } } }
listview下部是按钮控制:
package com.ScrollListView; import Android.app.ListActivity; import Android.os.Bundle; import Android.view.Gravity; import Android.view.View; import Android.view.ViewGroup; import Android.widget.AbsListView; import Android.widget.BaseAdapter; import Android.widget.Button; import Android.widget.LinearLayout; import Android.widget.ProgressBar; import Android.widget.TextView; import Android.widget.AbsListView.OnScrollListener; public class EndLessActivity extends ListActivity { Aleph0 adapter = new Aleph0(); int mProgressStatus = 0 ; protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); LinearLayout searchLayout = new LinearLayout( this ); searchLayout.setOrientation(LinearLayout.HORIZONTAL); Button textView = new Button( this ); textView.setText( " 加载中... " ); textView.setGravity(Gravity.CENTER_VERTICAL); searchLayout.addView(textView, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT )); searchLayout.setGravity(Gravity.CENTER); LinearLayout loadingLayout = new LinearLayout( this ); loadingLayout.addView(searchLayout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT )); loadingLayout.setGravity(Gravity.CENTER); getListView().addFooterView(loadingLayout); textView.setOnClickListener( new Button.OnClickListener() { @Override public void onClick(View v) { adapter.count += 10 ; adapter.notifyDataSetChanged(); } }); setListAdapter(adapter); // getListView().setOnScrollListener(this); } /* public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) { boolean loadMore = firstVisible + visibleCount >= totalCount; if(loadMore) { adapter.count += visibleCount; adapter.notifyDataSetChanged(); } } */ public void onScrollStateChanged(AbsListView v, int s) { } class Aleph0 extends BaseAdapter { int count = 40 ; /* starting amount */ public int getCount() { return count; } public Object getItem( int pos) { return pos; } public long getItemId( int pos) { return pos; } public View getView( int pos, View v, ViewGroup p) { TextView view = new TextView(EndLessActivity. this ); view.setText( " entry " + pos); return view; } } }
希望本文所述对大家的Android程序设计有所帮助。
相关文章
Android关于Glide的使用(高斯模糊、加载监听、圆角图片)
这篇文章主要为大家详细介绍了Android关于Glide的使用,内容丰富,高斯模糊、加载监听、圆角图片希望大家可以掌握,感兴趣的小伙伴们可以参考一下2016-11-11Android编程之listView中checkbox用法实例分析
这篇文章主要介绍了Android编程之listView中checkbox用法,结合实例形式分析了Android中checkbox的页面布局及功能实现相关技巧,需要的朋友可以参考下2016-01-01Kotlin 协程 supervisorScope {} 运行崩溃解决方法
看过很多 supervisorScope {} 文档的使用,我照抄一摸一样的代码,运行就崩溃,最后找到了解决方法,应该是kotlin版本更新做过改动,当前我使用的是 androidx.core:core-ktx:1.9.0,本文给大家介绍Kotlin 协程 supervisorScope {} 运行崩溃解决方法,感兴趣的朋友一起看看吧2024-01-01
最新评论