您好,登錄后才能下訂單哦!
main6.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView6_1_1" android:layout_width="match_parent" android:layout_height="fill_parent" ></ListView> </LinearLayout>
main6_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/textView_title_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="28sp" android:text="title" android:textColor="#55ff88" /> <TextView android:id="@+id/textView_content_2_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:text="duanluosdfsdf" /> </LinearLayout>
MainActivity6
package com.example.listview; import java.util.ArrayList; import java.util.List; import java.util.Vector; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity6 extends Activity implements OnScrollListener{ private ListView listView; private List<News> news=new Vector<News>(); private MyAdapter myDapter; //線程之間通信的橋梁 private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case 1: //通知適配器,更新數據 myDapter.notifyDataSetChanged(); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main6); listView=(ListView) findViewById(R.id.listView6_1_1); //設置底部視圖 View footView=getLayoutInflater().inflate(R.layout.load, null); listView.addFooterView(footView); listView.setOnScrollListener(this); //initDate(); new LoadDateThread().start(); //加載數據的線程 myDapter=new MyAdapter(); listView.setAdapter(myDapter); } /** * 初始化數據 * */ int index; //計數器 void initDate(){ for(int i=0;i<10;i++){ News newss=new News(); newss.title="title-"+index; newss.content="content-"+index; index++; news.add(newss); } } /** * 自定義適配器 * */ class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return news.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return news.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) { // TODO Auto-generated method stub Viewholer viewholer; if(convertView==null){ convertView=getLayoutInflater().inflate(R.layout.main6_item, null); viewholer=new Viewholer(); viewholer.textView=(TextView) convertView.findViewById(R.id.textView_title_1_1); viewholer.textContent=(TextView) convertView.findViewById(R.id.textView_content_2_2); convertView.setTag(viewholer); }else{ viewholer=(Viewholer) convertView.getTag(); } //從veclor中取出數據并填充 News newss=news.get(position); viewholer.textView.setText(newss.title); viewholer.textContent.setText(newss.content); return convertView; } } static class Viewholer{ TextView textView; TextView textContent; } // int scrollState=0; int visiableLastIndex=0; //滾動條狀態發生變化事件方法 @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub /* this.scrollState=scrollState; System.out.println("scrollState:"+scrollState);*/ //滾動條在最下面并且滾動條是停止狀態 if(myDapter.getCount()==visiableLastIndex&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){ //啟動線程加載數據 new LoadDateThread().start(); } } //滾動事件方法 @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub System.out.println("view:"+view); System.out.println("firstVisibleItem:"+firstVisibleItem); System.out.println("visibleItemCount:"+visibleItemCount); System.out.println("totalItemCount"+totalItemCount); visiableLastIndex=firstVisibleItem+visibleItemCount-1; } //模擬加載數據 //子線程不允許訪問主線程的UI組件(保證主線程UI組件安全) class LoadDateThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub super.run(); initDate(); try { Thread.sleep(2000); //通知主線程,數據已加載完畢 handler.sendEmptyMessage(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
News
package com.example.listview; /** * 新聞對象 * */ public class News { String title; String content; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。