今天学习了一下Fragment 写了几个Demo 下面从两个方面总结一下fragment 第第一是fragment的基本语法 怎么添加 移除到activity。第二是fragment的实际应用:仿新浪微博的一个界面
1. Fragment基础知识
添加fragment到activity的两种方法:
第一种: 静态添加,在activity的布局中直接插入fragment
<fragment
android:name="com.example.demo2.fragment1"
android:id="@+id/fragment1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
几个fragment可以同时存在的 通过android:layout_weight来控制他们的比重 一般用于平板的开发
然后编写我们的fragment:
publicView onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
returninflater.inflate(R.layout.example_fragment, container, false);
}
onCreateView()参数中的container是存放fragment的layout的ViewGroup对象。saveInstanceState参数是一个Bundle,跟activity的onCreate()中Bundle差不多,用于状态恢复。但是fragment的onCreate()中也有Bundle参数,所以此处的Bundle中存放的数据与onCreate()中存放的数据还是不同的。
Inflate()方法中有三个参数:
<1> layout的资源ID;
<2> 存放fragment的layout的ViewGroup;
<3> 布尔数据表示是否在创建fragment的layout期间,把layout附加到container上(在这个例子中,因为系统已经把layout插入到container中了,所以值为false,如果为true会导致在最终的layout中创建多余的ViewGroup)。
<fragment>中声明一个fragment。当系统创建上例中的layout时,它实例化每一个fragment,然后调用它们的onCreateView()方法,以获取每个fragment的layout。系统把fragment返回的view对象插入到<fragment>元素的位置,直接代替<fragment>元素。
那怎么对fragment进行操作呢?由于fragment已经作为标签写进了activity的布局文件 可以用Fragment frag1 = getFragmentManager().findFragmentById(R.id.fragment1); 直接拿到Fragment的实例 然后对其操作,操作方法在下面的动态添加讲到。
第二种:在activity中动态添加:
对上面的静态方法进行改进 activity的布局文件不需要写fragment的标签 用android.R.layout.content作为fragment的容器 这么写:
public class MainActivity extends Activity {
private Fragment frag1,frag2,frag3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frag1 = new frag1();// 此处得到fragment的方法区别于上面的静态方法
frag2 = new frag2();
frag3 = new frag3();
getFragmentManager().beginTransaction().add(android.R.id.content,frag1, "frag1").commit(); //操作方法
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
其他的操作方法:
getFragmentManager().beginTransaction().hide(frag1).hide(frag2).hide(frag3).show(frag1).commit();
注意: 1. 在显示某个fragment之前先hide所有的fragment 结束一定要commit()
2. 一个FragmentTransaction只能进行一次commit 所以不要作为全局变量
2. 仿新浪微博的界面
先看看activity总的布局文件:
<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"
>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/> // 这里的weight说明除了底部菜单, fragment占据剩下全部面积
<android.support.v4.app.FragmentTabHost
android:id="@+id/android:tabhost" //注意 这里的ID是android默认
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/maintab_toolbar_bg"
>
<FrameLayout
android:id="@+id/android:tabcontent" //注意 这里的ID是android默认
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
再看看 每个fragment怎么写的:
package com.example.demo4;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class frag1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.frag1, null);// 直接由layout生成 不需要container
}
}
最后就是Activity的实现:
package com.example.demo4;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
private Class[] frags = {frag1.class,frag2.class,frag3.class};
private int[] imgs = {R.drawable.tab1,R.drawable.tab2,R.drawable.tab3};
private String[] titles = {"首页","群组","设置"};
private FragmentTabHost fragmentTabHost;
private LayoutInflater inflater;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = LayoutInflater.from(MainActivity.this);
fragmentTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.content);//fragmentTabHost的初始化
int count = frags.length;
for(int i=0;i<count;i++){
TabSpec spec = fragmentTabHost.newTabSpec(titles[i]).setIndicator(getViewItem(i));//生成标签,newTabSpec()是给背个Tab一个TAG setIndicator()是给每个tab一个View
fragmentTabHost.addTab(spec, frags[i], null);// 绑定到相应的fragment
fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.maintab_toolbar_bg);
}
}
private View getViewItem(int index){
View view = inflater.inflate(R.layout.tabitem, null);
ImageView image = (ImageView)view.findViewById(R.id.tab_img);
image.setImageResource(imgs[index]);
TextView textview = (TextView)view.findViewById(R.id.tab_title);
textview.setText(titles[index]);
return view;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}