Android 动态添加Fragment的实例代码

 更新时间:2016年08月10日 14:18:30   作者:孺子-小哥  
这篇文章主要介绍了Android 动态添加Fragment的实例代码的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下

1.fragment1布局及代码

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment1Activity">
<fragment
android:layout_width="match_parent"
android:layout_height="100dp"
android:name="com.example.administrator.jreduch06.fragment.TopFragment"
android:id="@+id/top_fragment"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</fragment>
<fragment
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/leftfragment"
android:name="com.example.administrator.jreduch06.fragment.LeftFragment"
android:layout_below="@+id/top_fragment"
android:layout_alignParentStart="true">
</fragment>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl"
android:layout_alignParentStart="true"
android:layout_below="@+id/leftfragment">
</FrameLayout>
</RelativeLayout>

代码

package com.example.administrator.jreduch06;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import com.example.administrator.jreduch06.fragment.FirstFragment;
import com.example.administrator.jreduch06.fragment.LeftFragment;
import com.example.administrator.jreduch06.fragment.SecondFragment;
public class Fragment1Activity extends AppCompatActivity implements LeftFragment.Myinterface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment1);
}
@Override
public void onchangeFragment(int which) {
if(which==1){
Fragment fragment1=new FirstFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl, fragment1)
.commit();
}else if(which==2){
Fragment fragment2=new SecondFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl,fragment2)
.commit();
}
}
}

2.fragment2布局及代码

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.jreduch06.Fragment2Activity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/one_fragment"
android:name="com.example.administrator.jreduch06.fragmentcallback.OneFragment"
>
</fragment>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl2"
android:layout_below="@+id/linearlatout"
>
</FrameLayout>
</RelativeLayout>

代码:

package com.example.administrator.jreduch06;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.administrator.jreduch06.fragment.FirstFragment;
import com.example.administrator.jreduch06.fragment.SecondFragment;
import com.example.administrator.jreduch06.fragmentcallback.OneFragment;
public class Fragment2Activity extends AppCompatActivity
implements OneFragment.OnFragmentInteractionListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment2);
}
@Override
public void changeFragment(int which) {
if(which==1){
Fragment fragment1=new FirstFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl2, fragment1)
.commit();
}else if(which==2){
Fragment fragment2=new SecondFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl2,fragment2)
.commit();
}
}
}

3.FirstFragment代码及布局

布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.jreduch06.fragment.FirstFragment"> 
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:id="@+id/tv"
android:text="我是Fragment1"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>

代码:

package com.example.administrator.jreduch06.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.administrator.jreduch06.R;
/**
* A simple {@link Fragment} subclass.
*/
public class SecondFragment extends Fragment {
public SecondFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false);
}
}

4.SecondFragment代码及布局

布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.jreduch06.fragment.SecondFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:text="我是Fragment2" />
</FrameLayout>

代码:

package com.example.administrator.jreduch06.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.administrator.jreduch06.R;
/**
* A simple {@link Fragment} subclass.
*/
public class FirstFragment extends Fragment {
public SecondFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
}

5.LeftFragment布局及代码

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#bece0d"
tools:context="com.example.administrator.jreduch06.fragment.LeftFragment">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一个Fragment"
android:id="@+id/bt1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二个Fragment"
android:id="@+id/bt2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="callback1"
android:id="@+id/bt3"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="callback2"
android:id="@+id/bt4"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隐藏"
android:id="@+id/bt5"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示"
android:id="@+id/bt6"
/>
</LinearLayout>

代码:

