一:加载中loading.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加载中..."
android:gravity="center"
android:padding="5dp"/>
</LinearLayout>
二:主界面布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
三:主界面调用
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter adapter;
private List list;
private View footView;
private TextView footText;
private int count = 0; // 模拟数据条数
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
listView = findViewById(R.id.listView);
// 如果不添加footView,最后一项未完全显示就会触发触底事件,此时如果展示的item高度比较高对用户体验很不友好
footView = View.inflate(MainActivity.this, R.layout.loading, null);
footText = footView.findViewById(R.id.loading);
listView.addFooterView(footView);
// listView触底事件
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
// 滚动停止时
case SCROLL_STATE_IDLE:
// 判断是否滚动到底部
if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
if (count < 50) {
for (int i = 0; i < 10; i++) {
list.add("新增:" + (count + i + 1));
}
count = list.size();
// 数据变化后通知adapter
adapter.notifyDataSetChanged();
} else {
// 模拟没有跟多数据了
footText.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "已经是最后一页了", Toast.LENGTH_SHORT).show();
}
}
break;
default:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
@Override
protected void onStart() {
super.onStart();
getData();
}
private void getData() {
// 模拟数据
list = new ArrayList();
for (int i = 0; i < 20; i++) {
list.add("数字" + (i+1));
}
count = list.size();
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
// 具体根据屏幕一次最大能展示多少item来决定是否显示加载中
if (list.size() < 16) {
footText.setVisibility(View.GONE);
}
}
}
四:效果图