最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)ListView分頁自動加載數(shù)據(jù)的方法

 更新時間:2015年12月23日 11:09:43   作者:cjjky  
這篇文章主要介紹了Android實現(xiàn)ListView分頁自動加載數(shù)據(jù)的方法,涉及Android生成listview列表的相關技巧,需要的朋友可以參考下

Android應用開發(fā)中,采用ListView組件來展示數(shù)據(jù)是很常用的功能,當一個應用要展現(xiàn)很多的數(shù)據(jù)時,一般情況下都不會把所有的數(shù)據(jù)一次就展示出來,而是通過分頁的形式來展示數(shù)據(jù),個人覺得這樣會有更好的用戶體驗。因此,很多應用都是采用分批次加載的形式來獲取用戶所需的數(shù)據(jù)。例如:微博客戶端可能會在用戶滑動至列表底端時自動加載下一頁數(shù)據(jù),也可能在底部放置一個"查看更多"按鈕,用戶點擊后,加載下一頁數(shù)據(jù)。
下面通過一個Demo來展示ListView功能如何實現(xiàn):該Demo通過在ListView列表的底部添加一個“查看更多...”按鈕來加載新聞(模擬新聞客戶端)分頁數(shù)據(jù)。同時限定每次加載10條記錄,但完全加載完數(shù)據(jù)后,就把ListView列表底部視圖“查看更多...”刪除。假設加載的數(shù)據(jù)總數(shù)為 38 條記錄。先看下該Demo工程的程序結構圖:

其中包 com.andyidea.bean中News.java類是新聞實體類,包com.andyidea.listview中paginationListViewActivity.java類是用來展示ListView列表。布局layout中包含三個布局文件,分別為:list_item.xml , loadmore.xml , main.xml 。下面分別貼下源碼:
layout中的 list_item.xml源碼:

<span style="font-size:13px;"><?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"> 
 <TextView 
   android:id="@+id/newstitle" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"/> 
 <TextView 
   android:id="@+id/newscontent" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"/> 
</LinearLayout></span> 

layout中l(wèi)oadmore.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"> 
 <Button  
   android:id="@+id/loadMoreButton"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content" 
   android:text="查看更多..." />  
</LinearLayout> 

layout中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:id="@+id/lvNews" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
</LinearLayou 

包 com.andyidea.bean中News.java類源碼:

package com.andyidea.bean; 
public class News { 
   
  private String title;  //標題 
  private String content; //內容 
   
  public String getTitle() { 
    return title; 
  } 
  public void setTitle(String title) { 
    this.title = title; 
  } 
  public String getContent() { 
    return content; 
  } 
  public void setContent(String content) { 
    this.content = content; 
  } 
 
} 

包com.andyidea.listview中paginationListViewActivity.java類源碼:

