欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现ListView滚动提示等待框功能示例

程序员文章站 2023-12-20 20:15:16
本文实例讲述了android编程实现listview滚动提示等待框功能。分享给大家供大家参考,具体如下: 其实原理很简单,只需要设置监听listview的滚动事件即可...

本文实例讲述了android编程实现listview滚动提示等待框功能。分享给大家供大家参考,具体如下:

其实原理很简单,只需要设置监听listview的滚动事件即可

file1:

package cn.stay.activity;
import java.util.arraylist;
import java.util.list;
import com.aoran.r;
import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.widget.abslistview;
import android.widget.abslistview.onscrolllistener;
import android.widget.linearlayout;
import android.widget.listview;
import android.widget.progressbar;
/**
 * @author riddlezhang
 * 滚动listview提示等待框
 */
public class listview_sep extends activity implements onscrolllistener {
  private list<string> list = new arraylist<string>();
  private listview listv01;
  private myadapter adapter;
  private int current_page = 2;
  progressbar progressbar;
  private linearlayout bottom_linear;
  private int lastitem;
  @override
  protected void oncreate(bundle savedinstancestate) {
    // todo auto-generated method stub
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    listv01 = (listview) findviewbyid(r.id.listv01);
    bottom_linear=(linearlayout) findviewbyid(r.id.bottom_linear);
    for (int i = 0; i < 100; i++) {
      list.add("i="+i);
    }
    adapter = new myadapter(listview_sep.this, list);
    listv01.setadapter(adapter);
//   listv01.setselection(getsingerstrs().size()-1); 设置每次最后一行被选中
    listv01.setonscrolllistener(this);
  }
  @override
  public void onscrollstatechanged(abslistview view, int scrollstate) {
    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
  public void onscroll(abslistview view, int firstvisibleitem,
      int visibleitemcount, int totalitemcount) {
      lastitem=firstvisibleitem+visibleitemcount;
  }
}

file2:

package cn.stay.activity;
import java.util.list;
import com.aoran.r;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.textview;
public class myadapter extends baseadapter{
  private context mcontext;
  private list<string> list;
  public int count=20;
  private viewholder holder;
  public myadapter(context mcontext,list<string> list) {
    this.mcontext=mcontext;
    this.list=list;
  }
  @override
  public int getcount() {
    return count;
  }
  @override
  public object getitem(int position) {
    // todo auto-generated method stub
    return list.get(position);
  }
  @override
  public long getitemid(int position) {
    return position;
  }
  @override
  public view getview(int position, view convertview, viewgroup parent) {
    // todo auto-generated method stub
    if(convertview==null || convertview.gettag()==null){
      convertview=layoutinflater.from(mcontext).inflate(r.layout.adapter,null);
      holder = new viewholder();
      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));
    return convertview;
  }
  private class viewholder{
    private textview adapter_tv;
  }
}

file3:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns: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"
  >
  <linearlayout android:id="@+id/bottom_linear" android:layout_alignparentbottom="true" android:layout_width="fill_parent" android:layout_height="50dip" android:gravity="center" android:visibility="gone">
    <progressbar android:layout_width="24dip" android:layout_height="24dip" android:layout_marginleft="20dip" />
    <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="正在加载,请稍后。。。" android:textcolor="#ffffff" android:textsize="20sp" android:gravity="center"/>
  </linearlayout>
  <listview android: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程序设计有所帮助。

上一篇:

下一篇: