Android中Fragment的使用,通过使用帧布局FrameLayout设置页面布局,并设置button按钮事件监听等

Activity_Fragment

package com.example.myapplication_one;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity_Fragment extends AppCompatActivity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_fragment);
        /*
         *  1.创建Fragment实例
         *  2.获取FragmentManager:1、getSupportFragmentManager();2、getFragmentManager();
         *  3.开启事务
         *  4.添加、替换Fragment
         *  5.提交事务
         */

        button = (Button) findViewById(R.id.button_fragment1);
        
        replaceFragment(new MyFragment2());//默认去加载一个MyFragment2()
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                replaceFragment(new MyFragment3());//从第二个MyFragment2()替换成MyFragment3()
            }
        });

    }

    public void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.framelayout1,fragment);//framelayout1使用的是帧布局中的id
        //模拟返回栈
        transaction.addToBackStack(null); //模拟返回栈,再次点击按钮的时候,返回原本的布局,但是我这里没起作用,不知道为什么。
        transaction.commit();
    }
}

layout_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"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#0000ff"
        android:text="主页面" />

    <LinearLayout
        android:id="@+id/ln_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <!--    name属性是用来寻找自己设置的页面布局的-->

<!--    以静态的方式加载fragment    -->

        <fragment
            android:id="@+id/fragment_1"
            android:name="com.example.myapplication_one.MyFragment1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <!--<fragment
            android:id="@+id/fragment_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:name="com.example.myapplication_one.MyFragment2"/>-->

<!--  帧布局  -->
        <FrameLayout
            android:id="@+id/framelayout1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
        ></FrameLayout>

    </LinearLayout>

</LinearLayout>

MyFragment1.java

package com.example.myapplication_one;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        /*
         * resourse:第一个参数是页面布局文件
         * root:第二个参数,父类
         * attachToRoot:第三个参数,是否以父类容器的形式添加(是否保留当前布局文件的当前属性)
         */
        View view = inflater.inflate(R.layout.layout_fragment1,
                container,false);//当前的页面布局,当前的布局文件(在写自己的适配器的时候使用过)
                            //false没有保留父类容器的布局样式

        return view;//返回当前的布局文件
    }
}

layout_fragment1.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"
    android:orientation="vertical"
    android:background="#ff0">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment1" />
<!--    android:layout_weight="1"-->

    <Button
        android:id="@+id/button_fragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment1" />
<!--    android:layout_weight="1"-->

</LinearLayout>

MyFragment2.java

package com.example.myapplication_one;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        /*
         * resourse:第一个参数是页面布局文件
         * root:第二个参数,父类
         * attachToRoot:第三个参数,是否以父类容器的形式添加(是否保留当前布局文件的当前属性)
         */
        View view = inflater.inflate(R.layout.layout_fragment2,
                container,false);//当前的页面布局,当前的布局文件(在写自己的适配器的时候使用过)

        return view;//返回当前的布局文件
    }
}

layout_fragment2.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"
    android:orientation="vertical"
    android:background="#ff0000">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment2" />
<!--    android:layout_weight="1"-->

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment2" />
<!--    android:layout_weight="1"-->

</LinearLayout>

MyFragment3.java

package com.example.myapplication_one;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment3 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        /*
         * resourse:第一个参数是页面布局文件
         * root:第二个参数,父类
         * attachToRoot:第三个参数,是否以父类容器的形式添加(是否保留当前布局文件的当前属性)
         */
        View view = inflater.inflate(R.layout.layout_fragment3,
                container,false);//当前的页面布局,当前的布局文件(在写自己的适配器的时候使用过)

        return view;//返回当前的布局文件
    }
}

layout_fragment3.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"
    android:orientation="vertical"
    android:background="#0ff">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment3" />
<!--    android:layout_weight="1"-->

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment3" />
<!--    android:layout_weight="1"-->

</LinearLayout>

最终运行结果:

点击FRAGMENT1按钮:

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要获取ViewPagerFragment里的Button按钮设置监听,可以通过以下步骤实现: 1. 在Fragment布局文件定义一个Button,例如: ``` <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" /> ``` 2. 在FragmentJava代码使用findViewById()方法获取Button对象,并设置点击监听器,例如: ``` public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment_layout, container, false); Button myButton = view.findViewById(R.id.myButton); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理按钮点击事件 } }); return view; } } ``` 3. 在Activity,获取ViewPager当前显示的Fragment对象,调用其findViewById()方法获取Button对象,并设置监听器,例如: ``` public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity_layout); ViewPager viewPager = findViewById(R.id.viewPager); // ... // 获取ViewPager当前显示的Fragment对象 MyFragment currentFragment = (MyFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + viewPager.getCurrentItem()); if (currentFragment != null) { Button myButton = currentFragment.getView().findViewById(R.id.myButton); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理按钮点击事件 } }); } } } ``` 注意:在ViewPagerFragment的创建和销毁是由ViewPager管理的,因此要获取当前显示的Fragment对象,需要使用getSupportFragmentManager().findFragmentByTag()方法,并指定正确的tag值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值