1.這裏編寫一個類用於開啓活動,首先在onCreateView()方法中加載了咱們剛剛建立的news_content_frag佈局,這個沒什麼好解釋的,接下來又提供了一個refresh()方法,這個方法就是用於將新聞的標題和內容顯示在界面上的。能夠看到,這裏經過findViewById()方法分別獲取到新聞的標題和內容控件,而後將方法傳遞進來的參數設置進去。android
package com.example.fragmentbestpractice; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class NewsContentFragment extends Fragment{ private View view; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { view = inflater.inflate(R.layout.news_content_frag, container,false); return view; } public void refresh(String newsTitle,String newsContent) { View visibilityLayout = view.findViewById(R.id.visibility_layout); visibilityLayout.setVisibility(View.VISIBLE); TextView newsTitleText = (TextView) view.findViewById(R.id.news_title); TextView newsContentText = (TextView) view.findViewById(R.id.news_content); newsTitleText.setText(newsTitle);//刷新新聞標題 newsContentText.setText(newsContent);//刷新新聞內容 } }
2.這裏咱們充分發揮了代碼的複用性,直接在佈局中引入了NewsContentFragment,這樣也就至關於把news_content_frag佈局中內容自動加了進來。git
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment android:id="@+id/news_content_fragment" android:name="com.example.fragmentbestpractice.NewsContentFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
3.而後新建一個類用於顯示新聞內容的活動,能夠看到在onCreate()方法中咱們經過Intent獲取到了傳入的新聞標題和內容,而後調用FragmentManager的findFragmentById()方法地獲得了NewsContentFragment的實例,接着調用它的refresh()方法,並將新聞的標題和內容傳入,就能夠把這些數據顯示出來了,注意這裏咱們還提供了一個actionStart()方法。github
package com.example.fragmentbestpractice; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.Window; public class NewsContentActivity extends Activity{ public static void actionStart(Context context,String newsTitle,String newsContent) { Intent intent = new Intent(context,NewsContentActivity.class); intent.putExtra("news_title",newsTitle); intent.putExtra("news_content",newsContent); context.startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.news_content); String newsTitle = getIntent().getStringExtra("news_title");//獲取傳入的新聞標題 String newsContent = getIntent().getStringExtra("news_content");//獲取傳入的新聞內容 NewsContentFragment newsContentFragment = (NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment); newsContentFragment.refresh(newsTitle,newsContent);//刷新NewsContentFragment界面 } }
4.接下來建立一個用於顯示新聞列表的佈局微信
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/news_title_list_view" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
3、源碼:app
1.項目地址ide
https://github.com/ruigege66/Android/tree/master/FragmentBestPractise佈局
2.CSDN:https://blog.csdn.net/weixin_44630050學習
3.博客園:https://www.cnblogs.com/ruigege0000/大數據
4.歡迎關注微信公衆號:傅里葉變換,我的公衆號,僅用於學習交流,後臺回覆」禮包「,獲取大數據學習資料ui