package com.example.administrator.jreduch06.fragment;
import android.content.Context;
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.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.administrator.jreduch06.R;
/**
* A simple {@link Fragment} subclass.
*/
public class LeftFragment extends Fragment {
private Fragment fragment1;
private Fragment fragment2;
private Myinterface myinterface ;
public LeftFragment() {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Myinterface) {
myinterface= (Myinterface) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_left, container, false);
Button bt1= (Button) view.findViewById(R.id.bt1);
Button bt2= (Button) view.findViewById(R.id.bt2);
Button bt3= (Button) view.findViewById(R.id.bt3);
Button bt4= (Button) view.findViewById(R.id.bt4);
Button bt5= (Button) view.findViewById(R.id.bt5);
Button bt6= (Button) view.findViewById(R.id.bt6);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "点击了按钮1", Toast.LENGTH_SHORT).show();
fragment1=new FirstFragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction fr=fm.beginTransaction();
fr.replace(R.id.fl,fragment1);
fr.commit();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragment2 = new SecondFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fr = fm.beginTransaction();
fr.replace(R.id.fl, fragment2);
fr.commit();
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myinterface.onchangeFragment(1);
}
});
bt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myinterface.onchangeFragment(2);
}
});
bt5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fragment1!=null&& !fragment1.isHidden()){
getFragmentManager().beginTransaction()
.hide(fragment1).commit();
}
if(fragment2!=null&& !fragment2.isHidden()){
getFragmentManager().beginTransaction()
.hide(fragment2).commit();
}
}
});
bt6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fragment1!=null&&fragment1.isHidden()){
getFragmentManager().beginTransaction()
.show(fragment1).commit();
}
if(fragment2!=null&& fragment2.isHidden()){
getFragmentManager().beginTransaction()
.hide(fragment2).commit();
}
}
});
return view;
}
public interface Myinterface {
void onchangeFragment(int which);
}
}

效果:

点击第一个按钮出现Fragment1.

点击第二个按钮出现Fragment2

点击第三个按钮出现Fragment1.(方法不同)

点击第四个按钮出现Fragment2.(方法不同)

点击隐藏,字条消失

点击显示,字条出现

以上所述是小编给大家介绍的Android 动态添加Fragment的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • 自定义Adapter并通过布局泵LayoutInflater抓取layout模板编辑每一个item实现思路

    自定义Adapter并通过布局泵LayoutInflater抓取layout模板编辑每一个item实现思路

    自定义Adapter并通过布局泵LayoutInflater抓取layout模板编辑每一个item,下面我们开始学习这一篇的内容,感兴趣的朋友可以了解下哈
    2013-06-06
  • Android 自定义通用的loadingview实现代码

    Android 自定义通用的loadingview实现代码

    本篇文章主要介绍了Android 自定义通用的loadingview实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-01-01
  • Android 10.0截屏流程详解

    Android 10.0截屏流程详解

    这篇文章主要为大家介绍了Android 10.0截屏流程详解,通常未通过特殊定制的 Android 系统,截屏都是经过同时按住音量下键和电源键来截屏,本篇文章就只讨论使用这些特殊按键来进行截屏
    2023-06-06
  • Android手势识别器GestureDetector使用详解

    Android手势识别器GestureDetector使用详解

    这篇文章主要为大家详细介绍了Android手势识别器GestureDetector的使用方法解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • 理解Android中的自定义属性

    理解Android中的自定义属性

    这篇文章主要介绍了理解Android中的自定义属性,在android相关应用开发过程中,固定的一些属性可能满足不了开发的需求,所以需要自定义控件与属性,本文将以此问题进行详细介绍,需要的朋友可以参考下
    2016-01-01
  • Kotlin协程概念原理与使用万字梳理

    Kotlin协程概念原理与使用万字梳理

    协程的作用是什么?协程是一种轻量级的线程,解决异步编程的复杂性,异步的代码使用协程可以用顺序进行表达,文中通过示例代码介绍详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-08-08
  • Android相机启动加速详解

    Android相机启动加速详解

    本篇文章给大家详细分析了Android实现相机启动加速的相关知识点内容以及实例代码,有兴趣的朋友参考下。
    2018-07-07
  • Android实现图片滚动效果

    Android实现图片滚动效果

    这篇文章主要为大家详细介绍了Android实现图片滚动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • Android自定义可标记日历效果

    Android自定义可标记日历效果

    这篇文章主要为大家详细介绍了Android自定义可标记日历效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • android语音即时通讯之录音、播放功能实现代码

    android语音即时通讯之录音、播放功能实现代码

    这篇文章主要为大家详细介绍了android语音即时通讯之录音、播放功能的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07

最新评论