要创建一个fragment,必须创建一个fragment的子类。
除了基类fragment,还有:
DialogFragment
ListFragment
PreferenceFragment
与Activity的对应生命周期
运行
创建Fragment
方式一
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/title_fragment"
android:name="com.example.fragmentapplication.TitleFragment"/>
方式二
//通过代码添加Fragment
private void addContentLayout() {
FragmentManager fm = getSupportFragmentManager();
//开启一个事务
FragmentTransaction ft = fm.beginTransaction();
contentFragment = new ContentFragment();
//添加fragment
ft.add(R.id.cf,contentFragment);
// ft.remove()//删除
// ft.replace()//替换
ft.commit();
}
//ContentFragment.java
public class ContentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_layout,container,false);
return view;
}
}
管理Fragment
public class PopBackFragment extends Fragment {
private String title;
public PopBackFragment(){}
@SuppressLint("ValidFragment")
public PopBackFragment(String title){
this.title = title;
}
//Fragment的传参方法
// public static PopBackFragment getInstance(String title){
// PopBackFragment p = new PopBackFragment();
//
// Bundle b = new Bundle();
// b.putString("title",title);
// p.setArguments(b);
// return p;
// }
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_pop_back_fragment,container,false);
TextView tv = (TextView)view.findViewById(R.id.textView_text);
tv.setText(title);
return view;
}
}
public void oneClick(View v){
PopBackFragment p1 = new PopBackFragment("one");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content,p1);
//把当前Fragment添加到Activity栈中
ft.addToBackStack(null);
ft.commit();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
if(getSupportFragmentManager().getBackStackEntryCount()==0){
finish();
}else {
getSupportFragmentManager().popBackStack();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
与Activity交互
<!--fragment_menu-->
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuFragment"
android:orientation="vertical">
<Button
android:id="@+id/button_news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="News"/>
<Button
android:id="@+id/button_music"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Musics" />
</LinearLayout>
<!--fragment_main-->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MainFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:id="@+id/value"
android:gravity="center"/>
</FrameLayout>
//MenuFragment
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MenuFragment extends Fragment implements View.OnClickListener {
private MyMenuListener myMenuListener;
public MenuFragment() {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
myMenuListener = (MyMenuListener)context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_menu, container, false);
view.findViewById(R.id.button_news).setOnClickListener(this);
view.findViewById(R.id.button_music).setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_news:
myMenuListener.changeValue("news");
break;
case R.id.button_music:
myMenuListener.changeValue("musics");
break;
}
}
//定义一个回调接口
public static interface MyMenuListener{
public void changeValue(String value);
}
}
//MainFragment
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainFragment extends Fragment {
private TextView tv_value;
public MainFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main, container, false);
tv_value = (TextView)view.findViewById(R.id.value);
return view;
}
public void changeTextViewValue(String value){
tv_value.setText(value);
}
}
//MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity3 extends AppCompatActivity implements MenuFragment.MyMenuListener{
private MenuFragment menuFragment;
private MainFragment mainFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
menuFragment = (MenuFragment) getSupportFragmentManager().findFragmentById(R.id.menuFragment);
mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.mainFragment);
}
@Override
public void changeValue(String value) {
mainFragment.changeTextViewValue(value);
}
}
PrefernceFragment
在res中新建一个名为xml的文件夹,在文件夹中新建一个preference.xml文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="是否打开声音">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="打开声音"
android:summary="设置打开声音后可以自动开启音乐"/>
</PreferenceCategory>
<PreferenceCategory
android:title="城市">
<EditTextPreference
android:key="edittext_preference"
android:title="昵称"
android:summary="请输入昵称"
android:dialogTitle="昵称" />
<ListPreference
android:key="list_preference"
android:title="城市名称"
android:summary="请选择一个城市"
android:entries="@array/city"
android:entryValues="@array/city"
android:dialogTitle="城市名称" />
</PreferenceCategory>
</PreferenceScreen>
然后新建一个fragment,实现addPreferencesFromResource方法
public class PerfsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
最后新建一个activity使用fragment
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/prefs_fragment"
android:name="com.example.fragmentapplication.PerfsFragment"/>