fragment、ListFragment使用ListView及自定义Listview等初始化操作

fragment、ListFragment使用ListView及自定义Listview等初始化操作

1.先说一下 从官方api中说fragment碎片中使用Listview有专门的 ListView碎片-> ListFragment 而不能继承Fragment

 官方api相关连接:http://developer.android.com/guide/components/fragments.html

2.不废话直接上代码,这是我项目种的代码 2.1版本同样适用

父类封装:

[java] view plaincopy
  1. public class BaseListFragment extends ListFragment {  
  2.     public Map<String, String> map;  
  3.     public String tag = this.getClass().getSimpleName(); // tag 用于测试log用  
  4.     public Context context; // 存储上下文对象  
  5.     public Activity activity; // 存储上下文对象  
  6.       
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         context = getActivity();  
  11.         activity = getActivity();  
  12.     }  
  13. }  

子类实现:

[java] view plaincopy
  1. /** 
  2.  * 新闻分类页面 
  3.  * @author chen 
  4.  * @date 2012-12-18 下午5:48:02 
  5.  */  
  6. public class NewsFragment extends BaseListFragment {  
  7.     /** 请求url */  
  8.     private String url;  
  9.     /** ListView */  
  10.     private PullListView mListView;  
  11. //  /** 分页 */  
  12. //  private Page page = new Page();  
  13.     /** 商铺适配器 */  
  14.     private Adapter mAdapter = new Adapter(activity);;  
  15.     /** 集合数据 */  
  16.     public ArrayList<News> mListData;  
  17.     /** 是否重新加载 */  
  18.     private boolean isClear;  
  19.       
  20.     private LayoutInflater mInflater;  
  21.     /** 新闻详细页面 url */  
  22.     private String detailUrl = C.http.http_news_detail;  
  23.       
  24.     public NewsFragment(){}  
  25.     public NewsFragment(Context context, String url) {  
  26.         this.context = context;  
  27.         this.url = url;  
  28.         mListData = null;  
  29.     }  
  30.       
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         Log.i(tag, "onCreate");  
  35.     }  
  36.       
  37.     // 加载多次  
  38.     public void onActivityCreated(Bundle savedInstanceState) {  
  39.         super.onActivityCreated(savedInstanceState);  
  40.         Log.i(tag, "onActivityCreated");  
  41.         mListView = (PullListView) getListView();//(ListView) activity.findViewById(android.R.id.list);  
  42.         if(mListData != null && mListData.size() > 0) {  
  43.             return;  
  44.         }  
  45.           
  46.         mListData = new ArrayList<News>();  
  47.         setListAdapter(mAdapter);  
  48.         mListView.onRefreshComplete();  
  49.         mListView.setonRefreshListener(new OnRefreshListener() {  
  50.             public void onRefresh() {  
  51.                 isClear = true;  
  52.                 // 初始化数据  
  53.                 AnsynHttpRequest.requestByGet(context, callbackData, R.string.http_news, url, truetruetrue);  
  54.             }  
  55.         });  
  56.         // 初始化数据  
  57.         AnsynHttpRequest.requestByGet(context, callbackData, R.string.http_news, url, truetruefalse);  
  58.     }  
  59.   
  60.     // 加载多次  
  61.     @Override  
  62.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  63.             Bundle savedInstanceState) {  
  64.         super.onCreateView(inflater, container, savedInstanceState);  
  65.         View view = inflater.inflate(R.layout.fragment_news_or_hotline, container, false);  
  66.         mInflater = inflater;  
  67.         Log.i(tag, "onCreateView");  
  68.         return view;  
  69.     }  
  70.     @Override  
  71.     public void onStart() {  
  72.         Log.i(tag, "onStart");  
  73.         super.onStart();  
  74.     }  
  75.     @Override  
  76.     public void onStop() {  
  77.         super.onStop();  
  78.         Log.i(tag, "onStop");  
  79.     }  
  80.     @Override  
  81.     public void onDestroy() {  
  82.         super.onDestroy();  
  83.         Log.i(tag, "onDestroy");  
  84.     }  
  85.       
  86.     /** 
  87.      * 异步回调回来并处理数据 
  88.      */  
  89.     private ObserverCallBack callbackData = new ObserverCallBack(){  
  90.         public void back(String data, int url) {  
  91.             Message msg = new Message();  
  92.             switch (url) {  
  93.             case R.string.http_news: // 解析主题信息数据  
  94.                 msg = new Message();  
  95.                 if(data == null) {  
  96.                     msg.what = 2;  
  97.                     mHandler.sendMessage(msg);  
  98.                     return;  
  99.                 }  
  100.                 try {  
  101.                     News news = new News();  
  102.                     ArrayList<News> dataInfo = news.getListInfo(data);  
  103.                     if(isClear) {  
  104.                         if(dataInfo != null && dataInfo.size()>0) mListData.clear();  
  105.                         isClear = false;  
  106.                     }  
  107.                       
  108.                     if(mListData != null && mListData.size() > 0)  
  109.                         msg.what = 1;  
  110.                     else  
  111.                         msg.what = 0;  
  112.                     if(dataInfo == null || dataInfo.size() == 0){  
  113.                         if( mListData != null && mListData.size() > 0){  
  114.                             msg = new Message();  
  115.                             msg.what = 2;  
  116.                             mHandler.sendMessage(msg);  
  117.                             return;  
  118.                         }  
  119.                     } else {  
  120.                         mListData.addAll(dataInfo);  
  121.                     }  
  122.                     mHandler.sendMessage(msg);  
  123.                 } catch (Exception e) {  
  124.                     e.printStackTrace();  
  125.                 }  
  126.                   
  127.                 break;  
  128.             case R.string.http_news_detail:  
  129.                 msg = new Message();  
  130.                 if(data == null) {  
  131.                     msg.what = 2;  
  132.                     mHandler.sendMessage(msg);  
  133.                     return;  
  134.                 }  
  135.                 try {  
  136.                     NewsDetail newsDetail = new NewsDetail();  
  137.                     newsDetail = newsDetail.getInfo(data, detailUrl);  
  138.                     msg.what = 3;  
  139.                     msg.obj = newsDetail;  
  140.                     if(newsDetail.content == null || newsDetail.content.length() == 0){  
  141.                         msg = new Message();  
  142.                         msg.what = 2;  
  143.                         mHandler.sendMessage(msg);  
  144.                         return;  
  145.                     }  
  146.                     mHandler.sendMessage(msg);  
  147.                 } catch (Exception e) {  
  148.                     msg = new Message();  
  149.                     msg.what = 2;  
  150.                     mHandler.sendMessage(msg);  
  151.                 }  
  152.                 break;  
  153.             default:  
  154.                 break;  
  155.             }  
  156.         };  
  157.     };  
  158.       
  159.     private Handler mHandler = new Handler(){  
  160.         @Override  
  161.         public void handleMessage(Message msg) {  
  162.             super.handleMessage(msg);  
  163.             switch (msg.what) {  
  164.             case 0:  
  165.                 mAdapter.notifyDataSetChanged();  
  166.                 mListView.onRefreshComplete();  
  167. //              if(mListData.size() == 0){  
  168. ////                    Toast.makeText(context, R.string.dialog_title_nowData, Toast.LENGTH_LONG).show();  
  169. //                    mListView.setVisibility(View.GONE);  
  170. //              } else mListView.setVisibility(View.VISIBLE);  
  171.                 break;  
  172.             case 1:  
  173.                 mAdapter.notifyDataSetChanged();  
  174. //              if(mListData.size() == 0){  
  175. ////                    Toast.makeText(context, R.string.dialog_title_nowData, Toast.LENGTH_LONG).show();  
  176. //                    mListView.setVisibility(View.GONE);  
  177. //              }else mListView.setVisibility(View.VISIBLE);  
  178.                 break;  
  179.             case 2:  
  180.                 Toast.makeText(context, R.string.no_data, Toast.LENGTH_LONG).show();  
  181.                 break;  
  182.             case 3:  
  183.                 NewsDetail newsDetail = (NewsDetail) msg.obj;  
  184.                 Intent intent = new Intent(context, NewsDetailActivity.class);  
  185.                 intent.putExtra("content", newsDetail.content);  
  186.                 intent.putExtra("hostUrl", newsDetail.hostUrl);  
  187.                 startActivity(intent);  
  188.                 break;  
  189.             default:  
  190.                 break;  
  191.             }  
  192.         }  
  193.     };  
  194.       
  195.     public final class ViewHolder {  
  196.         public TextView title;  
  197.         public TextView time;  
  198.     }  
  199.       
  200.     // 添加列表内容  
  201.     public class Adapter extends BaseAdapter {  
  202.         public Adapter(){}  
  203.         @Override  
  204.         public boolean areAllItemsEnabled() {  
  205.             return super.areAllItemsEnabled();  
  206.         }  
  207.           
  208.         public Adapter(Context context) {  
  209.         }  
  210.         @Override  
  211.         public int getCount() {  
  212.             return mListData.size();  
  213.         }  
  214.         @Override  
  215.         public Object getItem(int position) {  
  216.             return null;  
  217.         }  
  218.           
  219.         @Override  
  220.         public long getItemId(int position) {  
  221.             return position;  
  222.         }  
  223.   
  224.         @Override  
  225.         public View getView(final int position, View convertView,  
  226.                 ViewGroup parent) {  
  227.             final News news = mListData.get(position);  
  228.             ViewHolder holder = null;  
  229.             if (convertView == null) {  
  230.                 holder = new ViewHolder();  
  231.                 convertView = mInflater.inflate(R.layout.list_item_mian, null);  
  232.                 holder.title = (TextView) convertView.findViewById(R.id.list_main_txt_title);  
  233.                 holder.time = (TextView) convertView.findViewById(R.id.list_main_txt_time);  
  234.                 convertView.setTag(holder);  
  235.             } else {  
  236.                 holder = (ViewHolder) convertView.getTag();  
  237.             }  
  238.   
  239.             // 进行数据设置  
  240.             holder.title.setText(news.title);  
  241.             holder.time.setText(news.time);  
  242.             convertView.setOnClickListener(new OnClickListener() {  
  243.                 @Override  
  244.                 public void onClick(View v) { // 加载详细新闻  
  245.                     detailUrl = mListData.get(position).childUrl;  
  246.                     AnsynHttpRequest.requestByGet(context, callbackData, R.string.http_news_detail, detailUrl, truetruefalse);  
  247.                 }  
  248.             });  
  249.             return convertView;  
  250.         }  
  251.     }  
  252. }  
阅读更多
个人分类: android 开发学习
想对作者说点什么? 我来说一句

ListFragment

ListFragment

yongwoozzang yongwoozzang

2018-03-12 19:20:02

阅读数:32

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