根據碎片的生命周期,我們知道onAttach()方法首先會被執行,因此在這裡做一些數據初始化的操作,比如調用getNews()方法獲取幾條模擬的新聞數據,以及完成NewsAdapter的創建,然後在onCreateView()方法中載入了news_title_frag佈局,並給新聞列表的ListVi ...
根據碎片的生命周期,我們知道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.歡迎關註微信公眾號:傅里葉變換,個人公眾號,僅用於學習交流,後臺回覆”禮包“,獲取大數據學習資料