动态添加Fragment

先看下效果图:

GIF.gif

首先,创建left_fragment.xml ,right_fragment.xml 和 another_right_fragment.xml布局:

<Button
    android:layout_gravity="center_horizontal"
    android:text="Button"
    android:textAllCaps="false"
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
left布局只有一个按钮
<TextView
    android:layout_gravity="center_horizontal"
    android:text="this is right fragment"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
right布局只有一个TextView,并给根布局设置背景为绿色:android:background="#00ff00"
<TextView
    android:layout_gravity="center_horizontal"
    android:text="this is right fragment"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
another_right_fragment布局也是只有一个TextView,并给根布局设置背景为黄色: android:background="#ffff00"

2 . 分别创建 LeftFragment ,RightFragment ,AnotherRightFragment并让他们继承Fragment

最好选择V4包下的Fragment

public class LeftFragment extends Fragment {
@Nullable
@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
   View view = LayoutInflater.from(getContext()).inflate(R.layout.left_farment,container,false);
    return view;
} }
LeftFragment 与 RightFragment,AnotherRightFragment 大致一样这里只给出一个

3 . 修改 activity_main.xml. 将右侧Fragment放在了一个 FrameLayout 中

< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
   android:id="@+id/left_fragment"
   android:layout_width="0dp"
   android:layout_weight="1"
   android:name = "com.example.fragment_4zhang.fragment.LeftFragment"
   android:layout_height="match_parent"/>

<FrameLayout
    android:id="@+id/right_layout"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/right_fragment"
        android:layout_width="match_parent"
        android:name = "com.example.fragment_4zhang.fragment.RightFragment"
        android:layout_height="match_parent"/>
    </FrameLayout>
</LinearLayout>

4 .修改MainActivity 中的代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:
            AnotherRightFragment fragment = new AnotherRightFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.right_layout, fragment);
            transaction.commit();
            break;
    }
}  }

。。。

5 .动态添加Fragment步骤

  • 创建待添加的Fragment实例。
  • 获取到 FragmentManager,在Activity中可以直接调用getFragmentManager()方法得到。
  • 开启一个事务,通过调用 beginTransaction()方法开启。
  • 向容器内加入Fragment,一般使用 replace()方法实现,需要- 传入容器的 id 和待添加的Fragment实例。
  • 提交事务,调用 commit()方法来完成。

代码已上传Github:https://github.com/15503737504/DynamicAddFragment

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,458评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 7,287评论 0 17
  • 1.创建一个类继承Fragment,复写onCreateView方法。例如: 2.在MainActivity中创建...
    前端develop阅读 6,557评论 0 0
  • “让王爷一人服蛊?...对王爷的身体可有伤害?” 司徒静犹豫着问道。 “禀娘娘,除了形体上的虚弱枯槁外全无伤害。”...
    兮云阅读 500评论 0 6
  • 自安古城位于通化市的治安村,古城始建于南北朝,高句丽族在此建立一个王国。目前流经身旁的是哈泥河,哈泥河是浑江的支流...
    自安古城先生阅读 641评论 0 0

友情链接更多精彩内容