日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第一行代码学习笔记第四章——探究碎片

發(fā)布時間:2025/1/21 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第一行代码学习笔记第四章——探究碎片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

知識點目錄

  • 4.1 碎片是什么
  • 4.2 碎片的使用方式
    * 4.2.1 碎片的簡單用法
    * 4.2.2 動態(tài)添加碎片
    * 4.2.3 在碎片中模擬返回棧
    * 4.2.4 碎片和活動之間進行通信
  • 4.3 碎片的生命周期
    * 4.3.1 碎片的狀態(tài)和回調(diào)
    * 4.3.2 體驗碎片的生命周期
  • 4.4 動態(tài)加載布局的技巧
    * 4.4.1 使用限定符
    * 4.4.2 使用最小寬度限定符
  • 4.5 碎片的最佳實踐——一個簡易版的新聞應(yīng)用

知識點回顧

一般手機的屏幕大小在3-6英寸之間;一般平板電腦的屏幕在7-10英寸之間。隨著屏幕大小的不同,同樣的界面在視覺效果上有較大的差異。為了同時兼顧手機和平板視覺效果,從Android3.0開始引入了碎片的概念,它可以讓界面在平板上更好地展示。

4.1 碎片是什么

碎片(Fragment)是一種可以嵌入在活動當(dāng)中的UI片段??梢詫⑵淅斫鉃橐环N迷你型的活動。有時候我們?yōu)榱顺浞掷闷聊坏拇笮?#xff0c;可以將多個Fragment嵌入到一個活動當(dāng)中。

4.2 碎片的使用方式

4.2.1 碎片的簡單用法

1. 新建碎片布局文件

新建左側(cè)碎片布局left_fragment.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="Button"android:textAllCaps="false"/></LinearLayout>

新建右側(cè)碎片布局right_fragment.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:background="#00ff00"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is right fragment"/></LinearLayout>

2. 創(chuàng)建Fragment類

新建的LeftFragment類需要繼承Fragment,Fragment有系統(tǒng)內(nèi)置的android.app.Fragment和support-v4庫中的android.support.v4.app.Fragment兩種,考慮更好的兼容性,建議使用android.support.v4.app.Fragment,而build.gradle文件中的appcompat-v7會將support-v4一起引入進來,所以直接引用即可。

創(chuàng)建LeftFragment類:

public class LeftFragment extends Fragment {@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.left_fragment, container, false);return view;} }

創(chuàng)建RightFragment類:

public class RightFragment extends Fragment {@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.right_fragment, container, false);return view;}

重寫Fragment的onCreateView方法,通過inflater()方法將上面定義的布局加載進來。

3. 使用創(chuàng)建的Fragment類

修改activity_main.xml文件:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/left_fragment"android:name="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"/><fragmentandroid:id="@+id/right_fragment"android:name="com.example.fragmenttest.RightFragment"android:layout_width="0dp"android:layout_weight="1"android:layout_height="match_parent"/></LinearLayout>

使用標(biāo)簽在布局中添加碎片,通過android:name屬于來顯式指明要添加的碎片類名。

4. Fragment的簡單運行效果

4.2.2 動態(tài)添加碎片

碎片不僅可以在xml中靜態(tài)加載,還可以在程序運行時動態(tài)地添加到活動中。

新建another_right_fragment.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffff00"android:orientation="vertical"><TextViewandroid:id="@+id/textview"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is another right fragment"/></LinearLayout>

創(chuàng)建AnotherRightFragment類:

public class AnotherRightFragment extends Fragment {@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.another_right_fragment, container, false);return view;} }

修改activity_main.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/left_fragment"android:name="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><FrameLayoutandroid:id="@+id/right_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout>

在代碼中向FrameLayout里添加內(nèi)容,從而實現(xiàn)動態(tài)添加碎片的功能。

修改MainActivity中的代碼:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button);button.setOnClickListener(this);replaceFragment(new RightFragment());}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button:replaceFragment(new AnotherRightFragment());break;default:break;}}private void replaceFragment(Fragment fragment) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.replace(R.id.right_layout, fragment);transaction.commit();} }

上面代碼是當(dāng)點擊左側(cè)的按鈕時,通過replaceFragment()方法將右邊的碎片替換成AnotherRightFragment。

總結(jié)動態(tài)添加碎片的步驟:

  • 創(chuàng)建待添加的碎片實例

  • 調(diào)用getSupportFragmentManager()方法來獲取FragmentManager

  • 通過調(diào)用beginTransaction()方法來開啟一個事務(wù)

  • 向容器內(nèi)添加或替換碎片,一般使用replace()方法實現(xiàn),需要傳入容器的id和待添加的碎片實例

  • 提交事務(wù),調(diào)用commit()方法來完成

效果圖:

運行程序,效果圖如下:

4.2.3 在碎片中模擬返回棧

上面通過點擊按鈕添加一個碎片后,如果此時按下Back鍵程序就會直接退出。如果想要模范返回棧的效果,返回到上一個碎片,則需要在提交事務(wù)之前調(diào)用addToBackStack()方法。

private void replaceFragment(Fragment fragment) {FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.replace(R.id.right_layout, fragment);transaction.addToBackStack(null);transaction.commit(); }

效果圖:

重新運行程序,效果圖如下:

4.2.4 碎片和活動之間進行通信

1. Activity調(diào)用Fragment方法

