listview分页加载和自动加载

//代码体现:

packagecom.example.lenovo.myapplication;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.support.v7.app.AppCompatActivity;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.widget.AbsListView;

importandroid.widget.Button;

importandroid.widget.ListView;

importandroid.widget.ProgressBar;

importandroid.widget.Toast;

importjava.util.ArrayList;

import staticandroid.R.id.list;

public classMainActivityextendsAppCompatActivityimplementsAbsListView.OnScrollListener {

privateListViewlv_main;

privateButtonfooter_btn;

privateProgressBarfooter_pb;

privateHandlerhandler=newHandler();

privateArrayListlist;

privateAdapteradapter;

//最大显示条目

private static final intMAX_COUNT=20;

privateViewfooterview;

private intlastindex;

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private voidinitView() {

lv_main= (ListView) findViewById(R.id.lv_main);

//底布局

footerview= LayoutInflater.from(this).inflate(R.layout.footeritem,null);

footer_btn=footerview.findViewById(R.id.footer_btn);

footer_pb=footerview.findViewById(R.id.footer_pb);

//listview添加底布局

lv_main.addFooterView(footerview);

//设置滑动事件

lv_main.setOnScrollListener(this);

list=newArrayList<>();

for(inti =0; i <10; i++) {

list.add(newCompany("张三"+ i, i +1));

}

adapter=newAdapter(list,this);

lv_main.setAdapter(adapter);

//底布局的监听事件,让按纽隐藏和进度条显示 发送handler消息

footer_btn.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

footer_btn.setVisibility(View.GONE);

footer_pb.setVisibility(View.VISIBLE);

//发送延时消息,不能用handler.postAttime(); 发送延时消息的时候就让按纽显示,进度条再隐藏

yanshi();

}

});

}

public voidyanshi() {

handler.postDelayed(newRunnable() {//handler.sendmessage();发送即时消息

@Override

public voidrun() {

footer_btn.setVisibility(View.VISIBLE);

footer_pb.setVisibility(View.GONE);

intcount =adapter.getCount();

Toast.makeText(MainActivity.this, count +"", Toast.LENGTH_SHORT).show();

//判断刷新几条和最大条目数比较

if(count +5

for(inti = count; i < count +5; i++) {

list.add(newCompany("李四"+ i, i +1));

}

}else{

for(inti = count; i

list.add(newCompany("李四"+ i, i +1));

}

footer_btn.setVisibility(View.GONE);

}

adapter.notifyDataSetChanged();

//这里count是int值,如果不加空字符串的话,会报资源没有找到的异常

}

},1000);

}

@Override

public voidonScrollStateChanged(AbsListView view,intscrollState) {

/*scrollState有三种状态,分别是SCROLL_STATE_IDLE、SCROLL_STATE_TOUCH_SCROLL、SCROLL_STATE_FLING

SCROLL_STATE_IDLE是当屏幕停止滚动时

SCROLL_STATE_TOUCH_SCROLL是当用户在以触屏方式滚动屏幕并且手指仍然还在屏幕上时(The user is scrolling using touch, and their finger is still on the screen)

SCROLL_STATE_FLING是当用户由于之前划动屏幕并抬起手指,屏幕产生惯性滑动时(The user had previously been scrolling using touch and had performed a fling)

*

*

*

* */

if(scrollState ==SCROLL_STATE_IDLE&&lastindex==adapter.getCount()) {

footer_btn.setVisibility(View.GONE);

footer_pb.setVisibility(View.VISIBLE);

yanshi();

}

}

@Override//自身,第一条显示的条目 ,可显示的条目数,总的条目数

public voidonScroll(AbsListView view,intfirstVisibleItem,intvisibleItemCount,inttotalItemCount) {

lastindex= firstVisibleItem + visibleItemCount -1;

if(totalItemCount ==MAX_COUNT+1) {

lv_main.removeFooterView(footerview);

Toast.makeText(this,"已经到底了", Toast.LENGTH_SHORT).show();

}

}

}

你可能感兴趣的:(listview分页加载和自动加载)