浅谈对Fragment的认识
?
首先聲明我是一個菜鳥。下面也只是我個人的見解。
我覺得不管學習什么,要把握三個大的方向,一是,這個東東是什么,二是,這個東東怎么用,三是這個這個東東為什么這樣用。
因為我也是一個初學者,對于第三個境界還達不到,只能勉強達到前面的兩個。下面介紹一下fragment!
1.從網(wǎng)上看到,Fragment的出現(xiàn)是為了解決一個APP可以同時適應手機和平板而產(chǎn)生的,可以把他理解為Activity的界面的組成部分。
換句話說,再用到Fragment時,把Activity界面想象成是由Fragment以及其他控件組成的,你可以通過對Fragment的修改(比如動態(tài)添加,替換,刪除某個fragment),完善相應功能。
2.如果你會使用了,上面說的就沒什么用了,因為每個人的理解都不一樣,還是說最關(guān)鍵的怎么使用吧!
在使用之前必須了解一下Fragment的生命周期,
Fragment必須依存于Activity的,Activity生命周期會直接影響到Fragmnet 的生命周期。
?
然后對他的生命周期簡單了解:
onAttach(Activity) ? ? ? ? ? ? //這個是Fragment與Activity關(guān)聯(lián)時調(diào)用,attach的漢語意思是伴隨,從屬,聯(lián)在一起。
onCreateView() ? ? ? ? ? //創(chuàng)建Fragment視圖
onActivityCreated() ? ? ? ? ?//當Activity中onCreated()方法返回時調(diào)用
onDestory() ? ? ? ? ? ? ? ? ? ? ?//當Fragment被移除時調(diào)用
onDetach() //當Activity和Fragment取消關(guān)聯(lián)時調(diào)用
第一步:先在Activity布局文件中添加FragmentLayout,正常設置id,layout_height,layout_weight即可(這里添加Fragment也可以但是跟Fragment是有區(qū)別的,這個自己去查一下吧,屬于細節(jié))
<?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();
}
}
下面是運行結(jié)果
不會弄動態(tài)的圖片,有人教教我嗎?我就搞兩個靜態(tài)的圖片吧
1.點擊博客出現(xiàn)的結(jié)果: 2.點擊好友出現(xiàn)的界面:
轉(zhuǎn)載于:https://www.cnblogs.com/chiwang/p/7410035.html
總結(jié)
以上是生活随笔為你收集整理的浅谈对Fragment的认识的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对spring cloud config
- 下一篇: 异步调用可以转化为同步调用吗?