English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article demonstrates how to implement a similar Market pagination effect based on ListView in Android. Share it with everyone for reference, as follows:
In recent days, I have been studying the implementation of pagination and scrolling loading in ListView, and found that it can be realized by using the OnScroll method of listView, let's upload the code directly
ListViewScroll.java
package zy.lucifer.ListViewScroll; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import android.widget.AbsListView.OnScrollListener; import android.widget.LinearLayout.LayoutParams; public class ListViewScroll extends Activity { /** Called when the activity is first created. */ private LayoutParams mLayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); /** * Set layout display target maximization attribute */ private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); ListView listView ; private int lastItem = 0; LinearLayout loadingLayout; private listViewAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView) findViewById(R.id.myList); Log.i("test", "onCreate(Bundle savedInstanceState)>>>>>>>>>>>>>>>"); // Linear layout LinearLayout layout = new LinearLayout(this); // Set layout to horizontal direction layout.setOrientation(LinearLayout.HORIZONTAL); // Progress bar ProgressBar progressBar = new ProgressBar(this); // Position of the progress bar display progressBar.setPadding(0, 0, 15, 0); // Add the progress bar to the layout layout.addView(progressBar, mLayoutParams); // Text content TextView textView = new TextView(this); textView.setText("Loading..."); textView.setGravity(Gravity.CENTER_VERTICAL); // Add text to layout layout.addView(textView, FFlayoutParams); // Set the gravity direction of layout, that is, the alignment method is layout.setGravity(Gravity.CENTER); // Set the ListView footer layout loadingLayout = new LinearLayout(this); loadingLayout.addView(layout, mLayoutParams); loadingLayout.setGravity(Gravity.CENTER); listView.addFooterView(loadingLayout); adapter = new listViewAdapter(); listView.setAdapter(adapter); listView.setOnScrollListener(new OnScrollListener() { @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub Log.i("test", "Scroll>>>first: ") + firstVisibleItem + ", visible: " + visibleItemCount + ", total: " + totalItemCount); lastItem = firstVisibleItem + visibleItemCount - 1; Log.i("test", "Scroll>>>lastItem: ") + lastItem); //Display50 items in ListItem, that is, 0-49because onScroll is triggered after the 'swipe' action, so use adapter.count <=41As a condition int scrolllength=101; if (adapter.count <= scrolllength) { if (firstVisibleItem+visibleItemCount == totalItemCount) { adapter.count += 10; adapter.notifyDataSetChanged(); listView.setSelection(lastItem); int currentPage=adapter.count/10; Toast.makeText(getApplicationContext(), "Page "+currentPage+"Page", Toast.LENGTH_LONG).show(); } } else { listView.removeFooterView(loadingLayout); } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub } }); } class listViewAdapter extends BaseAdapter { int count = 10; /* starting amount */ public int getCount() { return count; } public Object getItem(int pos) { return pos; } public long getItemId(int pos) { return pos; } public View getView(int pos, View v, ViewGroup p) { Log.i("test", "getView>>>pos:" + pos); TextView view; if (v == null) { view = new TextView(ListViewScroll.this); } else { view = (TextView) v; } view.setText("ListItem ") + pos); view.setTextSize(20f); view.setGravity(Gravity.CENTER); view.setHeight(60); return view; } } }
main.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" > <ListView android:cacheColorHint="#00000000" android:id="@"+id/myList" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> </LinearLayout>
Readers who are interested in more content related to Android can check the special topics on this site: 'Summary of Android View View Techniques', 'Summary of Android Layout Layout Techniques', 'Summary of Android Graphics and Image Processing Techniques', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android Debugging Techniques and Common Problem Solving Methods', 'Summary of Android Multimedia Operation Techniques (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', and 'Summary of Android Widget Usage'
I hope the content described in this article will be helpful to everyone in Android program design.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending email) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.