Paging Loading in Android

Source: Internet
Author: User

----------------------the mainactivity---------------------------------------------------

Package com.example.fenye;

Import java.util.ArrayList;
Import java.util.List;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.AbsListView;
Import Android.widget.AbsListView.OnScrollListener;
Import Android.widget.ListView;
Import Android.widget.Toast;

public class Mainactivity extends Activity {
Number of loads per load
private final int page_number=15;
Index the first few items
private int index=0;

Private list<string> list=new arraylist<string> ();

Private ListView ListView;
Private Myadapter adapter;


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Locate the Control listview
ListView = (ListView) Findviewbyid (R.id.listview);

To set the scroll listener for a ListView
Listview.setonscrolllistener (New Onscrolllistener () {

@Override
public void onscrollstatechanged (Abslistview view, int scrollstate) {
Judging if scrolling is stopped
if (Scrollstate==onscrolllistener.scroll_state_idle) {
If you scroll to the bottom of the line
if (View.getlastvisibleposition () ==list.size ()-1) {
New Assignment
Index=index+page_number;
Toast Reminder
Toast.maketext (Mainactivity.this, "Loading ...", 0). Show ();
Reload data
GetData ();
}
}

}

@Override
public void Onscroll (Abslistview view, int firstvisibleitem,
int visibleitemcount, int totalitemcount) {


}
});

Loading data
GetData ();

}

Loading data
private void GetData () {
New Thread () {
public void Run () {
Index the first few items
for (int i = index; i < Index+page_number; i++) {
List.add ("I Am the Entry" +i);
}
};
}.start ();

Number of loads per load
try {
1 seconds delay
Thread.Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
If the adapter is empty, create the adapter
if (adapter==null) {
adapter = new Myadapter (list, this);
Listview.setadapter (adapter);
}else{
Refresh Adapter
Adapter.notifydatasetchanged ();
}

}




}

------------------------the main.xml layout file-----------------------------

<listview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/listview" ></ListView>

-------------------Adapter----Create a class that inherits----Baseadapter---------------------------------------------

Package com.example.fenye;

Import java.util.List;

Import Android.content.Context;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.TextView;

public class Myadapter extends baseadapter{
Private list<string> List;
Private context context;


Public Myadapter (list<string> List, context context) {
Super ();
This.list = list;
This.context = context;
}

@Override
public int GetCount () {
TODO auto-generated Method Stub
return List.size ();
}

@Override
Public Object getItem (int position) {
TODO auto-generated Method Stub
return List.get (position);
}

@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
if (convertview==null) {
Convertview=view.inflate (context, r.layout.list_item, null);

}
TextView tv_name= (TextView) Convertview.findviewbyid (r.id.tv_name);
Tv_name.settext (List.get (position));

return convertview;
}

}

-----------------the list_item.xml layout file-----------------------------------------

<imageview android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/ic_launcher"/>
<textview android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/tv_name"/>

================ above is automatic loading ===========================================

============== below do a bit of manual loading ===== only need to be modified in =oncreate method, as follows: =================

/*
* Manually loaded in the ListView with Listview.addfooterview (view), add the button to the bottom of the ListView,
* Re-assign and reload data when clicked
*/

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);


Locate the Control listview
ListView = (ListView) Findviewbyid (R.id.listview);


Find the layout of the button
View view=getlayoutinflater (). Inflate (r.layout.button_footer, NULL);
Add the control button to the bottom of the ListView
Listview.addfooterview (view);
Button button= (button) View.findviewbyid (R.id.button);

Loading data
GetData ();
Button.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Re-assign Value
Index=index+page_number;
Toast Tips
Toast.maketext (Mainactivity.this, "Loading ...", 0). Show ();
Loading data
GetData ();

}
});

}

---------------the button_footer.xml layout file------------------

<button android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/button"
android:text= "Click to load"/>

Paging Loading in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.