Android serial 13 - create startup activity class

According to the life cycle of fragments, we know that onAttach() method will be executed first, so we do some data initialization operations here, such as calling getNews() method to get several simulated news data, and completing the creation of NewsAdapter, and then loading news in onCreateView() method_ title_ Frag layout, and register the click practice for the list View of the news list. Next, in the onActivityCreated() method, we can find a news with id as news_ content_ The View of layout only appears in two page mode, which will be seen later in the layout. Then in the click event of ListView, we can judge that if the current mode is single page mode, a new activity will be started to display the news content. If the current mode is double page mode, the data in the news content fragment will be updated. ​

 

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(); //Initialize news data

    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;//Can be found news_content_layout Layout in two page mode     

    }else {

      isTwoPane = false;//can't find news_content_layout Single page layout

    }

  }

 

  @Override

  public void onItemClick(AdapterView<?> parent,View view,int position,long id) {

    News news = newsList.get(position);

    if(isTwoPane) {

      //Refresh if in two page mode NewsContentFrayment Content in

      NewsContentFragment newsContentFragment = (NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment);

      newsContentFragment.refresh(news.getTitle(),news.getTitle());

    }else {

      //In case of single page mode, start directly 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;

   

   

  }

​

}

 

3, Source code:

1. Project address

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

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

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. welcome to WeChat official account: Fourier transform, official account number, only for learning communication, background reply, "gift package", get big data learning materials.

 

Tags: Java Android Fragment Google

Posted on Mon, 25 May 2020 07:53:45 -0700 by geoffjb