2017-08-27
原CSDN博客已弃用,文章会逐渐迁移过来。
xml布局文件
fragment_list.xml (控制fragment里面的内容)
<?xml version=”1.0” encoding=”utf-8”?>
activity_static_load_fragment.xml (控制fragment控件所处于Activity页面中的位置)
<?xml version=”1.0” encoding=”utf-8”?>
<fragment
android:id="@+id/listFragment"
android:name="com.administrator.fragmentdemo.ListFragment"
android:layout_width="100dp"
android:layout_height="100dp"/>
<fragment
android:id="@+id/listFragmentTow"
android:name="com.administrator.fragmentdemo.ListFragment"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
Fragment的Java代码管理类
package com.administrator.fragmentdemo;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* 列表fragment
*/
public class ListFragment extends Fragment {
//创建视图
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//new view
View view = inflater.inflate(R.layout.fragment_list,container,false);
TextView textView = view.findViewById(R.id.textView);//需要根据根视图view进行findViewById
textView.setText("hi");
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
装载Fragment的Activity的Java代码
package com.administrator.fragmentdemo;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* 列表fragment
*/
public class ListFragment extends Fragment {
//创建视图
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//new view
View view = inflater.inflate(R.layout.fragment_list,container,false);
TextView textView = view.findViewById(R.id.textView);//需要根据根视图view进行findViewById
textView.setText("hi");
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}