您的位置:首页 > 移动开发 > Android开发

Fragment的初步使用

2016-03-29 17:11 579 查看
静态的Fragmet相当于View存在Activity中。

实现方法如下:(activity_main.xml)

<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.hzx.activity.MainActivity" >

<fragment
android:id="@+id/fragment_title"
android:name="com.hzx.fragment.TitleFragment"
android:layout_width="fill_parent"
android:layout_height="45dp"/>

<fragment
android:id="@+id/fragment_contact"
android:name="com.hzx.fragment.ContactFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

</RelativeLayout>


fragment_title.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar">

<ImageButton
android:id="@+id/id_title_left_personbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/fragmentdevicelist_user_selected"/>

<TextView
android:id="@+id/id_title_center_titletv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#FFF"
android:layout_margin="3dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="@string/title_weixin"/>
</RelativeLayout>


TitleFragment.java实现:

package com.hzx.fragment;

import com.hzx.demo1android.R;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

public class TitleFragment extends Fragment {

private static String LOGTAG = "TitleFragment_LOG";
private ImageButton person_ImgBtn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e(LOGTAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_title, container, false);

person_ImgBtn = (ImageButton) view.findViewById(R.id.id_title_left_personbtn);

person_ImgBtn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "this is not chat title!", Toast.LENGTH_SHORT).show();
}

});

return view;
}

}


fragment_centent.xml:

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

<TextView
android:id="@+id/id_centent_cententTV"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:text="@string/centent_tv"/>
</LinearLayout>


CententFragment实现:

package com.hzx.fragment;

import com.hzx.demo1android.R;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CententFragment extends Fragment {

private static String LOGTAG = "CententFragment_LOG";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e(LOGTAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_centent, container, false);
return view;
}

}


MainActivity实现:

package com.hzx.activity;

import com.hzx.demo1android.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

public class MainActivity extends Activity {

private static String LOGTAG = "ManActivity_LOG";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.e(LOGTAG, "MainActivity onCreate");
setContentView(R.layout.activity_main);
}


这是静态的Fragmet实现,就像两个视图一样显示在Activity上。

常用的是动态的Fragment,需要提交不同的Fragment,并且切换不同的Fragment。具体实现如下。

activity_main.xml

<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.hzx.activity.MainActivity" >

<fragment
android:id="@+id/fragment_title"
android:name="com.hzx.fragment.TitleFragment"
android:layout_width="fill_parent"
android:layout_height="45dp"/>

<include
android:id="@+id/id_layout_bottombar"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:layout_alignParentBottom="true"
layout="@layout/bottom_bar"/>

<FrameLayout
android:id="@+id/id_fragment_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/fragment_title"
android:layout_above="@id/id_layout_bottombar"/>

</RelativeLayout>


bottom_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal"
android:weightSum="4"
android:gravity="center"
android:background="@drawable/title_bar" >

<RadioButton
android:id="@+id/id_weixin_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/fragmentdevicelist_user_selected"
android:gravity="center_horizontal"
android:text="@string/chat_str"
android:textColor="#fff"/>

<RadioButton
android:id="@+id/id_contacts_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/setting_white_selected"
android:gravity="center_horizontal"
android:text="@string/contacts_str"
android:textColor="#fff"/>

<RadioButton
android:id="@+id/id_me_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/setting_white_selected"
android:gravity="center_horizontal"
android:text="@string/me_str"
android:textColor="#fff"/>

<RadioButton
android:id="@+id/id_setting_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/setting_white_selected"
android:gravity="center_horizontal"
android:text="@string/setting_str"
android:textColor="#fff"/>

</RadioGroup>


styles.xml中添加如下代码:

<style name="main_tab_bottom">
<item name="android:gravity">center_horizontal</item>
<item name="android:button">@null</item>
<item name="android:layout_weight">1.0</item>
<item name="android:singleLine">true</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">3dp</item>
<item name="android:layout_width">0dp</item>
<item name="android:drawablePadding">3dp</item>
</style>


修改MainActivity.java

package com.hzx.activity;

import com.hzx.demo1android.R;
import com.hzx.fragment.CententFragment;
import com.hzx.fragment.ContactFragment;
import com.hzx.fragment.MeFragment;
import com.hzx.fragment.SettingFragment;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RadioButton;

public class MainActivity extends Activity implements OnClickListener{

private static String LOGTAG = "ManActivity_LOG";
private RadioButton chat_rb;
private RadioButton contacts_rb;
private CententFragment cententFragment;
private ContactFragment contactFragment;
private RadioButton me_rb;
private RadioButton setting_rb;
private MeFragment meFragment;
private SettingFragment settingFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.e(LOGTAG, "MainActivity onCreate");
setContentView(R.layout.activity_main);

chat_rb = (RadioButton) findViewById(R.id.id_weixin_rb);
chat_rb.setOnClickListener(this);
contacts_rb = (RadioButton) findViewById(R.id.id_contacts_rb);
contacts_rb.setOnClickListener(this);

me_rb = (RadioButton) findViewById(R.id.id_me_rb);
me_rb.setOnClickListener(this);
setting_rb = (RadioButton) findViewById(R.id.id_setting_rb);
setting_rb.setOnClickListener(this);

setDefaultFragment();
}

private void setDefaultFragment() {
cententFragment = new CententFragment();
getFragmentManager().beginTransaction().replace(R.id.id_fragment_content, cententFragment).commit();
}

@Override
public void onClick(View v) {
FragmentTransaction fTransaction = getFragmentManager().beginTransaction();

switch (v.getId()) {
case R.id.id_weixin_rb:
if (cententFragment == null) {
cententFragment = new CententFragment();
}
fTransaction.replace(R.id.id_fragment_content, cententFragment);

break;

case R.id.id_contacts_rb:
if (contactFragment == null) {
contactFragment = new ContactFragment();
}
fTransaction.replace(R.id.id_fragment_content, contactFragment);
break;
case R.id.id_me_rb:
if (meFragment == null) {
meFragment = new MeFragment();
}
fTransaction.replace(R.id.id_fragment_content, meFragment);
break;
case R.id.id_setting_rb:
if (settingFragment == null) {
settingFragment = new SettingFragment();
}
fTransaction.replace(R.id.id_fragment_content, settingFragment);
break;
default:
break;
}
fTransaction.commit();
}
}


ContactFragment,MeFragment,SettingFragment可以仿照CententFragment写。

最后得到得图片如下:



这里有个知识点:

View view = inflater.inflate(R.layout.fragment_centent, container, false);


inflater.inflate()是加载xml的,跟findViewById()不同(它是加载控件的),inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。

关于该方法的参数含义,在网上找到一篇解释:

关于LayoutInflater类inflate(int resource, ViewGroup root, boolean attachToRoot)方法三个参数的含义

resource:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。

root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。

attachToRoot:是否将root附加到布局文件的根视图上。

检验概况,初步了解了Fragment的使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android