Android汇总
Demo下载地址:http://download.csdn.net/detail/baopengjian/9336883
Fragment在xml中使用,Fragment之间通信
1 FirstMethodActivity
public class FirstMethodActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_first);
}
}
activity_first.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.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:
public class FirstMethodFragment2 extends Fragment{
private Context context;
private TextView tv_fragment1,tv_fragment2,tv_fragment3;
private String versionName,VersionCode;
private int screenWidth;
private int screenHeight;
private File sdCardDir;
private long totalSpace;
private String totalSize,usableSize;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = 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);
return view;
}
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity();
try {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
versionName = info.versionName;
VersionCode = info.versionCode+"";
} catch (NameNotFoundException e) {
e.printStackTrace();
}
DisplayMetrics metrics = new DisplayMetrics();
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());
}
}
public void setText(){
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);
}
public void clear(){
tv_fragment1.setText(null);
tv_fragment2.setText(null);
tv_fragment3.setText(null);
}
}
fragment_first2.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="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:
public class FirstMethodFragment1 extends Fragment implements OnClickListener {
private TextView tv1, tv2;
private FirstMethodFragment2 mFragment2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = 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);
return view;
}
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mFragment2 = (FirstMethodFragment2) getActivity().getSupportFragmentManager().findFragmentById(R.id.fg2);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv1:
mFragment2.setText();
break;
case R.id.tv2:
mFragment2.clear();
break;
}
}
}
<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.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>