package com.andyidea.listview; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import com.andyidea.bean.News; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AbsListView; 
import android.widget.AbsListView.OnScrollListener; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class PaginationListViewActivity extends Activity implements OnScrollListener { 
   
  private ListView listView;  
  private int visibleLastIndex = 0;  //最后的可視項索引  
  private int visibleItemCount;    // 當前窗口可見項總數(shù)  
  private int datasize = 38;     //模擬數(shù)據(jù)集的條數(shù) 
  private PaginationAdapter adapter;  
  private View loadMoreView;  
  private Button loadMoreButton;  
  private Handler handler = new Handler();  
   
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
     
    loadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null); 
    loadMoreButton = (Button)loadMoreView.findViewById(R.id.loadMoreButton); 
    loadMoreButton.setOnClickListener(new View.OnClickListener() { 
       
      @Override 
      public void onClick(View v) { 
        loadMoreButton.setText("正在加載中...");  //設置按鈕文字 
        handler.postDelayed(new Runnable() { 
           
          @Override 
          public void run() { 
            loadMoreData(); 
            adapter.notifyDataSetChanged(); 
            loadMoreButton.setText("查看更多..."); //恢復按鈕文字 
          } 
        },2000); 
         
      } 
    }); 
     
    listView = (ListView)findViewById(R.id.lvNews); 
    listView.addFooterView(loadMoreView);  //設置列表底部視圖 
    initializeAdapter(); 
    listView.setAdapter(adapter); 
    listView.setOnScrollListener(this); 
  } 
   
  @Override 
  public void onScrollStateChanged(AbsListView view, int scrollState) { 
    int itemsLastIndex = adapter.getCount()-1; //數(shù)據(jù)集最后一項的索引  
    int lastIndex = itemsLastIndex + 1; 
    if (scrollState == OnScrollListener.SCROLL_STATE_IDLE 
        && visibleLastIndex == lastIndex) { 
      // 如果是自動加載,可以在這里放置異步加載數(shù)據(jù)的代碼 
    } 
  } 
 
 
  @Override 
  public void onScroll(AbsListView view, int firstVisibleItem, 
      int visibleItemCount, int totalItemCount) { 
    this.visibleItemCount = visibleItemCount; 
    visibleLastIndex = firstVisibleItem + visibleItemCount - 1; 
     
    Log.e("========================= ","========================"); 
    Log.e("firstVisibleItem = ",firstVisibleItem+""); 
    Log.e("visibleItemCount = ",visibleItemCount+""); 
    Log.e("totalItemCount = ",totalItemCount+""); 
    Log.e("========================= ","========================"); 
     
    //如果所有的記錄選項等于數(shù)據(jù)集的條數(shù),則移除列表底部視圖 
    if(totalItemCount == datasize+1){ 
      listView.removeFooterView(loadMoreView); 
      Toast.makeText(this, "數(shù)據(jù)全部加載完!", Toast.LENGTH_LONG).show(); 
    } 
  } 
   
  /** 
   * 初始化ListView的適配器 
   */ 
  private void initializeAdapter(){ 
    List<News> news = new ArrayList<News>(); 
    for(int i=1;i<=10;i++){ 
      News items = new News(); 
      items.setTitle("Title"+i); 
      items.setContent("This is News Content"+i); 
      news.add(items); 
    } 
    adapter = new PaginationAdapter(news); 
  } 
   
  /** 
   * 加載更多數(shù)據(jù) 
   */ 
  private void loadMoreData(){ 
    int count = adapter.getCount(); 
     
    if(count+10 <= datasize){ 
      for(int i=count+1; i<=count+10; i++){ 
        News item = new News(); 
        item.setTitle("Title"+i); 
        item.setContent("This is News Content"+i); 
        adapter.addNewsItem(item); 
      } 
    }else{ 
      for(int i=count+1; i<=datasize; i++){ 
        News item = new News(); 
        item.setTitle("Title"+i); 
        item.setContent("This is News Content"+i); 
        adapter.addNewsItem(item); 
      } 
    } 
     
  } 
   
   
  class PaginationAdapter extends BaseAdapter{ 
     
    List<News> newsItems; 
     
    public PaginationAdapter(List<News> newsitems){ 
      this.newsItems = newsitems; 
    } 
 
    @Override 
    public int getCount() { 
      return newsItems.size(); 
    } 
 
    @Override 
    public Object getItem(int position) { 
      return newsItems.get(position); 
    } 
 
    @Override 
    public long getItemId(int position) { 
      return position; 
    } 
 
    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
      if(view == null){ 
        view = getLayoutInflater().inflate(R.layout.list_item, null); 
      } 
       
      //新聞標題 
      TextView tvTitle = (TextView)view.findViewById(R.id.newstitle); 
      tvTitle.setText(newsItems.get(position).getTitle()); 
      //新聞內容 
      TextView tvContent = (TextView)view.findViewById(R.id.newscontent); 
      tvContent.setText(newsItems.get(position).getContent()); 
       
      return view; 
    } 
     
    /** 
     * 添加數(shù)據(jù)列表項 
     * @param newsitem 
     */ 
    public void addNewsItem(News newsitem){ 
      newsItems.add(newsitem); 
    } 
     
  } 
 
} 

最后,運行程序的結果截圖如下:

通過上面的截圖,當我們點擊"查看更多..."按鈕時,就會加載下10條記錄,當加載完所有的記錄后,ListView的底部視圖將會移除。

希望本文所述對大家學習Android軟件編程有所幫助。

相關文章

最新評論

徐水县| 乐都县| 冷水江市| 湖北省| 宜良县| 墨脱县| 乐亭县| 黑水县| 合江县| 东阿县| 扬州市| 广汉市| 双辽市| 宁陵县| 镇沅| 龙游县| 平遥县| 山丹县| 灵川县| 高邮市| 贞丰县| 山阴县| 楚雄市| 赣州市| 井研县| 宕昌县| 交城县| 阿坝县| 大荔县| 南京市| 封丘县| 揭东县| 建宁县| 肥城市| 嵩明县| 岳普湖县| 北宁市| 和政县| 黔西| 石景山区| 怀仁县|