Android腾讯微博客户端开发5:利用FootView实现ListView滑动动态加载实现分页

老规矩,先上图 
这张图片左边部分和右边部分还是有很大的不同,不是指内容上,是指布局上,左边的是我今天写这次代码之前的布局,root是用的RelativeLayout,右边是用的FrameLayout,体现在界面上就是右边的list内容可以顶到最上面,而左边的list是在上面的topBar下面。 


看布局代码:布局代码还用到了include,主要是用来重用布局的。 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  3.     <LinearLayout android:orientation="vertical" android:background="#ffffffff" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  4.         <ListView android:id="@id/android:list" android:paddingTop="45.0dip" android:paddingBottom="50.0dip" android:layout_width="fill_parent" android:cacheColorHint="#00000000" android:fadingEdge="none" android:fastScrollEnabled="false" android:clipToPadding="false"  
  5.             android:layout_height="wrap_content" android:divider="@drawable/list_divider"/>  
  6.         <TextView android:text="" android:textSize="20.0sp" android:textColor="#ff5a5a5a" android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="15.0dip" android:visibility="invisible"/>  
  7.     </LinearLayout>  
  8.     <include android:id="@+id/home_top" layout="@layout/top_panel" android:layout_width="fill_parent" android:layout_height="wrap_content" />  
  9. </FrameLayout>  

把图片往下拉,此时还未到加载到数据的最底部。 
 
继续往下拉,ok,见底了,看到更多的提示了嘛,点击他 
 

出现进度条提示 
 
加载完毕之后显示新的一页,继续往下拉,又会看见更多提示,然后如此循环。主要是利用了隐藏。 
 

下面看看主的Activity的代码: 

