简介
Fragment,碎片,常用的内容,但是一直没有系统的学习下它的使用方法,花几天抽空看看随便记录一下。
生命周期
来自官网的图片一目了然。
自测试结果:
基本使用
1.自定义一个Fragment:BaseFragment
/*带参数构造方法*/
public static BaseFragment newInstance(String content) {
Bundle args = new Bundle();
args.putString("CONTENT", content);
BaseFragment fragment = new BaseFragment();
fragment.setArguments(args);//传递参数,设置内容文本
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
View view = inflater.inflate(R.layout.content_fragment
, container, false);
Bundle args = getArguments();//获取参数并设置文本内容
if (args != null) {
String content = args.getString("CONTENT");
tv = (TextView) view.findViewById(R.id.tv_content);
tv.setText(content);
}
return view;
}
2.对应布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/colorAccent"
android:textSize="30sp"
android:textStyle="italic|bold" />
</LinearLayout>
3.主Activity布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="jzfp.gs.com.animationdemo.MainActivity">
<!-- 工具栏-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"></android.support.v7.widget.Toolbar>
<!-- 内容区域-->
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
4.Fragment切换
public class MainActivity extends AppCompatActivity {
BaseFragment leftFragment = null;//左侧fragment
BaseFragment rightFragment = null;//右侧Fragment
BaseFragment defaultFragment= null;//默认Fragment
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.left);
setSupportActionBar(toolbar);//设置ActionBar
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showLeftFragment();
}
});
defaultFragment = BaseFragment.newInstance("This is default fragment");
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fl_content, defaultFragment);
transaction.commit();//显示默认碎片
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.right:
showRightFragment();break;
default:break;
}
return true;
}
public void showLeftFragment(){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if(leftFragment == null) {
leftFragment = BaseFragment.newInstance("This is Left Fragment");
}
transaction.replace(R.id.fl_content, leftFragment);
transaction.commit();
}
public void showRightFragment(){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if(rightFragment == null) {
rightFragment = BaseFragment.newInstance("This is Right Fragment");
}
transaction.replace(R.id.fl_content, rightFragment);
transaction.commit();
}
}
5.结果
点击左右按钮切换fragment