[Android]Fragment的应用/点击替换fragment应用实例

来源:互联网 发布:mac 播放器 自动字幕 编辑:程序博客网 时间:2024/05/16 05:56

》静态添加Fragment

把片段当作一个view控件来自动添加,根据需求创建fragment,一个片段对应一个fragment,然后添加在主布局中


<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical"    tools:context="com.girls.tiffany270.sunshine.MainActivity">    <fragment        android:id="@+id/fg_title"        android:name="com.girls.tiffany270.sunshine.TitleFragment"        android:layout_width="match_parent"        android:layout_height="50dp">            </fragment>    <fragment        android:id="@+id/fg_content"        android:name="com.girls.tiffany270.sunshine.ContentFragment"        android:layout_width="match_parent"        android:layout_height="300dp">    </fragment></LinearLayout>

这里是作为fragment的填充布局,可以自定义随便添加,这里就不写了。


package com.girls.tiffany270.sunshine;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.Toast;/** * Created by Tiffany270 on 2017/11/9. */public class TitleFragment extends Fragment {    //第一次创建用户界面时系统回调的方法,返回值是一个view对象    //LayoutInflater将xml文件转为view    //ViewGroup 插入Activity的布局视图    //Bundle 存储上一个fragment的信息    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_title,null);        //如果你要设置点击事件--这里是点击标题的区域        LinearLayout layout = (LinearLayout) view.findViewById(R.id.f_title);        layout.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(getActivity(),"标题",Toast.LENGTH_SHORT).show();}        });        return view;    }}

package com.girls.tiffany270.sunshine;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by Tiffany270 on 2017/11/9. */public class ContentFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_content,null);    }}
运行效果:


》动态创建

一般不建议静态添加片段,而是通过Fragment的管理器对象和事务对象的添加和开启对Fragment添加和删除操作

要注意虽然不再是将片段作为控件直接添加到主布局里,但是还是需要主布局存在一个容器去填充片段,不能凭空添加(这里有个很奇怪的地方,就是当作填充的布局如果用线性布局会填充不满……所有建议填充用的布局用相对布局)


package com.girls.tiffany270.sunshine;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.support.annotation.Nullable;public class MainActivity extends Activity {    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //创建碎片的管理器对象        FragmentManager manager = getFragmentManager();        //创建碎片的事务对象并开启事务        FragmentTransaction transaction = manager.beginTransaction();        //动态添加         位置----------对象        transaction.add(R.id.dy_title, new TitleFragment());        transaction.add(R.id.dy_content, new ContentFragment());        //如果你想移除,则可以transaction.remove()        //动态切换就是替换transaction.replace(片段资源id,被替换资源)        //提交事务        transaction.commit();    }}
<?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"    android:orientation="vertical"    tools:context="com.girls.tiffany270.sunshine.MainActivity">    <LinearLayout        android:id="@+id/dy_title"        android:layout_width="match_parent"        android:layout_height="50dp"        android:orientation="horizontal">    </LinearLayout>    <LinearLayout        android:id="@+id/dy_content"        android:layout_width="match_parent"        android:layout_height="500dp"        android:orientation="horizontal">    </LinearLayout></LinearLayout>
注意:当你移除或替换 Fragment 并向返回堆栈添加事务时,已移除的 Fragment 会停止(而不是销毁)。如果用户向后导航,还原该 Fragment,它会重新启动。如果你没有向返回堆栈添加事务,那么该 Fragment 在移除或替换时就会被销毁。

所以如果想要回退功能,要执行替换的时候,最好这样写

transaction.addToBackStack(null);             //提交事务        transaction.commit();


=======小例子==============================

点击不同按钮会切换到不同页面

项目结构



代码:


<?xml version="1.0" encoding="utf-8"?><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="com.girls.tiffany270.fragment_.MainActivity">    <LinearLayout        android:id="@+id/content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">    </LinearLayout>    <LinearLayout        android:id="@+id/layout_button"        android:layout_width="match_parent"        android:layout_height="100dp"        android:layout_alignParentBottom="true"        android:orientation="horizontal">        <RadioGroup            android:id="@+id/buttons"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:gravity="center"            android:orientation="horizontal">            <RadioButton                android:id="@+id/btn1"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:button="@null"                android:drawablePadding="10dp"                android:drawableTop="@mipmap/ic_launcher"                android:gravity="center"                android:text="Home"/>            <RadioButton                android:id="@+id/btn2"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:button="@null"                android:drawablePadding="10dp"                android:drawableTop="@mipmap/ic_launcher"                android:gravity="center"                android:text="shopping"/>            <RadioButton                android:id="@+id/btn3"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:button="@null"                android:drawablePadding="10dp"                android:drawableTop="@mipmap/ic_launcher"                android:gravity="center"                android:text="share"/>            <RadioButton                android:id="@+id/btn4"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:button="@null"                android:drawablePadding="10dp"                android:drawableTop="@mipmap/ic_launcher"                android:gravity="center"                android:text="play"/>        </RadioGroup>    </LinearLayout></RelativeLayout>
每个布局都是一样的,自行修改即可

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="match_parent"                android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="center"        android:text="我是主页"/></RelativeLayout>
package fragments;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.girls.tiffany270.fragment_.R;/** * Created by Tiffany270 on 2017/11/9. */public class MyFragment_home extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_home,null);    }}

package com.girls.tiffany270.fragment_;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.RadioButton;import fragments.MyFragment_home;import fragments.MyFragment_play;import fragments.MyFragment_share;import fragments.MyFragment_shop;public class MainActivity extends Activity implements View.OnClickListener {    private android.app.FragmentManager manager;    private android.app.FragmentTransaction transaction;    private RadioButton btn1, btn2, btn3, btn4;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();        manager = getFragmentManager();        transaction = manager.beginTransaction();        transaction.add(R.id.content, new MyFragment_home());        transaction.commit();    }    private void init() {        btn1 = (RadioButton) findViewById(R.id.btn1);        btn2 = (RadioButton) findViewById(R.id.btn2);        btn3 = (RadioButton) findViewById(R.id.btn3);        btn4 = (RadioButton) findViewById(R.id.btn4);        btn1.setOnClickListener(this);        btn2.setOnClickListener(this);        btn3.setOnClickListener(this);        btn4.setOnClickListener(this);    }    @Override    public void onClick(View v) {        transaction = manager.beginTransaction();        switch (v.getId()) {            case R.id.btn1:                transaction.replace(R.id.content, new MyFragment_home());                break;            case R.id.btn2:                transaction.replace(R.id.content, new MyFragment_shop());                break;            case R.id.btn3:                transaction.replace(R.id.content, new MyFragment_share());                break;            case R.id.btn4:                transaction.replace(R.id.content, new MyFragment_play());                break;        }       // transaction.addToBackStack(null);        // 注意这个方法如果用了fragment是会叠加具有回退功能的        transaction.commit();    }}








原创粉丝点击