日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

浅谈对Fragment的认识

發布時間:2025/4/9 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅谈对Fragment的认识 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

首先聲明我是一個菜鳥。下面也只是我個人的見解。

我覺得不管學習什么,要把握三個大的方向,一是,這個東東是什么,二是,這個東東怎么用,三是這個這個東東為什么這樣用。

因為我也是一個初學者,對于第三個境界還達不到,只能勉強達到前面的兩個。下面介紹一下fragment!

1.從網上看到,Fragment的出現是為了解決一個APP可以同時適應手機和平板而產生的,可以把他理解為Activity的界面的組成部分。

換句話說,再用到Fragment時,把Activity界面想象成是由Fragment以及其他控件組成的,你可以通過對Fragment的修改(比如動態添加,替換,刪除某個fragment),完善相應功能。

2.如果你會使用了,上面說的就沒什么用了,因為每個人的理解都不一樣,還是說最關鍵的怎么使用吧!

在使用之前必須了解一下Fragment的生命周期,

Fragment必須依存于Activity的,Activity生命周期會直接影響到Fragmnet 的生命周期。

?

然后對他的生命周期簡單了解:

onAttach(Activity) ? ? ? ? ? ? //這個是Fragment與Activity關聯時調用,attach的漢語意思是伴隨,從屬,聯在一起。

onCreateView() ? ? ? ? ? //創建Fragment視圖

onActivityCreated() ? ? ? ? ?//當Activity中onCreated()方法返回時調用

onDestory() ? ? ? ? ? ? ? ? ? ? ?//當Fragment被移除時調用

onDetach()       //當Activity和Fragment取消關聯時調用

第一步:先在Activity布局文件中添加FragmentLayout,正常設置id,layout_height,layout_weight即可(這里添加Fragment也可以但是跟Fragment是有區別的,這個自己去查一下吧,屬于細節)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame"
android:layout_weight="9"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_boke"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="博客"/>
<TextView
android:id="@+id/tv_friend"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="好友"/>
<TextView
android:id="@+id/tv_setting"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="設置"/>
</LinearLayout>
</LinearLayout>

第二步,添加Fragment類繼承Fragment,并重寫OnCreateView方法,我這里添加了三個,分別對應上面的博客,好友,設置

BokeFragment

package com.example.administrator.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017-08-21 .
*/
public class BokeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.boke,container,false);
}
}
FriendFragment package com.example.administrator.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017-08-21 .
*/
public class FriendFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.friend,container,false);
}
}
SettingFragment package com.example.administrator.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017-08-21 .
*/

public class SettingFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.setting,container,false);
}
}
第三步:設置每個Fragment對應的布局;這里我列舉一個博客的就OK,好友和設置跟博客的類似。

boke.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:text="我是癡王,我會加油!"
android:textAlignment="center"
android:textColor="#FF0000" />

</LinearLayout>
第四步就是在Activity中對Fragment做需要的操作。 package com.example.administrator.fragment;

import android.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.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView boke;
private TextView friend;
private TextView setting;
private BokeFragment bokefragment;
private FriendFragment friendfragment;
private SettingFragment settingfragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boke = (TextView) findViewById(R.id.tv_boke);
friend = (TextView) findViewById(R.id.tv_friend);
setting = (TextView) findViewById(R.id.tv_setting);
boke.setOnClickListener(this);
friend.setOnClickListener(this);
setting.setOnClickListener(this);
}
public void onClick(View view){
android.app.FragmentManager fm = getFragmentManager();
android.app.FragmentTransaction transaction = fm.beginTransaction();
switch (view.getId()){
case R.id.tv_boke:
bokefragment = new BokeFragment();
transaction.replace(R.id.frame,bokefragment);
break;
case R.id.tv_friend:
friendfragment = new FriendFragment();
transaction.replace(R.id.frame,friendfragment);
break;
case R.id.tv_setting:
settingfragment = new SettingFragment();
transaction.replace(R.id.frame,settingfragment);
break;
}
transaction.commit();
}
}
下面是運行結果
不會弄動態的圖片,有人教教我嗎?我就搞兩個靜態的圖片吧
1.點擊博客出現的結果:

2.點擊好友出現的界面:

轉載于:https://www.cnblogs.com/chiwang/p/7410035.html

總結

以上是生活随笔為你收集整理的浅谈对Fragment的认识的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。