Java代码  收藏代码
  1. public class HomeTimeLineActivity extends ListActivity implements OnItemClickListener,OnItemLongClickListener{  
  2.       
  3.     private DataHelper dataHelper;  
  4.     private UserInfo user;  
  5.     private MyWeiboSync weibo;  
  6.     private ListView listView;  
  7.     private HomeAdapter adapter;  
  8.     private JSONArray array;  
  9.     private AsyncImageLoader asyncImageLoader;  
  10.     private Handler handler;  
  11.     private ProgressDialog progressDialog;  
  12.     private View top_panel;  
  13.     private Button top_btn_left;  
  14.     private Button top_btn_right;  
  15.     private TextView top_title;  
  16.     private LinearLayout list_footer;  
  17.     private TextView tv_msg;  
  18.     private LinearLayout loading;  
  19.     private List<JSONObject> list;//微博数据列表  
  20.     private ExecutorService executorService;  
  21.     private static int PAGE_SIZE = 5;//每页显示的微博条数  
  22.     private int TOTAL_PAGE = 0;//当前已经记在的微博页数  
  23.     private static int THREADPOOL_SIZE = 5;//线程池的大小  
  24.       
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.home);  
  29.         setUpViews();  
  30.         setUpListeners();  
  31.           
  32.         dataHelper = DataBaseContext.getInstance(getApplicationContext());  
  33.         weibo = WeiboContext.getInstance();  
  34.           
  35.         List<UserInfo> userList = dataHelper.GetUserList(false);  
  36.           
  37.         SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);  
  38.         String nick = preferences.getString("user_default_nick""");//取得默认账号信息  
  39.           
  40.         if (nick != "") {  
  41.             user = dataHelper.getUserByName(nick,userList);  
  42.             top_title.setText(nick);//顶部工具栏昵称  
  43.         }  
  44.           
  45.         weibo.setAccessTokenKey(user.getToken());  
  46.         weibo.setAccessTokenSecrect(user.getTokenSecret());  
  47.           
  48.         progressDialog = new ProgressDialog(HomeTimeLineActivity.this);// 生成一个进度条  
  49.         progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  50.         progressDialog.setTitle("请稍等");  
  51.         progressDialog.setMessage("正在读取数据中!");  
  52.         handler = new GetHomeTimeLineHandler();  
  53.           
  54.         executorService.submit(new GetHomeTimeLineThread());//耗时操作,开启一个新线程获取数据  
  55.         progressDialog.show();  
  56.     }  
  57.       
  58.     private void setUpViews(){  
  59.         list_footer = (LinearLayout)LayoutInflater.from(HomeTimeLineActivity.this).inflate(R.layout.list_footer, null);  
  60.         tv_msg = (TextView)list_footer.findViewById(R.id.tv_msg);  
  61.         loading = (LinearLayout)list_footer.findViewById(R.id.loading);  
  62.         listView = getListView();  
  63.         top_panel = (View)findViewById(R.id.home_top);  
  64.         top_btn_left = (Button)top_panel.findViewById(R.id.top_btn_left);  
  65.         top_btn_right = (Button)top_panel.findViewById(R.id.top_btn_right);  
  66.         top_title = (TextView)top_panel.findViewById(R.id.top_title);  
  67.         listView.addFooterView(list_footer);//这儿是关键中的关键呀,利用FooterVIew分页动态加载  
  68.         list = new ArrayList<JSONObject>();  
  69.         executorService = Executors.newFixedThreadPool(THREADPOOL_SIZE);  
  70.     }  
  71.       
  72.     private void setUpListeners(){  
  73.         listView.setOnItemClickListener(this);  
  74.         listView.setOnItemLongClickListener(this);  
  75.         top_btn_right.setOnClickListener(new OnClickListener() {  
  76.             @Override  
  77.             public void onClick(View v) {  
  78.                 Intent intent = new Intent(HomeTimeLineActivity.this,AddWeiboActivity.class);  
  79.                 startActivity(intent);  
  80.             }  
  81.         });  
  82.         tv_msg.setOnClickListener(new OnClickListener() {  
  83.             @Override  
  84.             public void onClick(View v) {  
  85.                 //Toast.makeText(HomeTimeLineActivity.this, "我将消失了", Toast.LENGTH_SHORT).show();  
  86.                 executorService.submit(new GetHomeTimeLineThread());  
  87.                 tv_msg.setVisibility(View.GONE);//隐藏更多提示的TextView  
  88.                 loading.setVisibility(View.VISIBLE);//显示最下方的进度条  
  89.             }  
  90.         });  
  91.     }  
  92.       
  93.     class GetHomeTimeLineHandler extends Handler {  
  94.         @Override  
  95.         public void handleMessage(Message msg) {  
  96.             adapter = new HomeAdapter(HomeTimeLineActivity.this, list);  
  97.             listView.setAdapter(adapter);  
  98.             listView.setSelection((TOTAL_PAGE-1)*PAGE_SIZE+1);//设置最新获取一页数据成功后显示数据的起始数据  
  99.             progressDialog.dismiss();// 关闭进度条  
  100.             loading.setVisibility(View.GONE);//隐藏下方的进度条  
  101.             tv_msg.setVisibility(View.VISIBLE);//显示出更多提示TextView  
  102.         }  
  103.     }  
  104.       
  105.     class GetHomeTimeLineThread extends Thread {  
  106.         @Override  
  107.         public void run() {  
  108.             refreshList();  
  109.             Message msg = handler.obtainMessage();//通知线程来处理范围的数据  
  110.             handler.sendMessage(msg);  
  111.         }  
  112.     }  
  113.       
  114.     private void refreshList(){  
  115.         String jsonStr = weibo.getHomeMsg(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), PageFlag.PageFlag_First, (TOTAL_PAGE+1)*PAGE_SIZE);  
  116.         try {  
  117.             JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");  
  118.             array = dataObj.getJSONArray("info");  
  119.             if(array!=null&&array.length()>0){  
  120.                 TOTAL_PAGE++;  
  121.                 list.clear();  
  122.                 int lenth =array.length();  
  123.                 for(int i = 0;i<lenth;i++){  
  124.                     list.add(array.optJSONObject(i));  
  125.                 }  
  126.             }  
  127.         } catch (JSONException e) {  
  128.             e.printStackTrace();  
  129.         }  
  130.     }  
  131.       
  132.       
  133.     class HomeAdapter extends BaseAdapter {  
  134.         private Context context;  
  135.         private LayoutInflater inflater;  
  136.         private List<JSONObject> list;  
  137.           
  138.         public HomeAdapter(Context context, List<JSONObject> list) {  
  139.             super();  
  140.             this.context = context;  
  141.             this.list = list;  
  142.             this.inflater = LayoutInflater.from(context);  
  143.         }  
  144.   
  145.         @Override  
  146.         public int getCount() {  
  147.             return list.size();  
  148.         }  
  149.   
  150.         @Override  
  151.         public Object getItem(int position) {  
  152.             return list.get(position);  
  153.         }  
  154.   
  155.         @Override  
  156.         public long getItemId(int position) {  
  157.             return position;  
  158.         }  
  159.   
  160.         @Override  
  161.         public View getView(final int position, View convertView, ViewGroup parent) {  
  162.             asyncImageLoader = new AsyncImageLoader();  
  163.             HomeViewHolder viewHolder = new HomeViewHolder();  
  164.             JSONObject data = (JSONObject)list.get(position);  
  165.             JSONObject source = null;  
  166.             convertView = inflater.inflate(R.layout.home_list_item, null);  
  167.             try {  
  168.                 source = data.getJSONObject("source");  
  169.             } catch (JSONException e1) {  
  170.                 e1.printStackTrace();   
  171.             }  
  172.             viewHolder.home_headicon = (ImageView) convertView.findViewById(R.id.home_headicon);  
  173.             viewHolder.home_nick = (TextView) convertView.findViewById(R.id.home_nick);  
  174.             viewHolder.home_hasimage = (ImageView) convertView.findViewById(R.id.home_hasimage);  
  175.             viewHolder.home_timestamp = (TextView) convertView.findViewById(R.id.home_timestamp);  
  176.             viewHolder.home_origtext = (TextView) convertView.findViewById(R.id.home_origtext);  
  177.             viewHolder.home_source = (TextView) convertView.findViewById(R.id.home_source);  
  178.               
  179.             if(data!=null){  
  180.                 try {  
  181.                     convertView.setTag(data.get("id"));  
  182.                     //viewHolder.home_headicon.setImageDrawable(ImageUtil.getDrawableFromUrl(data.getString("head")+"/100"));//同步加载图片  
  183.                     viewHolder.home_nick.setText(data.getString("nick"));  
  184.                     viewHolder.home_timestamp.setText(TimeUtil.converTime(Long.parseLong(data.getString("timestamp"))));  
  185.                     //viewHolder.home_origtext.setText(data.getString("origtext"), TextView.BufferType.SPANNABLE);  
  186.                       
  187.                     /*Spannable spannable = (Spannable)viewHolder.home_origtext.getText();//加高亮显示 
  188.                     spannable.setSpan(new BackgroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);*/  
  189.                       
  190.                     SpannableString spannable = new SpannableString(data.getString("origtext"));  
  191.                     //把字符串解析成本地表情  
  192.                     spannable = TextUtil.getImageSpannableString(spannable, RegexUtil.getStartAndEndIndex(data.getString("origtext"), Pattern.compile("\\/[\u4e00-\u9fa5a-zA-Z]{1,3}")), getResources());  
  193.                     viewHolder.home_origtext.setText(spannable);  
  194.                       
  195.                       
  196.                     if(source!=null){  
  197.                         viewHolder.home_source.setText(source.getString("nick")+":"+source.getString("origtext"));  
  198.                         viewHolder.home_source.setBackgroundResource(R.drawable.home_source_bg);  
  199.                     }  
  200.                     //异步加载图片  
  201.                     Drawable cachedImage = asyncImageLoader.loadDrawable(data.getString("head")+"/100",viewHolder.home_headicon, new ImageCallback(){  
  202.                         @Override  
  203.                         public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {  
  204.                             imageView.setImageDrawable(imageDrawable);  
  205.                         }  
  206.                     });  
  207.                     if (cachedImage == null) {  
  208.                         viewHolder.home_headicon.setImageResource(R.drawable.icon);  
  209.                     } else {  
  210.                         viewHolder.home_headicon.setImageDrawable(cachedImage);  
  211.                     }  
  212.                     if(data.getJSONArray("image")!=null){  
  213.                         viewHolder.home_hasimage.setImageResource(R.drawable.hasimage);  
  214.                     }  
  215.                 } catch (JSONException e) {  
  216.                     e.printStackTrace();  
  217.                 } catch (Exception e) {  
  218.                     e.printStackTrace();  
  219.                 }  
  220.             }  
  221.             return convertView;  
  222.         }  
  223.     }  
  224.       
  225.     static class HomeViewHolder {  
  226.         private ImageView home_headicon;  
  227.         private TextView home_nick;  
  228.         private TextView home_timestamp;  
  229.         private TextView home_origtext;  
  230.         private TextView home_source;  
  231.         private ImageView home_hasimage;  
  232.     }  
  233.   
  234.     @Override  
  235.     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position,long arg3) {  
  236.         CharSequence [] items = null;  
  237.         try {  
  238.             items= new CharSequence[]{"转播","对话","点评","收藏",((JSONObject)array.opt(position)).getString("nick"),"取消"};  
  239.         } catch (JSONException e) {  
  240.             e.printStackTrace();  
  241.         }  
  242.         new AlertDialog.Builder(HomeTimeLineActivity.this).setTitle("选项").setItems(items,new DialogInterface.OnClickListener() {  
  243.             @Override  
  244.             public void onClick(DialogInterface dialog, int which) {  
  245.                         switch (which) {  
  246.                         case 0: {  
  247.                         }  
  248.                             break;  
  249.                         case 1: {  
  250.                         }  
  251.                             break;  
  252.                         case 2: {  
  253.                         }  
  254.                             break;  
  255.                         case 3: {  
  256.                         }  
  257.                             break;  
  258.                         case 4: {  
  259.                         }  
  260.                             break;  
  261.                         case 5: {  
  262.                         }  
  263.                             break;  
  264.                         default:  
  265.                             break;  
  266.                         }  
  267.             }  
  268.         }).show();  
  269.         return false;  
  270.     }  
  271.   
  272.     @Override  
  273.     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {  
  274.         JSONObject weiboInfo = (JSONObject)array.opt(position);  
  275.         Intent intent = new Intent(HomeTimeLineActivity.this, WeiboDetailActivity.class);  
  276.         try {  
  277.             intent.putExtra("weiboid", weiboInfo.getString("id"));  
  278.             startActivity(intent);  
  279.         } catch (JSONException e) {  
  280.             e.printStackTrace();  
  281.         }  
  282.     }  
  283.       
  284.     @Override  
  285.     public boolean onCreateOptionsMenu(Menu menu){  
  286.         super.onCreateOptionsMenu(menu);  
  287.         MenuInflater menuInflater = getMenuInflater();  
  288.         menuInflater.inflate(R.menu.home_timeline_menu, menu);  
  289.         return true;  
  290.     }  
  291.       
  292.     @Override  
  293.     public boolean onOptionsItemSelected(MenuItem item) {  
  294.         switch (item.getItemId()) {  
  295.         case R.id.menu_settings: {  
  296.   
  297.         }  
  298.             break;  
  299.         case R.id.menu_official: {  
  300.   
  301.         }  
  302.             break;  
  303.         case R.id.menu_feedback: {  
  304.   
  305.         }  
  306.             break;  
  307.         case R.id.menu_account: {  
  308.             Intent intent = new Intent(HomeTimeLineActivity.this,  
  309.                     AccountActivity.class);  
  310.             startActivity(intent);  
  311.         }  
  312.             break;  
  313.         case R.id.menu_about: {  
  314.             Intent intent = new Intent(HomeTimeLineActivity.this,  
  315.                     AddWeiboActivity.class);  
  316.             startActivity(intent);  
  317.         }  
  318.             break;  
  319.         case R.id.menu_quit: {  
  320.   
  321.         }  
  322.             break;  
  323.         default:  
  324.             break;  
  325.         }  
  326.         return true;  
  327.     }  
  328. }  


下面的布局文件是你所看见的更多提示和下方的进度条布局文件: 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:minHeight="?android:listPreferredItemHeight"  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <TextView android:textSize="16.0sp" android:textColor="#ff545454" android:gravity="center" android:id="@id/tv_msg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="更多"/>  
  5.     <LinearLayout android:gravity="center" android:layout_gravity="center" android:orientation="horizontal" android:id="@id/loading" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  6.         <ProgressBar android:layout_gravity="center_vertical" android:id="@id/footprogress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateBehavior="repeat" style="?android:progressBarStyleSmallInverse" />  
  7.         <TextView android:textColor="#ff000000" android:gravity="left|center" android:padding="3.0px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取中" />  
  8.     </LinearLayout>  
  9. </LinearLayout>  
posted @ 2012-02-20 17:25  aflylove  阅读(419)  评论(0编辑  收藏  举报