LeftFragment leftFragment = (LeftFragment) getSupportFragmentManager().findFragmentById(R.id.left_fragment);

2. Fragment調(diào)用Activity方法

MainActivity mainActivity = (MainActivity) getActivity();

獲取到相應(yīng)的實例對象后,然后就可以直接調(diào)用相應(yīng)的公用方法。

4.3 碎片的生命周期

碎片也有生命周期,并且它和Activity的生命周期很相似。

4.3.1 碎片的狀態(tài)和回調(diào)

1. 運行狀態(tài)

碎片可見,并且與它所關(guān)聯(lián)的活動正處于運行狀態(tài)時,該碎片也在運行狀態(tài)。

2. 暫停狀態(tài)

當(dāng)一個活動進入暫停狀態(tài)時(由于另一個未占滿屏幕的活動被添加到了棧頂),與它相關(guān)聯(lián)的可見碎片就會進入到暫停狀態(tài)。

3. 停止?fàn)顟B(tài)

  • Activity進入停止?fàn)顟B(tài)時,與它所關(guān)聯(lián)的碎片就會進入停止?fàn)顟B(tài)。

  • 調(diào)用了FragmentTransaction的remove()、replace()方法,但在提交事務(wù)之前調(diào)用了addToBackStack()方法,此時碎片也會進入到停止?fàn)顟B(tài)。

4. 銷毀狀態(tài)

  • Activity進入銷毀狀態(tài)時,與它所關(guān)聯(lián)的碎片就會進入銷毀狀態(tài)。

  • 調(diào)用了FragmentTransaction的remove()、replace()方法,但在提交事務(wù)之間沒有調(diào)用了addToBackStack()方法,此時碎片也會進入到銷毀狀態(tài)。

Android官方給出了Fragment完整的生命周期示意圖:

  • onAttach():碎片實例被關(guān)聯(lián)到活動實例

  • onCreate():創(chuàng)建碎片時,系統(tǒng)調(diào)用該方法

  • onCreateView():當(dāng)碎片將要第一次繪制它的用戶界面時系統(tǒng)調(diào)用該方法

  • onActivityCreated:當(dāng)宿主活動被創(chuàng)建,在onCreateView()方法之后調(diào)用該方法

  • onStart(): 碎片可見時調(diào)用該方法

  • onResume(): 碎片可交互時調(diào)用該方法

  • onPause(): 當(dāng)首次表明用戶將要離開碎片時系統(tǒng)調(diào)用該方法

  • onStop(): 碎片將要被停止時調(diào)用

  • onDestroyView(): 調(diào)用該方法后,碎片將要被銷毀

  • onDestroy(): 該方法被用來清理碎片的狀態(tài)

4.3.2 體驗碎片的生命周期

省略…需要自己寫Demo去體驗!!!

4.4 動態(tài)加載布局的技巧

Android可以根據(jù)設(shè)備的分辨率或者屏幕大小來動態(tài)加載布局。

4.4.1 使用限定符

平板與手機最大的區(qū)別就是平板采用的雙頁模式,手機采用的是單頁模式。

在實際開發(fā)中,我們可以通過限定符讓程序的運行時動態(tài)的判斷是使用雙頁模式還是單頁模式。

修改activity_main文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/left_fragment"android:name="com.example.fragmenttest.LeftFragment"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>

在res目錄下新建layout-large文件夾,并新建一個布局,也叫activity_main.xml,代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/left_fragment"android:name="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><fragmentandroid:id="@+id/right_fragment"android:name="com.example.fragmenttest.RightFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="3"/></LinearLayout>

layout/activity_main布局只包含了一個碎片,即單頁模式,而layout-large/activity_main布局包含了兩個碎片,即雙頁模式。

large就是一個限定符,那些屏幕被認(rèn)為是large的設(shè)備就會自動加載layout-large文件夾下的布局,而屏幕小的設(shè)備則會加載layout文件夾下的布局。

效果圖:

單頁模式運行:

雙頁模式運行:

Android中有一些常見的限定符,參考下表:

4.4.2 使用最小寬度限定符

上面使用了large限定符,large屬于一個模糊的概念,如果我們想具體一點,到底多大是叫l(wèi)arge,此時我們就可以使用最小寬度限定符(Smallest-width Qualifier)。

最小寬度限定符允許我們對屏幕的寬度指定一個最小值(以dp為單位),然后以這個值為臨界點,屏幕寬度大于這個值的設(shè)備就加載一個布局,屏幕寬度小于這個值的設(shè)備就加載另一個布局。

在res目錄下新建一個layout-sw600dp文件夾,然后新建activity_main.xml文件。

當(dāng)程序運行在屏幕寬度大于600dp的設(shè)備上時,就會加載layout-sw600dp/activity_main布局;當(dāng)程序運行在屏幕寬度小于600dp的設(shè)備上時,則加載默認(rèn)的layout/activity_main布局。

4.5 碎片的最佳實踐——一個簡易版的新聞應(yīng)用

代碼已上傳到我的github上,需要的朋友可以點擊如下鏈接查看:

碎片的最佳實踐——一個簡易版的新聞應(yīng)用

運行效果圖:

單頁模式:

雙頁模式:

非常感謝您的耐心閱讀,希望我的文章對您有幫助。歡迎點評、轉(zhuǎn)發(fā)或分享給您的朋友或技術(shù)群。

總結(jié)

以上是生活随笔為你收集整理的第一行代码学习笔记第四章——探究碎片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。