你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发教程:ListView下拉回弹刷新

[日期:2011-12-03] 来源:Linux社区  作者:MongDB [字体:大 中 小]

ListView item项布局listview_item.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.      <ImageView  
  8.         android:id="@+id/imageView"  
  9.          android:layout_width="wrap_content"  
  10.        android:layout_height="wrap_content"  
  11.          android:src="@drawable/pretty_girl"  
  12.     />  
  13.      <TextView  
  14.         android:id="@+id/textview"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.     />  
  18.   
  19. </LinearLayout>  

清单文件也贴出来吧,其实就是自动生成的,什么都不用改:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.focustech.android"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="14" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:label="@string/app_name"  
  14.             android:name="com.focustech.android.MainActivity" >  
  15.             <intent-filter >  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.   
  23. </manifest>  

此Demo的Activity:

  1. package com.focustech.android;  
  2.   
  3. import android.app.Activity;  
  4. import android.database.DataSetObserver;  
  5. import android.graphics.Color;  
  6. import android.os.Bundle;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.ListAdapter;  
  11. import android.widget.TextView;  
  12.   
  13. import com.focustech.android.CustomListView.OnRefreshListener;  
  14. import com.focustech.android.R;  
  15.   
  16. public class MainActivity extends Activity {  
  17.   
  18.     private ListAdapter adapter;  
  19.     int size = 10;  
  20.   
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.   
  25.         final CustomListView list = (CustomListView) findViewById(R.id.list);  
  26.   
  27.         adapter = new ListAdapter() {  
  28.             public void unregisterDataSetObserver(DataSetObserver arg0) {  
  29.   
  30.             }  
  31.   
  32.             public void registerDataSetObserver(DataSetObserver arg0) {  
  33.   
  34.             }  
  35.   
  36.             public boolean isEmpty() {  
  37.                 return false;  
  38.             }  
  39.   
  40.             public boolean hasStableIds() {  
  41.                 return false;  
  42.             }  
  43.   
  44.             public int getViewTypeCount() {  
  45.                 return 1;  
  46.             }  
  47.   
  48.             public View getView(int position, View convertView, ViewGroup parent) {  
  49.                 convertView = LayoutInflater.from(getApplicationContext())  
  50.                         .inflate(R.layout.listview_item, null);  
  51.   
  52.                 TextView mTextView = (TextView) convertView  
  53.                         .findViewById(R.id.textview);  
  54.                 mTextView.setText("pretty girl" + position);  
  55.                 mTextView.setTextColor(Color.RED);  
  56.                 return convertView;  
  57.             }  
  58.   
  59.             public int getItemViewType(int arg0) {  
  60.                 return arg0;  
  61.             }  
  62.   
  63.             public long getItemId(int arg0) {  
  64.                 return arg0;  
  65.             }  
  66.   
  67.             public Object getItem(int arg0) {  
  68.                 return null;  
  69.             }  
  70.   
  71.             public int getCount() {  
  72.                 return size;  
  73.             }  
  74.   
  75.             @Override  
  76.             public boolean isEnabled(int position) {  
  77.                 return false;  
  78.             }  
  79.   
  80.             @Override  
  81.             public boolean areAllItemsEnabled() {  
  82.                 return false;  
  83.             }  
  84.         };  
  85.   
  86.         list.setAdapter(adapter);  
  87.         // 在list上注册自定义的监听器   
  88.         list.setonRefreshListener(new OnRefreshListener() {  
  89.             public void onRefresh() {  
  90.                 new Thread(new Runnable() {  
  91.                     public void run() {  
  92.                         try {  
  93.                             Thread.sleep(1000);  
  94.                         } catch (Exception e) {  
  95.                         }  
  96.   
  97.                         runOnUiThread(new Runnable() {  
  98.                             public void run() {  
  99.                                 list.onRefreshComplete();  
  100.                             }  
  101.                         });  
  102.                         size++;  
  103.   
  104.                     }  
  105.                 }).start();  
  106.             }  
  107.         });  
  108.     }  
  109.   
  110. }  
linux
本文评论   查看全部评论 (7)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 7 楼
* 帆 发表于 2012/9/27 11:30:57
很牛逼啊。效果很好啊
第 6 楼
* 1 发表于 2012/7/24 15:45:47
好
第 5 楼
* hm 发表于 2012/6/5 14:15:41
牛人版主,崇拜啊
第 4 楼
* kjsolo 发表于 2012/4/8 22:28:53
http://soloit.diandian.com/post/2012-04-08/17148558
ListView下拉刷新,上拉更多,回弹,监听到达最顶部和最底部
第 3 楼
* a 发表于 2012/1/5 16:19:31
请教问题,怎么样直接对话