Demo下载地址:http://download.csdn.net/detail/baopengjian/9336883
Fragment在xml中使用,Fragment之间通信
(1)FirstMethodActivity
@H_403_20@ @H_403_20@publicclassFirstMethodActivityextendsFragmentActivity{
@Override
protectedvoidonCreate(Bundlearg0){
super.onCreate(arg0);
setContentView(R.layout.activity_first);
}
}
activity_first.xml:
xmlns:tools="
http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bpj.fragmentdemo.MainActivity">
<fragment
android:id="@+id/fg1"
android:name="com.bpj.fragmentdemo.fragment.FirstMethodFragment1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/first_method"/>
<fragment
android:layout_below="@id/fg1"
android:id="@+id/fg2"
android:name="com.bpj.fragmentdemo.fragment.FirstMethodFragment2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/first_method"/>
</RelativeLayout>
(2)Fragment2:
publicclassFirstMethodFragment2extendsFragment{
privateContextcontext;
privateTextViewtv_fragment1,tv_fragment2,tv_fragment3;
privateStringversionName,VersionCode;
privateintscreenWidth;
privateintscreenHeight;
privateFilesdCardDir;
privatelongtotalSpace;
privateStringtotalSize,usableSize;
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
Viewview=inflater.inflate(R.layout.fragment_first2,null);
tv_fragment1=(TextView)view.findViewById(R.id.tv_fragment1);
tv_fragment2=(TextView)view.findViewById(R.id.tv_fragment2);
tv_fragment3=(TextView)view.findViewById(R.id.tv_fragment3);
returnview;
}
@Override
publicvoidonActivityCreated(BundlesavedInstanceState){
super.onActivityCreated(savedInstanceState);
context=getActivity();
try{
PackageInfoinfo=context.getPackageManager().getPackageInfo(context.getPackageName(),0);
versionName=info.versionName;
VersionCode=info.versionCode+"";
}catch(NameNotFoundExceptione){
e.printStackTrace();
}
DisplayMetricsmetrics=newDisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth=metrics.widthPixels;
screenHeight=metrics.heightPixels;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
sdCardDir=Environment.getExternalStorageDirectory();
totalSpace=sdCardDir.getTotalSpace();
totalSize=Formatter.formatFileSize(context,totalSpace);
usableSize=Formatter.formatFileSize(context,sdCardDir.getUsableSpace());
}
}
publicvoidsetText(){
tv_fragment1.setText("versionName:"+versionName+";versionCode:"+VersionCode);
tv_fragment2.setText("screenWidth:"+screenWidth+";screenHeight:"+screenHeight);
tv_fragment3.setText("sdDir:"+sdCardDir.getPath()+"\n"+"totalSize="+totalSize+";usableSize="+usableSize);
}
publicvoidclear(){
tv_fragment1.setText(null);
tv_fragment2.setText(null);
tv_fragment3.setText(null);
}
}
fragment_first2.xml:
xmlns:tools="
http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="600dp"
android:background="@android:color/holo_blue_light"
tools:context="com.bpj.fragmentdemo.MainActivity">
<TextView
android:id="@+id/tv_fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="14dp"
android:text="Fragment2_1"/>
<TextView
android:id="@+id/tv_fragment3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_fragment2"
android:layout_below="@+id/tv_fragment2"
android:layout_marginTop="105dp"
android:text="Fragment2_3"/>
<TextView
android:id="@+id/tv_fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_fragment1"
android:layout_below="@+id/tv_fragment1"
android:layout_marginTop="100dp"
android:text="Fragment2_2"/>
</RelativeLayout>
(3)Fragment1:
publicclassFirstMethodFragment1extendsFragmentimplementsOnClickListener{
privateTextViewtv1,tv2;
privateFirstMethodFragment2mFragment2;
@Override
publicViewonCreateView(LayoutInflaterinflater,
BundlesavedInstanceState){
Viewview=inflater.inflate(R.layout.fragment_first1,null);
tv1=(TextView)view.findViewById(R.id.tv1);
tv1.setOnClickListener(this);
tv2=(TextView)view.findViewById(R.id.tv2);
tv2.setOnClickListener(this);
returnview;
}
@Override
publicvoidonActivityCreated(BundlesavedInstanceState){
super.onActivityCreated(savedInstanceState);
mFragment2=(FirstMethodFragment2)getActivity().getSupportFragmentManager().findFragmentById(R.id.fg2);
}
@Override
publicvoidonClick(Viewview){
switch(view.getId()){
caseR.id.tv1:
mFragment2.setText();
break;
caseR.id.tv2:
mFragment2.clear();
break;
}
}
}
xmlns:tools="
http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bpj.fragmentdemo.MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="42dp"
android:text="Fragment1"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv1"
android:layout_marginLeft="23dp"
android:layout_toRightOf="@+id/tv1"
android:text="Fragment2"/>
</RelativeLayout>
原文链接:https://www.f2er.com/xml/295785.html