Android编程实现ListView滚动提示等待框功能示例
本文实例讲述了Android编程实现ListView滚动提示等待框功能。分享给大家供大家参考,具体如下:
其实原理很简单,只需要设置监听listview的滚动事件即可
file1:
packagecn.stay.activity; importjava.util.ArrayList; importjava.util.List; importcom.aoran.R; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.AbsListView; importandroid.widget.AbsListView.OnScrollListener; importandroid.widget.LinearLayout; importandroid.widget.ListView; importandroid.widget.ProgressBar; /** *@authorriddlezhang *滚动listview提示等待框 */ publicclassListView_sepextendsActivityimplementsOnScrollListener{ privateList<String>list=newArrayList<String>(); privateListViewlistv01; privateMyAdapteradapter; privateintcurrent_page=2; ProgressBarprogressBar; privateLinearLayoutbottom_linear; privateintlastItem; @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onCreate(savedInstanceState); setContentView(R.layout.main); listv01=(ListView)findViewById(R.id.listv01); bottom_linear=(LinearLayout)findViewById(R.id.bottom_linear); for(inti=0;i<100;i++){ list.add("i="+i); } adapter=newMyAdapter(ListView_sep.this,list); listv01.setAdapter(adapter); //listv01.setSelection(getSingerStrs().size()-1);设置每次最后一行被选中 listv01.setOnScrollListener(this); } @Override publicvoidonScrollStateChanged(AbsListViewview,intscrollState){ if(lastItem==adapter.count&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){ if((list.size()-current_page*10)>0){ bottom_linear.setVisibility(View.VISIBLE); if((list.size()-current_page*10)>=10){ adapter.count+=10; }else{ adapter.count+=(list.size()-current_page*10); } current_page++; adapter.notifyDataSetChanged(); //bottom_linear.setVisibility(View.GONE); } } } @Override publicvoidonScroll(AbsListViewview,intfirstVisibleItem, intvisibleItemCount,inttotalItemCount){ lastItem=firstVisibleItem+visibleItemCount; } }
file2:
packagecn.stay.activity; importjava.util.List; importcom.aoran.R; importandroid.content.Context; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.BaseAdapter; importandroid.widget.TextView; publicclassMyAdapterextendsBaseAdapter{ privateContextmContext; privateList<String>list; publicintcount=20; privateViewHolderholder; publicMyAdapter(ContextmContext,List<String>list){ this.mContext=mContext; this.list=list; } @Override publicintgetCount(){ returncount; } @Override publicObjectgetItem(intposition){ //TODOAuto-generatedmethodstub returnlist.get(position); } @Override publiclonggetItemId(intposition){ returnposition; } @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ //TODOAuto-generatedmethodstub if(convertView==null||convertView.getTag()==null){ convertView=LayoutInflater.from(mContext).inflate(R.layout.adapter,null); holder=newViewHolder(); holder.adapter_tv=(TextView)convertView.findViewById(R.id.adapter_tv); convertView.setTag(holder); }else{ holder=(ViewHolder)convertView.getTag(); } holder.adapter_tv.setText(list.get(position)); returnconvertView; } privateclassViewHolder{ privateTextViewadapter_tv; } }
file3:
main.xml
<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/project_main_bg" > <LinearLayoutandroid:id="@+id/bottom_linear"android:layout_alignParentBottom="true"android:layout_width="fill_parent"android:layout_height="50dip"android:gravity="center"android:visibility="gone"> <ProgressBarandroid:layout_width="24dip"android:layout_height="24dip"android:layout_marginLeft="20dip"/> <TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="正在加载,请稍后。。。"android:textColor="#FFFFFF"android:textSize="20sp"android:gravity="center"/> </LinearLayout> <ListViewandroid:id="@+id/listv01"android:background="@null"android:layout_above="@id/bottom_linear"android:layout_width="fill_parent"android:layout_height="wrap_content"android:listSelector="#00000000"android:cacheColorHint="#00000000"/> </RelativeLayout>
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。