Fragment(一)

MainActivity:

10年积累的做网站、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有富源免费网站建设让你可以放心的选择与我们合作。

package com.example.fragmentmanage;

import android.app.Activity;

import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

private Button btn1;

private Button btn2;

private FirstFragment firstFragment;

private SencondFragment sencondFragment;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

setDefaultFragment();

}

private void initView() {

btn1 = (Button) findViewById(R.id.btn1);

btn2 = (Button) findViewById(R.id.btn2);

btn1.setOnClickListener(this);

btn2.setOnClickListener(this);

}

private void setDefaultFragment() {

FragmentManager manager = getFragmentManager();

FragmentTransaction transaction = manager.beginTransaction();

firstFragment = new FirstFragment();

transaction.replace(R.id.fragme_layout, firstFragment);

transaction.commit();

}

@Override

public void onClick(View arg0) {

FragmentManager manager = getFragmentManager();

FragmentTransaction transaction = manager.beginTransaction();

switch (arg0.getId()) {

case R.id.btn1:

if (firstFragment == null) {

firstFragment = new FirstFragment();

}

transaction.replace(R.id.fragme_layout, firstFragment);

break;

case R.id.btn2:

if (sencondFragment == null) {

sencondFragment = new SencondFragment();

}

transaction.replace(R.id.fragme_layout, sencondFragment);

break;

default:

break;

}

transaction.commit();

}

}

activity_main:

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

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

        <Button

            android:id="@+id/btn1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="btn1" />

        <Button

            android:id="@+id/btn2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="btn2" />

    </LinearLayout>

    <FrameLayout

        android:id="@+id/fragme_layout"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</RelativeLayout>

FirstFragment:

package com.example.fragmentmanage;

import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

public class FirstFragment extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_first, null);

}

}

SencondFragment:

package com.example.fragmentmanage;

import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

public class FirstFragment extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_first, null);

}

}

当前文章:Fragment(一)
链接URL:/article20/jogsjo.html

成都网站建设公司_创新互联,为您提供ChatGPT面包屑导航标签优化微信公众号动态网站网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设