国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 動態添加Fragment的實例代碼

2019-12-12 05:44:41
字體:
來源:轉載
供稿:網友

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"><fragmentandroid: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><fragmentandroid: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><FrameLayoutandroid: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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_fragment1);}@Overridepublic 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"><fragmentandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/one_fragment"android:name="com.example.administrator.jreduch06.fragmentcallback.OneFragment"></fragment><FrameLayoutandroid: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 AppCompatActivityimplements OneFragment.OnFragmentInteractionListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_fragment2);}@Overridepublic 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"> <TextViewandroid: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}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn 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"><TextViewandroid: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}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn 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"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="第一個Fragment"android:id="@+id/bt1"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="第二個Fragment"android:id="@+id/bt2"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="callback1"android:id="@+id/bt3"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="callback2"android:id="@+id/bt4"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="隱藏"android:id="@+id/bt5"/><Buttonandroid: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() {}@Overridepublic void onAttach(Context context) {super.onAttach(context);if (context instanceof Myinterface) {myinterface= (Myinterface) context;} else {throw new RuntimeException(context.toString()+ " must implement OnFragmentInteractionListener");}}@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onClick(View v) {myinterface.onchangeFragment(1);}});bt4.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {myinterface.onchangeFragment(2);}});bt5.setOnClickListener(new View.OnClickListener() {@Overridepublic 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() {@Overridepublic 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的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 进贤县| 亳州市| 安泽县| 黄陵县| 东源县| 清镇市| 阜新市| 柞水县| 辽宁省| 东阿县| 宣威市| 丽水市| 苍梧县| 南华县| 民乐县| 锦屏县| 黄冈市| 泰来县| 克什克腾旗| 盐城市| 东光县| 修水县| 易门县| 高陵县| 望谟县| 潼南县| 科尔| 隆尧县| 肇东市| 浦城县| 墨玉县| 浦北县| 莱州市| 平和县| 涟源市| 兴和县| 新宁县| 时尚| 两当县| 吕梁市| 平利县|