最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 動態(tài)添加Fragment的實例代碼

 更新時間:2016年08月10日 14:18:30   作者:孺子-小哥  
這篇文章主要介紹了Android 動態(tài)添加Fragment的實例代碼的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

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">
<fragment
android: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>
<fragment
android: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>
<FrameLayout
android: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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment1);
}
@Override
public 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">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/one_fragment"
android:name="com.example.administrator.jreduch06.fragmentcallback.OneFragment"
>
</fragment>
<FrameLayout
android: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 AppCompatActivity
implements OneFragment.OnFragmentInteractionListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment2);
}
@Override
public 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"> 
<TextView
android: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
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return 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">
<TextView
android: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
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return 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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一個Fragment"
android:id="@+id/bt1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二個Fragment"
android:id="@+id/bt2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="callback1"
android:id="@+id/bt3"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="callback2"
android:id="@+id/bt4"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隱藏"
android:id="@+id/bt5"
/>
<Button
android: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() {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Myinterface) {
myinterface= (Myinterface) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public 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() {
@Override
public 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() {
@Override
public 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() {
@Override
public void onClick(View v) {
myinterface.onchangeFragment(1);
}
});
bt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myinterface.onchangeFragment(2);
}
});
bt5.setOnClickListener(new View.OnClickListener() {
@Override
public 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() {
@Override
public 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);
}
}

效果:

點擊第一個按鈕出現(xiàn)Fragment1.

點擊第二個按鈕出現(xiàn)Fragment2

點擊第三個按鈕出現(xiàn)Fragment1.(方法不同)

點擊第四個按鈕出現(xiàn)Fragment2.(方法不同)

點擊隱藏,字條消失

點擊顯示,字條出現(xiàn)

以上所述是小編給大家介紹的Android 動態(tài)添加Fragment的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 自定義Adapter并通過布局泵LayoutInflater抓取layout模板編輯每一個item實現(xiàn)思路

    自定義Adapter并通過布局泵LayoutInflater抓取layout模板編輯每一個item實現(xiàn)思路

    自定義Adapter并通過布局泵LayoutInflater抓取layout模板編輯每一個item,下面我們開始學(xué)習(xí)這一篇的內(nèi)容,感興趣的朋友可以了解下哈
    2013-06-06
  • Android 自定義通用的loadingview實現(xiàn)代碼

    Android 自定義通用的loadingview實現(xiàn)代碼

    本篇文章主要介紹了Android 自定義通用的loadingview實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-01-01
  • Android 10.0截屏流程詳解

    Android 10.0截屏流程詳解

    這篇文章主要為大家介紹了Android 10.0截屏流程詳解,通常未通過特殊定制的 Android 系統(tǒng),截屏都是經(jīng)過同時按住音量下鍵和電源鍵來截屏,本篇文章就只討論使用這些特殊按鍵來進行截屏
    2023-06-06
  • Android手勢識別器GestureDetector使用詳解

    Android手勢識別器GestureDetector使用詳解

    這篇文章主要為大家詳細介紹了Android手勢識別器GestureDetector的使用方法解,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 理解Android中的自定義屬性

    理解Android中的自定義屬性

    這篇文章主要介紹了理解Android中的自定義屬性,在android相關(guān)應(yīng)用開發(fā)過程中,固定的一些屬性可能滿足不了開發(fā)的需求,所以需要自定義控件與屬性,本文將以此問題進行詳細介紹,需要的朋友可以參考下
    2016-01-01
  • Kotlin協(xié)程概念原理與使用萬字梳理

    Kotlin協(xié)程概念原理與使用萬字梳理

    協(xié)程的作用是什么?協(xié)程是一種輕量級的線程,解決異步編程的復(fù)雜性,異步的代碼使用協(xié)程可以用順序進行表達,文中通過示例代碼介紹詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-08-08
  • Android相機啟動加速詳解

    Android相機啟動加速詳解

    本篇文章給大家詳細分析了Android實現(xiàn)相機啟動加速的相關(guān)知識點內(nèi)容以及實例代碼,有興趣的朋友參考下。
    2018-07-07
  • Android實現(xiàn)圖片滾動效果

    Android實現(xiàn)圖片滾動效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)圖片滾動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Android自定義可標(biāo)記日歷效果

    Android自定義可標(biāo)記日歷效果

    這篇文章主要為大家詳細介紹了Android自定義可標(biāo)記日歷效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • android語音即時通訊之錄音、播放功能實現(xiàn)代碼

    android語音即時通訊之錄音、播放功能實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了android語音即時通訊之錄音、播放功能的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評論

黄龙县| 潼南县| 大冶市| 邢台县| 金寨县| 邓州市| 中山市| 淮滨县| 贺兰县| 怀柔区| 建湖县| 镇江市| 同仁县| 博野县| 满城县| 宁强县| 黑水县| 寿光市| 台山市| 大同市| 娄烦县| 双柏县| 潼南县| 文登市| 滦南县| 肃宁县| 岚皋县| 达孜县| 郓城县| 贺兰县| 芦溪县| 河北区| 清远市| 邯郸县| 佛山市| 晋州市| 改则县| 清水县| 恩平市| 七台河市| 娱乐|