zoukankan      html  css  js  c++  java
  • android 滑动加载数据

    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(
    00150);
                  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; 
            } 
        } 



    }
  • 相关阅读:
    线上redis禁止使用keys等时间复杂度高的命令
    组合索引的使用效果的总结
    Netty 断线重连解决方案
    可作为GC Root的对象
    在同一个sqlSession执行一个相同的查询时,Mybatis有一级缓存,不会去查数据库,由此引发的一个bug
    HashMap 和 currentHashMap JDK8总结
    Java程序导致服务器CPU占用率过高的问题排除过程
    一条sql执行的很慢的原因有哪些
    主键索引和非主键索引的区别
    黑马程序员
  • 原文地址:https://www.cnblogs.com/tt_mc/p/1800722.html
Copyright © 2011-2022 走看看