将fragment与layout结合使用,一般都是主activity以frame填充activity的方式交互管理fragment :
1.由于用到getsupportfragmentmanager()之类,所以主activity的extends需为fragmentactivity:
1
2
3
|
public class mainactivity extends fragmentactivity{
..........
}
|
2.主activity的layout(xml文件)中建立多个frame并定义其android:id="@+id/xxx"
3.主activity内操作函数中,由查找id来对这些framelayout进行activity填充:
添加语句如下
getsupportfragmentmanager().begintransaction().add(r.id.fragment_container2,new nullfrag()).commit();
取得支持fragment管理()->开始交易()->添加(frame的id , fragment).交付();
替换语句如下
getsupportfragmentmanager().begintransaction().replace(r.id.fragment_container2,new nullfrag()).commit();
取得支持fragment管理()->开始交易()->替代(frame的id , fragment).交付();
例:
public void click_btn_flag02(view view){
getsupportfragmentmanager().begintransaction().replace(r.id.fragment_container2,new midfrag()).commit();
}
注:函数参数要用view
下面我们来看实例:
效果图的左边是一个列表,右边是列表item的详情。
先看一下布局文件(layout):
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "horizontal" android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<fragment
class = "com.fragment.main.titlesfragment"
android:id= "@+id/titles" android:layout_weight= "1"
android:layout_width= "0px" android:layout_height= "match_parent" />
<framelayout android:id= "@+id/details" android:layout_weight= "1"
android:layout_width= "0px" android:layout_height= "match_parent"
android:background= "?android:attr/detailselementbackground" />
</linearlayout>
|
布局文件中使用了fragment标签和framelayout标签。android fragment使用 中介绍了2中嵌入fragment的方法,这个实例中都用到,从布局文件看到有了fragment标签,这是一种使用方法,framelayout标签将会成为第二种加载fragment的载体view。
看一下程序实现(com.fragment.main.titlesfragment):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
public class titlesfragment extends listfragment {
int mcurcheckposition = 0 ;
int mshowncheckposition = - 1 ;
@override
public void onactivitycreated(bundle savedinstancestate) {
super .onactivitycreated(savedinstancestate);
setlistadapter( new arrayadapter<string>(getactivity(),
android.r.layout.simple_list_item_activated_1,
shakespeare.titles)); //使用静态数组填充列表
if (savedinstancestate != null ) {
mcurcheckposition = savedinstancestate.getint( "curchoice" , 0 );
mshowncheckposition = savedinstancestate.getint( "shownchoice" , - 1 );
}
getlistview().setchoicemode(listview.choice_mode_single);
showdetails(mcurcheckposition);
}
@override
public void onsaveinstancestate(bundle outstate) {
super .onsaveinstancestate(outstate);
outstate.putint( "curchoice" , mcurcheckposition);
outstate.putint( "shownchoice" , mshowncheckposition);
}
@override
public void onlistitemclick(listview l, view v, int position, long id) {
showdetails(position);
}
/**
*显示listview item 详情
*/
void showdetails( int index) {
mcurcheckposition = index;
getlistview().setitemchecked(index, true );
if (mshowncheckposition != mcurcheckposition) {
detailsfragment df = detailsfragment.newinstance(index);
fragmenttransaction ft = getfragmentmanager()
.begintransaction();
ft.replace(r.id.details, df);
ft.settransition(fragmenttransaction.transit_fragment_fade);
ft.commit();
mshowncheckposition = index;
}
}
}
|
titlesfragment
titlesfragment继承自fragment的子类listfragment,使用了一个静态数组填充列表,重写了onlistitemclick方法,showdetails方法展示listview item的详情。
1
2
3
4
5
|
detailsfragment df = detailsfragment.newinstance(index); //获取详情fragment的实例
fragmenttransaction ft = getfragmentmanager().begintransaction(); //获取fragmenttransaction 实例
ft.replace(r.id.details, df); //使用detailsfragment 的实例
ft.settransition(fragmenttransaction.transit_fragment_fade);
ft.commit(); //提交
|
看一下detailsfragment :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public class detailsfragment extends fragment {
/** * create a new instance of detailsfragment, initialized to * show the text at 'index'. */
public static detailsfragment newinstance( int index) {
detailsfragment f = new detailsfragment();
// supply index input as an argument.
bundle args = new bundle();
args.putint( "index" , index);
f.setarguments(args);
return f;
}
@override
public view oncreateview(layoutinflater inflater, viewgroup container,
bundle savedinstancestate) {
if (container == null ) {
return null ;
}
scrollview scroller = new scrollview(getactivity());
textview text = new textview(getactivity());
int padding = ( int ) typedvalue.applydimension(
typedvalue.complex_unit_dip, 4 , getactivity().getresources()
.getdisplaymetrics());
text.setpadding(padding, padding, padding, padding);
scroller.addview(text);
text.settext(shakespeare.dialogue[getarguments().getint( "index" , 0 )]);
return scroller;
}
}
|
detailsfragment 中使用newinstance(int index)方法产生detailsfragment 实例并接受整型参数,重载了oncreateview方法创建view。