本篇内容介绍了“Android怎么实现ListView分页加载数据”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
FenyeActivity
package com.example.myapplication.fenye; import androidx.appcompat.app.AppCompatActivity; import android.app.ListActivity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.widget.AbsListView; import android.widget.Button; import android.widget.ListView; import com.example.myapplication.R; import java.util.ArrayList; public class FenyeActivity extends ListActivity implements AbsListView.OnScrollListener { private ListView listView; private int visibleLastIndex = 0; //最后的可视项索引 private int visibleItemCount; // 当前窗口可见项总数 private ListViewAdapter adapter; private View loadMoreView; private Button loadMoreButton; private Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fenye); loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null); loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton); listView = getListView(); //获取id是list的ListView listView.addFooterView(loadMoreView); //设置列表底部视图 initAdapter(); setListAdapter(adapter); //自动为id是list的ListView设置适配器 listView.setOnScrollListener(this); //添加滑动监听 } /** * 初始化适配器 */ private void initAdapter() { ArrayList<String> items = new ArrayList<String>(); for (int i = 0; i < 16; i++) { items.add(String.valueOf(i + 1)); } adapter = new ListViewAdapter(this, items); } /** * 滑动时被调用 */ @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { this.visibleItemCount = visibleItemCount; visibleLastIndex = firstVisibleItem + visibleItemCount - 1; } /** * 滑动状态改变时被调用 */ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { int itemsLastIndex = adapter.getCount() - 1; //数据集最后一项的索引 int lastIndex = itemsLastIndex + 1; //加上底部的loadMoreView项 if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) { //如果是自动加载,可以在这里放置异步加载数据的代码 Log.e("wy", "loading..."); handler.postDelayed(new Runnable() { @Override public void run() { loadData(); adapter.notifyDataSetChanged(); //数据集变化后,通知adapter listView.setSelection(visibleLastIndex - visibleItemCount + 1); //设置选中项 loadMoreButton.setText("load more"); //恢复按钮文字 } }, 1000); } } /** * 点击按钮事件 * @param view */ public void loadMore(View view) { loadMoreButton.setText("loading..."); //设置按钮文字loading handler.postDelayed(new Runnable() { @Override public void run() { loadData(); adapter.notifyDataSetChanged(); //数据集变化后,通知adapter listView.setSelection(visibleLastIndex - visibleItemCount + 1); //设置选中项 loadMoreButton.setText("load more"); //恢复按钮文字 } }, 1000); } /** * 模拟加载数据 */ private void loadData() { int count = adapter.getCount(); for (int i = count; i < count + 10; i++) { adapter.addItem(String.valueOf(i + 1)); } } }
ListViewAdapter
package com.example.myapplication.fenye; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.example.myapplication.R; public class ListViewAdapter extends BaseAdapter { private List<String> items; private LayoutInflater inflater; public ListViewAdapter(Context context, List<String> items) { this.items = items; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View view, ViewGroup parent) { if (view == null) { view = inflater.inflate(R.layout.list_fy_item, null); } TextView text = (TextView) view.findViewById(R.id.list_item_text); if(null!=items){ text.setText(items.get(position)); } return view; } /** * 添加列表项 * @param item */ public void addItem(String item) { items.add(item); } }
activity_fenye.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="3dp" android:paddingRight="3dp"> <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
list_fy_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/list_item_text" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="aa" android:gravity="center" android:textSize="20sp" android:paddingTop="10dp" android:paddingBottom="10dp"/> </LinearLayout>
load_more.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/loadMoreButton" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="load more" android:onClick="loadMore"/> </LinearLayout>
“Android怎么实现ListView分页加载数据”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。