溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Android中動態添加碎片

發布時間:2022-04-19 10:58:05 來源:億速云 閱讀:234 作者:iii 欄目:開發技術

本篇內容主要講解“怎么在Android中動態添加碎片”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么在Android中動態添加碎片”吧!

1.新建一個碎片布局,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. 新建一個類Fragment1.java,繼承自Fragment

注意Fragment有兩個不同的包,推薦使用support-v4中的,兼容性更好,另一個安卓4.2以下就會崩潰。在該碎片中可以進行各種操作,就如同操作一個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;
}
}

碎片和活動之間的通信。雖然碎片都是嵌入在活動中顯示的,但他們之間的關系并不明顯。

1.在活動中調用碎片的方法。FragmentManagert提供了一個類似于finViewById()的方法,用于從布局文件中獲取碎片的實例。如果是動態加載的就跟簡單了加載是你就有了該碎片的實例。

2.在碎片中調用活動的方法??梢酝ㄟ^getActivity()方法得到和當前碎片綁定的活動實例。

碎片的綁定

1.靜態綁定

在活動布局中加一個碎片標簽,比較簡單不細說。android:name="",該標簽為碎片對應的類,注意要包含路徑全名。

<?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.動態綁定

這個才是碎片的強大之處,在程序運行時動態的添加到碎片中,根據具體情況來動態添加碎片,可以將程序界面定制得更加多樣化(多用于自適應手機和平板的應用)

下面的代碼以點擊按鈕。有三個碎片,通過點擊事件在一個活動中動態切換顯示的碎片。

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);//第一個參數表示容器的id,第二個參數為碎片實例。
transaction.commit();
}
}

到此,相信大家對“怎么在Android中動態添加碎片”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女