Fragment技能点汇总总结:
目录:
过程和代码如下:
1.获取管理者
FragmentManager fragmentManager = getFragmentManager();
2.找到fragment
Fragment fragment = fragmentManager.findFragmentByTag(tag);
Fragment fragment = fragmentManager.findFragmentById(id);
3.一般直接管理对象.获取事物(替换布局,frgmentXX).提交.
案例一:通过fragment构造传给Activity
public class FramentTestActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new TestFragment("param")).commit();
}
}
public static class TestFragment extends Fragment {
private String mArg = "non-param";
public TestFragment() {
Log.i("INFO", "TestFragment non-parameter constructor");
}
public TestFragment(String arg){
mArg = arg;
Log.i("INFO", "TestFragment construct with parameter");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView tv = (TextView) rootView.findViewById(R.id.tv);
tv.setText(mArg);
return rootView;
}
}
}
结果: 能正常显示
当横屏后:得到的值为null
案例二:换成setArguments(bundle)传递值
public class FramentTest2Activity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id. container, TestFragment.newInstance("param")).commit();
}
}
public static class TestFragment extends Fragment {
private static final String ARG = "arg";
public TestFragment() {
Log. i("INFO", "TestFragment non-parameter constructor" );
}
public static Fragment newInstance(String arg){
TestFragment fragment = new TestFragment();
Bundle bundle = new Bundle();
bundle.putString( ARG, arg);
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout. fragment_main, container,
false);
TextView tv = (TextView) rootView.findViewById(R.id. tv);
tv.setText(getArguments().getString( ARG));
return rootView;
}
}
}
不论横屏还是竖屏都会得到值
布局:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" >
<LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:background="#22ff0000" android:orientation="vertical">
<Button android:id="@+id/bt_news" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="新闻"/>
<Button android:id="@+id/bt_fun" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="娱乐"/>
<TextView android:id="@+id/tv_sport" android:layout_width="match_parent" android:layout_height="40dp" android:background="#22000000" android:gravity="center" android:text="体育"/>
LinearLayout>
<LinearLayout android:orientation="vertical" android:layout_width="0dp" android:layout_weight="5" android:layout_height="match_parent">
<EditText android:id="@+id/et_info" android:layout_width="match_parent" android:hint="请输入内容" android:layout_height="wrap_content"/>
<FrameLayout android:id="@+id/fl_content" android:background="#2200ff00" android:layout_width="match_parent" android:layout_height="match_parent">
FrameLayout>
LinearLayout>
LinearLayout>
fragment_fun.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent">
<TextView android:background="#33ff00f7" android:gravity="center" android:text="我是娱乐" android:layout_width="match_parent" android:layout_height="match_parent"/>
LinearLayout>
fragment_news.xml
android:background="#330000ff"
android:text="我是新闻"
fragment_sport.xml
Fragment_fun.java
public class Fragment_fun extends Fragment {
//1.固定
@Override
public void onAttach(Context context) {
Log.d("abce", "onAttach");
super.onAttach(context);
}
//2.创建
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("abce", "onCreate");
super.onCreate(savedInstanceState);
}
//3.创建View
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("abce", "onCreateView");
//填充布局进来
return inflater.inflate(R.layout.fragment_fun,null);
}
//4.创建活动界面
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d("abce", "onActivityCreated");
super.onActivityCreated(savedInstanceState);
}
@Override
public void onDestroyView() {
Log.d("abce", "onDestroyView");
super.onDestroyView();
}
@Override
public void onDestroy() {
Log.d("abce", "onDestroy");
super.onDestroy();
}
@Override
public void onDetach() {
Log.d("abce", "onDetach");
super.onDetach();
}
}
Fragment_news.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("abce", "onCreateView");
//填充布局
return inflater.inflate(R.layout.fragment_news,null);
}
Fragment_sport.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("abce", "onCreateView");
View view =inflater.inflate(R.layout.fragment_sport,null);
mBt_getInfo = (Button) view.findViewById(R.id.bt_getInfo);
mBt_getInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//通过getActivity()方法获取到绑定的Activity对象
EditText et = (EditText) getActivity().findViewById(R.id.et_info);
String str = et.getText().toString().trim();
if (TextUtils.isEmpty(str)) {
Toast.makeText(getActivity(), "null", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(getActivity(),"获取到的信息是"+str,Toast.LENGTH_SHORT).show();
}
});
Button goA = (Button) view.findViewById(R.id.bt_goFun);
goA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction().replace(R.id.fl_content, new Fragment_fun()).commit();
}
});
return view;
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mBt_news;
private Button mBt_fun;
private TextView mTv_sport;
private FragmentTransaction mFt;
private FragmentManager mFm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initView();
}
private void init() {
// 获取管理对象
mFm = getFragmentManager();
// 获取事物
mFt = mFm.beginTransaction();
// 开始的时候马上替换,帧布局先放new模块;
mFt.replace(R.id.fl_content, new Fragment_news());
// 提交
mFt.commit();
}
private void initView() {
mBt_news = (Button) findViewById(R.id.bt_news);
mBt_fun = (Button) findViewById(R.id.bt_fun);
mTv_sport = (TextView) findViewById(R.id.tv_sport);
mBt_news.setOnClickListener(this);
mBt_fun.setOnClickListener(this);
mTv_sport.setOnClickListener(this);
}
/** * 各个板块的点击事件: 每点击一个,创建一个: 1.管理 2.事务 3.事务替换内容 4.提交 */
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_news:
getFragmentManager().beginTransaction().replace(R.id.fl_content, new Fragment_news()).commit();
break;
case R.id.bt_fun:
getFragmentManager().beginTransaction().replace(R.id.fl_content, new Fragment_fun()).commit();
break;
case R.id.tv_sport:
getFragmentManager().beginTransaction().replace(R.id.fl_content, new Fragment_sport()).commit();
break;
}
}
}