Android動(dòng)態(tài)添加碎片代碼實(shí)例
碎片的創(chuàng)建
要使用碎片先要?jiǎng)?chuàng)建一個(gè)碎片,創(chuàng)建一個(gè)碎片很簡單。
1.新建一個(gè)碎片布局,fragment.xml
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是碎片1"/> </LinearLayout>
2. 新建一個(gè)類Fragment1.java,繼承自Fragment
注意Fragment有兩個(gè)不同的包,推薦使用support-v4中的,兼容性更好,另一個(gè)安卓4.2以下就會(huì)崩潰。在該碎片中可以進(jìn)行各種操作,就如同操作一個(gè)activity。
public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_questions1,container,false); Log.d("questionMain1","碎片1加載"); return view; } }
碎片和活動(dòng)之間的通信。雖然碎片都是嵌入在活動(dòng)中顯示的,但他們之間的關(guān)系并不明顯。
1.在活動(dòng)中調(diào)用碎片的方法。FragmentManagert提供了一個(gè)類似于finViewById()的方法,用于從布局文件中獲取碎片的實(shí)例。如果是動(dòng)態(tài)加載的就跟簡單了加載是你就有了該碎片的實(shí)例。
2.在碎片中調(diào)用活動(dòng)的方法??梢酝ㄟ^getActivity()方法得到和當(dāng)前碎片綁定的活動(dòng)實(shí)例。
碎片的綁定
1.靜態(tài)綁定
在活動(dòng)布局中加一個(gè)碎片標(biāo)簽,比較簡單不細(xì)說。android:name="",該標(biāo)簽為碎片對(duì)應(yīng)的類,注意要包含路徑全名。
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是碎片3"/> <fragment android:id="@+id/fragment1" android:name="com.example.fragment1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
2.動(dòng)態(tài)綁定
這個(gè)才是碎片的強(qiáng)大之處,在程序運(yùn)行時(shí)動(dòng)態(tài)的添加到碎片中,根據(jù)具體情況來動(dòng)態(tài)添加碎片,可以將程序界面定制得更加多樣化(多用于自適應(yīng)手機(jī)和平板的應(yīng)用)
下面的代碼以點(diǎn)擊按鈕。有三個(gè)碎片,通過點(diǎn)擊事件在一個(gè)活動(dòng)中動(dòng)態(tài)切換顯示的碎片。
package com.xiaobu.xiaoyan1.question; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.TextView; import com.xiaobu.xiaoyan1.R; import com.xiaobu.xiaoyan1.base.BaseActivity; public class QuestionsMain extends BaseActivity implements TextView.OnClickListener{ private TextView fragment1; private TextView fragment2; private TextView fragment3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_question_main); initView(); } private void initView(){ ((TextView)findViewById(R.id.question_text)).setTextColor(getResources().getColor(R.color.colorTextChecked)); fragment1=(TextView)findViewById(R.id.quiz_text_view); fragment2=(TextView)findViewById(R.id.answer_text_view); fragment3=(TextView)findViewById(R.id.chosen_text_view); fragment1.setOnClickListener(this); fragment2.setOnClickListener(this); fragment3.setOnClickListener(this); changeFragment(new QuestionsMain1()); checkedChange(fragment1); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.quiz_text_view: changeFragment(new QuestionsMain1()); break; case R.id.answer_text_view: changeFragment(new QuestionsMain2()); break; case R.id.chosen_text_view: changeFragment(new QuestionsMain3()); break; default: break; } } private void changeFragment(Fragment fragment){ FragmentManager fragmentManager=getSupportFragmentManager(); FragmentTransaction transaction=fragmentManager.beginTransaction(); transaction.replace(R.id.main_view,fragment);//第一個(gè)參數(shù)表示容器的id,第二個(gè)參數(shù)為碎片實(shí)例。 transaction.commit(); } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義軟鍵盤的設(shè)計(jì)與實(shí)現(xiàn)代碼
本篇文章主要介紹了 Android自定義軟鍵盤的設(shè)計(jì)與實(shí)現(xiàn)代碼,有需要的可以了解一下。2016-11-11Android?ViewPager2?+?Fragment?聯(lián)動(dòng)效果的實(shí)現(xiàn)思路
這篇文章主要介紹了Android?ViewPager2?+?Fragment?聯(lián)動(dòng),本篇主要介紹一下 ViewPager2 + Fragment聯(lián)動(dòng)效果的實(shí)現(xiàn)思路,需要的朋友可以參考下2022-12-12Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)
這篇文章主要介紹了Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01Android自定義View實(shí)現(xiàn)游戲搖桿鍵盤的方法示例
Android進(jìn)階過程中有一個(gè)繞不開的話題——自定義View。最近在做項(xiàng)目中又遇到了,所以下面這篇文章主要給大家介紹了利用Android自定義View實(shí)現(xiàn)游戲搖桿鍵盤的相關(guān)資料,操作方式類似王者榮耀的搖桿操作,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。2017-07-07Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解
這篇文章主要為大家介紹了Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法
這篇文章主要介紹了Android源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法,用idegen來生成針對(duì)AndroidStudio或IntelliJ?IDEA的Android系統(tǒng)源代碼工程配置文件,需要的朋友可以參考下2022-08-08