服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android动态添加碎片代码实例

Android动态添加碎片代码实例

2022-10-21 14:57烦嚣的人 Android

这篇文章主要介绍了Android动态添加碎片代码实例,需要的朋友可以参考下

碎片的创建

要使用碎片先要创建一个碎片,创建一个碎片很简单。

1.新建一个碎片布局,fragment.xml

?
1
2
3
4
5
6
7
8
9
<?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。

?
1
2
3
4
5
6
7
8
9
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="",该标签为碎片对应的类,注意要包含路径全名。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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.动态绑定

这个才是碎片的强大之处,在程序运行时动态的添加到碎片中,根据具体情况来动态添加碎片,可以将程序界面定制得更加多样化(多用于自适应手机和平板的应用)

下面的代码以点击按钮。有三个碎片,通过点击事件在一个活动中动态切换显示的碎片。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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();
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/wuyoucao/p/6803242.html

延伸 · 阅读

精彩推荐