Android中包含Header和Footer的无限ListView的实现
我在经历的两个项目中都遇到过这种情况,在做第一个项目时,因为经验不足,走了很多弯路,写出来的代码也相当复杂。在我做第二个项目的时候,我对前一次的代码进行了优化,在满足要求和性能的同时,代码量大大减少。
1. MainActivity.java MainActivity的作用是显示主界面的ListView。
package com.wubotao.activity;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import com.wubotao.adapter.StringAdapter;public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_list); ListView listView = (ListView) findViewById(R.id.listview_main); StringAdapter adapter = new StringAdapter(this, listView); listView.setAdapter(adapter); }}
2. AbstractAdapter.java AbstractAdapter是一个抽象类,实现了BaseAdapter的部分方法。
package com.wubotao.adapter;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.os.Handler;import android.os.Message;import android.widget.BaseAdapter;import android.widget.ListView;/** * * * @author wubotao * * @param * 被存放元素的类型 * * */public abstract class AbstractAdapter extends BaseAdapter { protected Context context; protected List list = new ArrayList(); protected ListView listView; protected AbstractAdapter(Context context, ListView listView) { this.context = context; this.listView = listView; } public int getCount() { return list == null ? 0 : list.size(); } public Object getItem(int position) { return list.get(position); } public long getItemId(int position) { return position; } protected Handler handler = new Handler() { @Override public void handleMessage(Message msg) { notifyDataSetChanged(); } };}
3. StringAdapter.java StringAdapter继承自AbstractAdapter,主要作用是实现数据和ListView图形界面的交互。
package com.wubotao.adapter;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.os.AsyncTask;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ListView;import android.widget.TextView;import android.widget.ViewSwitcher;import com.wubotao.activity.R;/** * * * @author wubotao * */public class StringAdapter extends AbstractAdapter { boolean headerLoading = false; boolean footerLoading = false; ViewSwitcher footerView; ViewSwitcher headerView; public StringAdapter(Context context, ListView listView) { super(context, listView); LayoutInflater inflater = LayoutInflater.from(context); headerView = (ViewSwitcher) inflater.inflate(R.layout.header, null); listView.addHeaderView(headerView); headerLoading(true); footerView = (ViewSwitcher) inflater.inflate(R.layout.footer, null); new ContentTask().execute(); TextView moreByHeader = (TextView) headerView .findViewById(R.id.textview_to_load_more); moreByHeader.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!headerLoading) { new HeaderTask().execute(); } } }); TextView moreByFooter = (TextView) footerView .findViewById(R.id.textview_to_load_more); moreByFooter.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!footerLoading) { new FooterTask().execute(); } } }); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.main_list_item, null); viewHolder.content = (TextView) convertView .findViewById(R.id.textview_content); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } String text = (String) getItem(position); viewHolder.content.setText(text); return convertView; } class ViewHolder { TextView content; } class HeaderTask extends AsyncTask> { @Override protected void onPreExecute() { headerLoading(true); } @Override protected List doInBackground(String... params) { List list = new ArrayList(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } for (int i = 0; i < 3; i++) { list.add("header-" + i); } return list; } @Override protected void onPostExecute(List result) { list.addAll(0, result); notifyDataSetChanged(); headerLoading(false); } } class ContentTask extends AsyncTask> { @Override protected List doInBackground(String... params) { List list = new ArrayList(); for (int i = 0; i < 3; i++) { list.add("content-" + i); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return list; } @Override protected void onPostExecute(List result) { list.addAll(0, result); notifyDataSetChanged(); headerLoading(false); listView.addFooterView(footerView); } } class FooterTask extends AsyncTask> { @Override protected void onPreExecute() { footerLoading(true); } @Override protected List doInBackground(String... params) { List messages = new ArrayList(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } for (int i = 0; i < 3; i++) { list.add("footer-" + i); } return messages; } @Override protected void onPostExecute(List result) { list.addAll(result); notifyDataSetChanged(); footerLoading(false); } } private void footerLoading(boolean isLoading) { if (isLoading) { footerView.setDisplayedChild(1); } else { footerView.setDisplayedChild(0); } footerLoading = isLoading; } private void headerLoading(boolean isLoading) { if (isLoading) { headerView.setDisplayedChild(1); } else { headerView.setDisplayedChild(0); } headerLoading = isLoading; }}
里面有一些XML在博客上没法显示,SVN源码下载:https://open-source-project-home.googlecode.com/svn/trunk/EndlessListView/
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~