android ListView的分段顯示、分頁顯示

來源:互聯網
上載者:User

Java代碼

package com.yangguangfu.listview;    import android.app.ListActivity;  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.AbsListView.OnScrollListener;  import android.widget.LinearLayout.LayoutParams;  /**  *   * @author 阿福  *  */  public class MainActivity extends ListActivity implements OnScrollListener {            private static final String TAG = "MainActivity";      private listViewAdapter adapter = new listViewAdapter();      private int lastItem = 0;      /**      * 設定布局顯示為目標有多大就多大      */      private LayoutParams WClayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);      /**      * 設定布局顯示目標最大化      */      private LayoutParams FFlayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);            private ProgressBar progressBar;        protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);            Log.i(TAG, "onCreate(Bundle savedInstanceState)" );          //線性布局          LinearLayout layout = new LinearLayout(this);         //設定布局 水平方向          layout.setOrientation(LinearLayout.HORIZONTAL);           //進度條          progressBar = new ProgressBar(this);           //進度條顯示位置          progressBar.setPadding(0, 0, 15, 0);                    layout.addView(progressBar, WClayoutParams);                    TextView textView = new TextView(this);          textView.setText("載入中...");          textView.setGravity(Gravity.CENTER_VERTICAL);                    layout.addView(textView, FFlayoutParams);          layout.setGravity(Gravity.CENTER);                    LinearLayout loadingLayout = new LinearLayout(this);          loadingLayout.addView(layout, WClayoutParams);          loadingLayout.setGravity(Gravity.CENTER);                    //得到一個ListView用來顯示條目          ListView listView = getListView();          //添加到腳頁顯示          listView.addFooterView(loadingLayout);          //          registerForContextMenu(listView);          //          setListAdapter(adapter);          listView.setOnScrollListener(this);      }        public void onScroll(AbsListView v, int firstVisibleItem,              int visibleItemCount, int totalItemCount) {          lastItem = firstVisibleItem + visibleItemCount - 1;          System.out.println("lastItem:" + lastItem);          Log.i(TAG, "lastItem:" + lastItem);                }        public void onScrollStateChanged(AbsListView v, int state) {          if (lastItem == adapter.count                  && state == OnScrollListener.SCROLL_STATE_IDLE) {              adapter.count += 10;              adapter.notifyDataSetChanged();              Log.i(TAG, "lastItem:" + lastItem);          }      }    /**    * 要用用於產生顯示資料    * @author 阿福    *    */      class listViewAdapter extends BaseAdapter {          int count = 10;            public int getCount() {              Log.i(TAG, "count:" + count);              return count;          }            public Object getItem(int pos) {              Log.i(TAG, "pos:" + pos);              return pos;          }            public long getItemId(int pos) {              return pos;          }            public View getView(int pos, View v, ViewGroup p) {              TextView view = new TextView(MainActivity.this);              view.setText("阿福播放器 " + pos);              Log.i(TAG, "entry: " + pos);              view.setTextSize(20f);              view.setHeight(80);              return view;          }      }  }  

Java代碼

package com.yangguangfu.listview;      import android.app.ListActivity;  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.Button;  import android.widget.LinearLayout;  import android.widget.ListView;  import android.widget.TextView;  import android.widget.LinearLayout.LayoutParams;  /**  *   * @author 阿福  *  */  public class ButtonLoadingActivity extends ListActivity    {      private static final String TAG = "ButtonLoadingActivity";               private  listViewAdapter adapter = new listViewAdapter();      /**      * 設定布局顯示為目標有多大就多大      */      private LayoutParams WClayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);      /**      * 設定布局顯示目標最大化      */      private LayoutParams FFlayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);            protected void onCreate(Bundle savedInstanceState) {              super.onCreate(savedInstanceState);               Log.i(TAG, "onCreate(Bundle savedInstanceState):" );                LinearLayout layout = new LinearLayout(this);                layout.setOrientation(LinearLayout.HORIZONTAL);                               Button button = new Button(this);                button.setText("點擊載入下五條...");                button.setGravity(Gravity.CENTER_VERTICAL);                                 layout.addView(button,FFlayoutParams);                layout.setGravity(Gravity.CENTER);                LinearLayout loadingLayout = new LinearLayout(this);                loadingLayout.addView(layout,WClayoutParams);                loadingLayout.setGravity(Gravity.CENTER);                                                  ListView listView = getListView();                                 listView.addFooterView(loadingLayout);                                 button.setOnClickListener(new Button.OnClickListener()                   {         @Override                         public void onClick(View v)                  {                             adapter.count += 5;                      Log.i(TAG, "setOnClickListener:" +  adapter.count);                    adapter.notifyDataSetChanged();                    }                      });                             setListAdapter(adapter);         }          public void onScrollStateChanged(AbsListView v, int s) {           Log.i(TAG, "onScrollStateChanged(AbsListView v, int s)");      }             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) {                   TextView textView = new TextView(ButtonLoadingActivity.this);                   textView.setHeight(80);                   textView.setTextSize(20);                   textView.setText("阿福播放器 " + pos);                   Log.i(TAG, "getView:pos:" + pos);                  return textView;           }       }        }  

轉自:http://yangguangfu.iteye.com/blog/832809

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.