1.這里撰寫一個類用于開啟活動,首先在onCreateView()方法中加載了我們剛剛創建的news_content_frag布局,這個沒什么好解釋的,接下來又提供了一個refresh()方法,這個方法就是用于將新聞的標題和內容顯示在界面上的,可以看到,這里通過findViewById()方法分別獲取到新聞的標題和內容控制元件,然后將方法傳遞進來的引數?設定進去,
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?布局中內容自動加了進來,
<?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()方法,并將新聞的標題和內容傳入,就可以把這些資料顯示出來了,注意這里我們還提供了一個action?Start()方法,
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>
?
?三、原始碼:
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.uj5u.com/yidong/4161.html
標籤:Android
