• Android连载13-创建启动活动类


    根据碎片的生命周期,我们知道onAttach()方法首先会被执行,因此在这里做一些数据初始化的操作,比如调用getNews()方法获取几条模拟的新闻数据,以及完成NewsAdapter的创建,然后在onCreateView()方法中加载了news_title_frag布局,并给新闻列表的ListView注册了点击实践,接下来在onActivityCreated()方法中,我们通过是否能够找到一个id为news_content_layout的View只在双页模式中才会出现,在稍后的布局中将会看到。然后在ListView的点击事件中我们可以判断出来,如果当前是单页模式,就会启动一个新的活动去显示新闻内容,如果当前是双页模式,​就更新新闻内容碎片里的数据。​

    package com.example.fragmentbestpractice;
    
    ​
    
    import java.util.ArrayList;
    
    import java.util.List;
    
    ​
    
    import android.app.Activity;
    
    import android.app.Fragment;
    
    import android.os.Bundle;
    
    import android.view.LayoutInflater;
    
    import android.view.View;
    
    import android.view.ViewGroup;
    
    import android.widget.AdapterView;
    
    import android.widget.AdapterView.OnItemClickListener;
    
    import android.widget.ListView;
    
    ​
    
    public class NewsTitleFragment extends Fragment implements OnItemClickListener{
    
      private ListView newsTitleListView;
    
     
    
      private List<News> newsList;
    
     
    
      private NewsAdapter adapter;
    
     
    
      private boolean isTwoPane;
    
     
    
      @Override
    
      public void onAttach(Activity activity) {
    
        super.onAttach(activity);
    
        newsList = getNews(); //初始化新闻数据
    
        adapter = new NewsAdapter(activity,R.layout.news_item,newsList);
    
      }
    
     
    
      @Override
    
      public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.news_title_frag,container,false);
    
        newsTitleListView = (ListView) view.findViewById(R.id.news_title_list_view);
    
        newsTitleListView.setAdapter(adapter);
    
        newsTitleListView.setOnItemClickListener(this);
    
        return view;
    
      }
    
     
    
      @Override
    
      public void onActivityCreated(Bundle savedInstanceState) {
    
        super.onActivityCreated(savedInstanceState);
    
        if(getActivity().findViewById(R.id.news_content_layout)!= null) {
    
          isTwoPane = true;//可以找到news_content_layout布局时,为双页模式     
    
        }else {
    
          isTwoPane = false;//找不到news_content_layout布局时,为单页模式
    
        }
    
      }
    
     
    
      @Override
    
      public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
    
        News news = newsList.get(position);
    
        if(isTwoPane) {
    
          //如果是双页模式,则刷新NewsContentFrayment中的内容
    
          NewsContentFragment newsContentFragment = (NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment);
    
          newsContentFragment.refresh(news.getTitle(),news.getTitle());
    
        }else {
    
          //如果是单页模式,则直接启动NewsContentActivity
    
          NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent()); 
    
        }
    
      }
    
      private List<News> getNews(){
    
        List<News> newsList = new ArrayList<News>();
    
        News news1 = new News();
    
        news1.setTitle("Succeed in College as a Learning Disabled Student");
    
        news1.setContent("Collaget freshmen will soon learn to live with roomate,adjust to a new social scene and survive less-than-stellar.");
    
        News news2 = new News();
    
        news2.setTitle("Google Android exec poached by China's Xiaomi");
    
        news2.setContent("China's Xiaomi has poached a key Google executive.");
    
        newsList.add(news2);
    
        return newsList;
    
       
    
       
    
      }
    
    ​
    
    }

    三、源码:

    1.项目地址

    https://github.com/ruigege66/Android/tree/master/FragmentBestPractise

    2.CSDN:https://blog.csdn.net/weixin_44630050

    3.博客园:https://www.cnblogs.com/ruigege0000/

    4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

     

  • 相关阅读:
    3. docker-compose实战--ghost app
    3. nacos服务发现
    1. nacos初识, 和eureka的比较, 在docker上安装nacos
    2.nacos服务配置中心
    如何将PHP对象数组转换成普通数组
    php 通过变量 来调用函数
    linux svn
    Sublime text 3安装svn插件
    php 字符串 定界符 json_last_error()
    centos7 rm -rf 删除文件的找回
  • 原文地址:https://www.cnblogs.com/ruigege0000/p/12953754.html
Copyright © 2020-2023  润新知