实现ListView滑动到底部自动加载更多
原理:监听ListView的滑动事件,判断ListView是否滑动到底部,然后去加载数据。
布局文件:activity_test_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="cn.sizebook.activities.measure.userdefine.TestMainActivity"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </android.support.constraint.ConstraintLayout>
布局文件:item.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="45dp" android:textSize="16sp" android:text="测试" android:gravity="center_vertical" android:id="@+id/tvContent"/> </android.support.constraint.ConstraintLayout>
Activity文件
package cn.sizebook.activities.measure.userdefine; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import cn.sizebook.R; public class TestMainActivity extends AppCompatActivity { private ListView listview; private LayoutInflater inflater; private MyAdapter adapter; private List<String> contents; private Handler handler = new Handler(); int lastItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_main); initData(); inflater = LayoutInflater.from(this); listview = (ListView) findViewById(R.id.listview); adapter = new MyAdapter(); listview.setAdapter(adapter); //添加listview滚动监听 listview.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == AbsListView.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() { load(); adapter.notifyDataSetChanged(); } 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; } } }