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

2020-05-25 16:06:27来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

 

 


原文链接:https://www.cnblogs.com/ruigege0000/p/12953754.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:HWPFDocument读取doc,wps文档(含图片读取)

下一篇:equals 和 hashCode 到底有什么联系?