Android List with Load more items
here is the code from list with more items progress bar code please go through
1. Main.java
- package android.test;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.AbsListView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.AbsListView.OnScrollListener;
- public class Main extends Activity {
- private ListView m_listView;
- private List m_data = new ArrayList<String>();
- private ArrayAdapter<String> m_adapter;
- private View m_footerView;
- private boolean m_bIsLoading = false;
- private Handler m_handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- m_footerView.setVisibility(View.INVISIBLE);
- for (int i = m_listView.getCount(); i < m_listView.getCount() + 10; ++i) {
- m_data.add("test__" + i);
- }
- m_adapter.notifyDataSetChanged();
- m_bIsLoading = false;
- }
- };
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- m_listView = (ListView) findViewById(R.id.listView1);
- m_footerView = LayoutInflater.from(this).inflate(R.layout.loading, null);
- m_listView.addFooterView(m_footerView);
- m_footerView.setVisibility(View.INVISIBLE);
- for (int i = 0; i < 10; ++i) {
- m_data.add("test__" + i);
- }
- m_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_data);
- m_listView.setAdapter(m_adapter);
- m_listView.setOnScrollListener(new OnScrollListener() {
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
- if (view.getLastVisiblePosition() >= totalItemCount - 1) {
- CheckLoadMore();
- }
- }
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- }
- });
- }
- private void CheckLoadMore() {
- if (m_bIsLoading)
- return;
- if (m_listView.getFooterViewsCount() != 0) {
- m_footerView.setVisibility(View.VISIBLE);
- m_listView.setSelection(m_data.size() - 1);
- LoadMoreItems();
- }
- }
- private void LoadMoreItems() {
- m_bIsLoading = true;
- new Thread() {
- public void run() {
- try {
- Thread.sleep(2000);
- m_handler.sendEmptyMessage(0);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- };
- }.start();
- }
- }
2. AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="android.test"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="7" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Main"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
3. loading.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <ProgressBar
- android:id="@+android:id/progress_large"
- android:layout_width="60px"
- android:layout_height="60px"
- android:layout_marginLeft="120px"
- android:layout_marginTop="10px"
- >
- </ProgressBar>
- <TextView
- android:id="@+id/loading_msg"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="30sp"
- android:text="@string/loading"
- android:layout_marginLeft="12px"
- android:layout_marginTop="10px"
- >
- </TextView>
- </LinearLayout>
4. main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
leave your comets
No comments:
Post a Comment