Fragment动态创建

今天写个fragment动态创建,。

不多说,直接上代码:

activity_fragment布局

<?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">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/fragment_left"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="左边Fragment" />

        <Button
            android:id="@+id/fragment_right"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="右边Fragment" />
    </LinearLayout>

</LinearLayout>
在加上两个简单的布局:

第一个布局activity_fourth

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <AnalogClock
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center_horizontal" />

            <Chronometer
                android:id="@+id/chronometer_fourth"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />

            <TextView
                android:id="@+id/tv_startTime_fourth"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/btn_ripple"
                android:text="开始" />

            <DatePicker
                android:id="@+id/datePicker"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />

            <TimePicker
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:timePickerMode="spinner"></TimePicker>
            <CalendarView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></CalendarView>
        </LinearLayout>
    </ScrollView>
</LinearLayout>
第二个布局activity_six
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/g10"
        android:background="@drawable/btn_ripple"
        android:gravity="center"
        android:padding="@dimen/g10"
        android:text="普通Dialog" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/g10"
        android:background="@drawable/btn_ripple"
        android:gravity="center"
        android:padding="@dimen/g10"
        android:text="列表Dialog" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/g10"
        android:background="@drawable/btn_ripple"
        android:gravity="center"
        android:padding="@dimen/g10"
        android:text="单选Dialog" />

    <TextView
        android:id="@+id/tv4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/g10"
        android:background="@drawable/btn_ripple"
        android:gravity="center"
        android:padding="@dimen/g10"
        android:text="自定义Dialog" />
</LinearLayout>

现在写第一个fragment

public class Fragment1 extends Fragment {
    private  View view;
    private TextView textView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
         view = inflater.inflate(R.layout.activity_six,null);
        textView = view.findViewById(R.id.tv1);
        return view;


    }


    @Override
    public void onResume() {
        super.onResume();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("李嘉诚");
            }
        });
    }
}
第二个fragment
public class Fragment2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.activity_fourth,null);
        return view;
    }
}

现在写activity的实现

public class FragmentActivity extends AppCompatActivity implements View.OnClickListener {
    private Button left, right;
    private FragmentManager fragmentManager;

    private Fragment fragment1, fragment2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        initView();

        initData();

    }

    public void initView() {
        left = findViewById(R.id.fragment_left);
        right = findViewById(R.id.fragment_right);

        left.setOnClickListener(this);
        right.setOnClickListener(this);
    }

    public void initData() {
        fragment1 = new Fragment1();


        fragmentManager = getFragmentManager();//获取fragment的管理器
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//通过管理器获取fragment的事物

        fragmentTransaction.add(R.id.fragment_frameLaout, fragment1).show(fragment1).commit();
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//通过管理器获取fragment的事物
        //隐藏所有的fragment
        hintAllFragment(fragmentTransaction);
        switch (v.getId()) {
            case R.id.fragment_left:
                if (fragment1 == null) {
                    fragment1 = new Fragment1();
                    fragmentTransaction.add(R.id.fragment_frameLaout, fragment1);
                }
                //fragmentTransaction.replace(R.id.fragment_frameLaout, fragment1);
                fragmentTransaction.show(fragment1);
                break;
            case R.id.fragment_right:
                if (fragment2 == null) {
                    fragment2 = new Fragment2();
                    fragmentTransaction.add(R.id.fragment_frameLaout, fragment2);
                }
                fragmentTransaction.show(fragment2);
                break;
        }
        fragmentTransaction.commit();

    }

    private void hintAllFragment(FragmentTransaction fragmentTransaction) {
        if(fragment1!=null)fragmentTransaction.hide(fragment1);
        if(fragment2!=null)fragmentTransaction.hide(fragment2);
    }

}
到这里我们的功能就基本实现了,是不是还挺简单的,。希望能对你有所帮助,大家共同进步哈。
源码下载地址:https://download.csdn.net/download/weixin_42267745/10438573






 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值