现在的位置: 首页 > 编程语言 > 正文

Android动态添加碎片代码实例

2020年02月14日 编程语言 ⁄ 共 3292字 ⁄ 字号 评论关闭

碎片的创建

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

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"><TextViewandroid: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@Overridepublic 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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这是碎片3"/><fragmentandroid: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;@Overrideprotected 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);}@Overridepublic 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动态添加碎片代码实例

以上就上有关Android动态添加碎片代码实例的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.