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{

Aleph0adapter
= new Aleph0();
int mProgressStatus = 0 ;
ProgressBarprogressBar;
protected void onCreate(BundlesavedInstanceState){
super .onCreate(savedInstanceState);
LinearLayoutsearchLayout
= 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
));
TextViewtextView
= 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);
LinearLayoutloadingLayout
= 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(AbsListViewview,
int firstVisible, int visibleCount, int totalCount){

boolean loadMore = /* maybeaddapadding */
firstVisible
+ visibleCount >= totalCount;

if (loadMore){
adapter.count
+= visibleCount; // oranyotheramount
adapter.notifyDataSetChanged();
}
}

public void onScrollStateChanged(AbsListViewv, int s){}

class Aleph0 extends BaseAdapter{
int count = 40 ; /* startingamount */

public int getCount(){ return count;}
public ObjectgetItem( int pos){ return pos;}
public long getItemId( int pos){ return pos;}

public ViewgetView( int pos,Viewv,ViewGroupp){
TextViewview
= 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{

Aleph0adapter
= new Aleph0();
int mProgressStatus = 0 ;

protected void onCreate(BundlesavedInstanceState){
super .onCreate(savedInstanceState);
LinearLayoutsearchLayout
= new LinearLayout( this );
searchLayout.setOrientation(LinearLayout.HORIZONTAL);

ButtontextView
= 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);
LinearLayoutloadingLayout
= 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(Viewv)
{
adapter.count
+= 10 ;
adapter.notifyDataSetChanged();
}
});


setListAdapter(adapter);
// getListView().setOnScrollListener(this);
}

/* publicvoidonScroll(AbsListViewview,
intfirstVisible,intvisibleCount,inttotalCount){

booleanloadMore=
firstVisible+visibleCount>=totalCount;

if(loadMore){
adapter.count+=visibleCount;
adapter.notifyDataSetChanged();
}
}
*/

public void onScrollStateChanged(AbsListViewv, int s){}

class Aleph0 extends BaseAdapter{
int count = 40 ; /* startingamount */

public int getCount(){ return count;}
public ObjectgetItem( int pos){ return pos;}
public long getItemId( int pos){ return pos;}

public ViewgetView( int pos,Viewv,ViewGroupp){
TextViewview
= new TextView(EndLessActivity. this );
view.setText(
" entry " + pos);
return view;
}
}



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值