如何实现滚动到底部自动加载,以及显示一个 loading cell

 我来答
育知同创教育
2016-08-01 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
public class MainActivity extends Activity {
private ListView listview;
private List<String> datas;
private LayoutInflater inflater;
private MyAdapter adapter;
private List<String> contents;
private int count = 0;
private View footView;
private Handler handler = new Handler();
int lastItem;
private RelativeLayout loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
inflater = LayoutInflater.from(this);
listview = (ListView) findViewById(R.id.listview);
adapter = new MyAdapter();
footView = inflater.inflate(R.layout.footer, null);
loading = (RelativeLayout) footView.findViewById(R.id.loading);
//listview的addFooterView()添加view到listview底部一定要加在listview.setAdapter(adapter);这代码前面
listview.addFooterView(footView);
listview.setAdapter(adapter);
//添加listview滚动监听
listview.setOnScrollListener(new OnScrollListener() {
//AbsListView view 这个view对象就是listview
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
if (view.getLastVisiblePosition() == view.getCount() - 1) {
loadData();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
lastItem = firstVisibleItem + visibleItemCount - 1 ;
}
});
}
protected void loadData() {
loading.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
@Override
public void run() {
load();
loading.setVisibility(View.GONE);
adapter.notifyDataSetChanged();
}
}, 5000);
}
protected void load() {
int count = adapter.getCount() + 1;
for(int i = count; i < count + 20; i++) { //每次加载20条数据
contents.add("加载数据:::" + i);
}
}
private void initData() {
contents = new ArrayList<String>();
for(int i = 1; i < 20; i++){
contents.add("加载数据:::" + i);
}
}

class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return contents.size();
}
@Override
public Object getItem(int position) {
return contents.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.item, parent, false);
holder = new ViewHolder();
holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tvContent.setText(contents.get(position));
return convertView;
}
class ViewHolder{
TextView tvContent;
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式