FragmeLayout的使用
xml布局
Activity内展示fragment
public
class
MainActivity
extends
FragmentActivity {
private
FrameLayout zhan;
private
RadioGroup gr;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
initView();
//设置初始fragment页面
getSupportFragmentManager().beginTransaction().
replace(R.id.zhan,
new
Fragment1()).commit();
gr.setOnCheckedChangeListener(
new
RadioGroup.OnCheckedChangeListener() {
@Override
public
void
onCheckedChanged(RadioGroup radioGroup,
int
i) {
switch
(i){
case
R.id.shouye:
getSupportFragmentManager().beginTransaction().
replace(R.id.zhan,
new
Fragment1()).commit();
break
;
case
R.id.xigua:
getSupportFragmentManager().beginTransaction().
replace(R.id.zhan,
new
Fragment2()).commit();
break
;
case
R.id.wei:
getSupportFragmentManager().beginTransaction().
replace(R.id.zhan,
new
Fragment3()).commit();
break
;
case
R.id.wode:
getSupportFragmentManager().beginTransaction().
replace(R.id.zhan,
new
Fragment4()).commit();
break
;
}
}
});
}
private
void
initView() {
zhan
= findViewById(R.id.zhan);
gr
= findViewById(R.id.gr);
}
}
fragment的显示与隐藏
public
class
MainActivity
extends
FragmentActivity {
private
Fragment2 fragment2;
private
Fragment1 fragment1;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment2
=
new
Fragment2();
fragment1
=
new
Fragment1();
//
如果想要做显示和隐藏的操作,,,必须在Fragment的栈里面添加Fragment
getSupportFragmentManager().beginTransaction()
.add(R.id.framelayout,
fragment1).commit();
getSupportFragmentManager().beginTransaction()
.add(R.id.framelayout,
fragment2).commit();
}
/**
*
显示Fragment1
*/
public
void
show(View view) {
//
创建Fragment管理者,,,并开启事务
FragmentTransaction
transaction = getSupportFragmentManager()
.beginTransaction();
transaction.show(fragment1);
transaction.hide(fragment2);
transaction.commit();
}
/**
*
隐藏Fragment1
*/
public
void
hide(View view) {
//
创建Fragment管理者,,,并开启事务
FragmentTransaction
transaction = getSupportFragmentManager()
.beginTransaction();
transaction.hide(fragment1);
transaction.show(fragment2);
transaction.commit();
}
}
fragment传值
Bundle
b=
new
Bundle();
String
urls = myTabs.get(position).getUrls();
b.putString(
"key"
,urls);
b.putString(
"pageIndex"
,
"1"
);
v_fragment
v_fragment=
new
v_fragment();
v_fragment.setArguments(b);
Bundle
bundle
= getArguments();
if
(bundle !=
null
){
key_url
= bundle.getString(
"key"
);
}
< RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = "com.example.wangwenda20171201_week1.MainActivity" > < FrameLayout android:id = "@+id/zhan" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_alignParentLeft = "true" android:layout_alignParentStart = "true" android:layout_above = "@id/gr" > </ FrameLayout > < RadioGroup android:id = "@+id/gr" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_alignParentBottom = "true" android:orientation = "horizontal" > < RadioButton android:id = "@+id/shouye" android:layout_width = "0dp" android:layout_height = "match_parent" android:layout_weight = "1" android:button = "@null" android:gravity = "center" android:padding = "10dp" android:text = "URLconnection"
/> < RadioButton android:id = "@+id/xigua" android:layout_width = "0dp" android:layout_height = "wrap_content" android:layout_weight = "1" android:button = "@null" android:gravity = "center" android:padding = "10dp" android:text = "HttpClient"
/> < RadioButton android:id = "@+id/wei" android:layout_width = "0dp" android:layout_height = "wrap_content" android:layout_weight = "1" android:button = "@null" android:gravity = "center" android:padding = "10dp" android:text = "WIFI状态"
/> < RadioButton android:id = "@+id/wode" android:layout_width = "0dp" android:layout_height = "wrap_content" android:layout_weight = "1" android:button = "@null" android:gravity = "center" android:padding = "10dp" android:text = "蓝牙状态"
/> </ RadioGroup > </ RelativeLayout >
|
Bundle
b=
new
Bundle();
String
urls = myTabs.get(position).getUrls();
b.putString(
"key"
,urls);
b.putString(
"pageIndex"
,
"1"
);
v_fragment
v_fragment=
new
v_fragment();
v_fragment.setArguments(b);