Fragment 布局

做了两个界面跳转的Fragment      只实现了简单跳转   代码不多可以借鉴

有个布局文件  两个java文件

代码如下

public class Frame_main extends AppCompatActivity
{

    public Frame_data mFrame_data;
    public Frame_chat mFrame_chat;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    public void initView(){
        ImageView frame_data;
        ImageView frame_chat;
        frame_data = (ImageView) findViewById(R.id.frame_data);
        frame_chat = (ImageView) findViewById(R.id.frame_chat);

        frame_data.setOnClickListener ( new View.OnClickListener ( )
        {
            @Override
            public void onClick(View v)
            {
                FragmentManager fm = getFragmentManager();
                //开启Fragment事务
                FragmentTransaction fragmentTransaction = fm.beginTransaction();
                if (mFrame_data == null){
                    mFrame_data = new Frame_data ();
                }
                //使用当前的Fragment的布局代替id_bottombar_liaotian的控件
                fragmentTransaction.replace(R.id.frame_main,mFrame_data);
                //事务提交
                fragmentTransaction.commit();
            }
        } );
        frame_chat.setOnClickListener ( new View.OnClickListener ( )
        {
            @Override
            public void onClick(View v)
            {
                FragmentManager fm = getFragmentManager();
                //开启Fragment事务
                FragmentTransaction fragmentTransaction = fm.beginTransaction();
                if (mFrame_chat == null){
                    mFrame_chat = new Frame_chat ();
                }
                fragmentTransaction.replace(R.id.frame_main,mFrame_chat);
                //事务提交
                fragmentTransaction.commit();
            }
        } );

        //设置默认的Fragment
        setDefaultFragment();
    }
    private void setDefaultFragment(){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        mFrame_data=new Frame_data ();
        fragmentTransaction.replace(R.id.frame_main,mFrame_data);
        fragmentTransaction.commit();
    }
}
2、第一个界面的java

public class Frame_data extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.frame_data,container,false);
    }
}
3、第二个界面的java

public class Frame_chat extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.frame_chat,container,false);
    }
}
布局文件如下

1、frame_main.xml

<RelativeLayout
    android:id="@+id/content_main"
    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="match_parent"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.mars.aaaa.MainActivity"
    tools:showIn="@layout/app_bar_main">


    <FrameLayout
        android:id="@+id/frame_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"/>

    <include

        android:id="@+id/include_title"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        layout="@layout/frame_title"
        android:layout_marginBottom="10dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"/>


</RelativeLayout>
2、frame_title.xml

<?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"
              xmlns:tools="http://schemas.android.com/tools"
              tools:showIn="@layout/content_main">

    <LinearLayout
        android:background="#f2f0f0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Space
            android:layout_width="70dp"
            android:layout_height="wrap_content"/>
        <ImageView
            android:id="@+id/frame_data"
            android:background="@drawable/touxiang_nv"
            android:layout_width="55dp"
            android:layout_height="55dp"/>
        <Space
            android:layout_width="130dp"
            android:layout_height="wrap_content"/>
        <ImageView
            android:id="@+id/frame_chat"
            android:background="@drawable/touxiang"
            android:layout_width="55dp"
            android:layout_height="55dp"/>


    </LinearLayout>
</LinearLayout>
3、剩下两个文件基本差不多  就列举一个

<?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">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="data"/>

</LinearLayout>


相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页