14.1、Fragment
Fragment
表示应用界面中可重复使用的一部分。Fragment 定义和管理自己的布局,具有自己的生命周期,并且可以处理自己的输入事件。Fragment 不能独立存在,而是必须由 Activity 或另一个 Fragment 托管。Fragment 的视图层次结构会成为宿主的视图层次结构的一部分,或附加到宿主的视图层次结构。
本代码使用的是app包下的Fragment
创建Fragment类
@SuppressLint("ValidFragment")
public class Myfragment extends Fragment {
private String content;
public Myfragment(String content) {
this.content = content;
}
}
重写onCreateView()
函数,通过调用该函数来让片段实例化它的用户界面视图。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fg_content,container,false);
TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
txt_content.setText(content);
return view;
}
源码构造方法:
public View onCreateView (LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
Parameters | |
---|---|
inflater | LayoutInflater : 可用于扩展片段中的任何视图 |
container | ViewGroup : 如果非空,Fragment 的UI应该附加到的父视图。 |
savedInstanceState | Bundle : 如果非空,则此Fragment 将根据这里给出的先前保存状态重新构造。 |
此处的inflate方法与ListView列表控件中的inflate方法类似。
Layout:
通过一个相对布局来确定Fragment的位置
<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=".Mymain">
<!--设置标题bar-->
<RelativeLayout
android:id="@+id/ly_top_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/bg_topbar">
<TextView
android:id="@+id/txt_topbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="18sp"
android:textColor="@color/text_topbar"
android:text="信息"/>
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="@color/div_white"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<!--设置底部按钮bar-->
<LinearLayout
android:id="@+id/ly_tab_bar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@color/bg_white"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_channel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_channel"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:id="@+id/txt_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_message"
android:gravity="center"
android:padding="5dp" />
<TextView
android:id="@+id/txt_better"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_better"
android:gravity="center"
android:padding="5dp" />
<TextView
android:id="@+id/txt_setting"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_setting"
android:gravity="center"
android:padding="5dp" />
</LinearLayout>
<View
android:id="@+id/div_tab_bar"
android:layout_width="match_parent"
android:layout_height="2px"
android:background="@color/div_white"
android:layout_above="@id/ly_tab_bar"/>
<!--在xml中配置FragMent显示位置-->
<!--layout_below与layout_above设置在RelativeLayout中的相对布局-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ly_top_bar"
android:layout_above="@id/div_tab_bar"
android:id="@+id/ly_content">
</FrameLayout>
</RelativeLayout>
其中四个TextView用来做按钮事件实现Fragment的切换
background背景色配置文件设置:
通过一个选择器来设置选中样式与未选中样式
Selector默认的选中状态为false,所以我们要在Activity创建时,设置一个默认的选中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<solid android:color="#FFC4C4C4" />
</shape>
</item>
<item>
<shape>
<solid android:color="@color/transparent" />
</shape>
</item>
</selector>
只展示了其中一个其余同理
drawableTop前景色配置文件设置:
前景色利用四张图片来进行覆盖:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/tab_channel_pressed"
android:state_selected="true" />
<item android:drawable="@mipmap/tab_channel_normal" />
</selector>
只调用一个作为展示,其他同理
Activity:
主页面继承Activity类并同时实现点击事件
public class Mymain extends Activity implements View.OnClickListener{
Fragment使用步骤:
/**Fragment 1.获取碎片管理器 调用getFragmentmanager 2.获取当前的业务对象 getfragmentmanager().begintransaction 3.准备fragment对象 4.commit提交业务 */
在创建时获取布局中需要的view
private TextView txt_topbar;
private TextView txt_channel;
private TextView txt_message;
private TextView txt_better;
private TextView txt_setting;
private FrameLayout ly_content;
//Fragment Object
private Myfragment fg1,fg2,fg3,fg4;
private FragmentManager fManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
txt_topbar = (TextView) findViewById(R.id.txt_topbar);
txt_channel = (TextView) findViewById(R.id.txt_channel);
txt_message = (TextView) findViewById(R.id.txt_message);
txt_better = (TextView) findViewById(R.id.txt_better);
txt_setting = (TextView) findViewById(R.id.txt_setting);
ly_content = (FrameLayout) findViewById(R.id.ly_content);
//1.调用getFragmentManager()获取FragmentManager对象
fManager = getFragmentManager();
txt_channel.setOnClickListener(this);
txt_message.setOnClickListener(this);
txt_better.setOnClickListener(this);
txt_setting.setOnClickListener(this);
//设置一个默认选择状态
txt_channel.setSelected(true);
}
FragmentManager
类负责对应用的 Fragment 执行一些操作,如添加、移除或替换它们,以及将它们添加到返回堆栈。
点击事件的定义:
@Override
public void onClick(View v) {
//2.获取当前的业务对象
FragmentTransaction fTransaction = fManager.beginTransaction();
//初始时隐藏所有的业务对象
hideAllFragment(fTransaction);
switch (v.getId()){
case R.id.txt_channel:
//设置所有选中状态为false
setSelected();
//设置当前选中
txt_channel.setSelected(true);
//如果当前Fragment对象不存在则创建
if(fg1 == null){
fg1 = new Myfragment("fg1 Fragment");
//将要显示的content添加到目标view(通过id指定)上去
fTransaction.add(R.id.ly_content,fg1);
}
//else直接显示
else{
fTransaction.show(fg1);
}
break;
case R.id.txt_message:
setSelected();
txt_message.setSelected(true);
if(fg2 == null){
fg2 = new Myfragment("fg2 Fragment");
fTransaction.add(R.id.ly_content,fg2);
}else{
fTransaction.show(fg2);
}
break;
case R.id.txt_better:
setSelected();
txt_better.setSelected(true);
if(fg3 == null){
fg3 = new Myfragment("fg3 Fragment");
fTransaction.add(R.id.ly_content,fg3);
}else{
fTransaction.show(fg3);
}
break;
case R.id.txt_setting:
setSelected();
txt_setting.setSelected(true);
if(fg4 == null){
fg4 = new Myfragment("fg4 Fragment");
fTransaction.add(R.id.ly_content,fg4);
}else{
fTransaction.show(fg4);
}
break;
}
//事务提交
fTransaction.commit();
}
private void setSelected(){
txt_channel.setSelected(false);
txt_message.setSelected(false);
txt_better.setSelected(false);
txt_setting.setSelected(false);
}
private void hideAllFragment(FragmentTransaction fragmentTransaction){
if(fg1 != null)fragmentTransaction.hide(fg1);
if(fg2 != null)fragmentTransaction.hide(fg2);
if(fg3 != null)fragmentTransaction.hide(fg3);
if(fg4 != null)fragmentTransaction.hide(fg4);
}
在运行时,FragmentManager
可以添加,删除,替换,并执行与片段响应用户交互的其他操作。你提交的每一组片段更改都被称为一个事务,你可以使用FragmentTransaction
类提供的api指定在事务内部做什么。您可以将多个操作分组到单个事务中,例如,一个事务可以添加或替换多个片段。当您在同一个屏幕上显示多个同级片段时,这种分组非常有用,例如拆分视图。
14.2、PreferenceFragment
安卓提供的专用设置界面
设置PreferenceFragment
类
public class pf extends PreferenceFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
//输入姓名事件处理
((EditTextPreference)getPreferenceScreen().findPreference("name_info"))
.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);//setsummary函数跟xml中"android:summary="属性的作用一样
return false;
}
});
//选择城市事件处理
getPreferenceScreen().findPreference("lp_city")
.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String)newValue);
((ListPreference)preference).setValue((String)newValue);
return false;
}
});
}
}
Activity获取FragmentManager对象进行模块覆盖
/** add preferencefromresouce(R.xml.settings,null); 1.新建xml resource folder */ /** activity: FragmentTranscaction.add(R.id.xxx,new preference()).commit(); */
preferencescreen
页面配置文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:key="pc_net1"
android:title="网络设置">
<CheckBoxPreference android:key="cb_net"
android:title="网络开关"
android:summary="打开网络移动流量" />
<SwitchPreference android:key="sw_net"
android:title="网络开关"
android:summary="打开网络可wifi" />
</PreferenceCategory>
<PreferenceCategory android:key="pc_loc"
android:title="位置设置">
<EditTextPreference android:key="name_info"
android:title="姓名"
android:summary="点击输入真实姓名"
android:dialogTitle="请输入姓名"/>
<ListPreference android:key="lp_city"
android:title="所在城市"
android:summary="点击从列表中选择您所在的位置"
android:dialogTitle="选择城市"
android:entries="@array/cityarr"
android:entryValues="@array/cityvalue"/>
</PreferenceCategory>
</PreferenceScreen>
字符数组配置
<string-array name="cityarr">
<item>济南</item>
<item>青岛</item>
<item>北京</item>
<item>上海</item>
</string-array>
<string-array name="cityvalue">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>