安卓创建一个fragment的样例

安卓创建一个fragment的样例


这是一只菜鸡的博客!看了网上的关于fragment的视频讲解,觉得fragmen也就那么回事 ,但是第一次真正在Android Studio上写fragment时,简直是中了毒。这中间的过程就不说了 。。。。

初写(一个极其简单样例。。。在这里插入图片描述

1.fragment动态切换
2.fragment静态加载
3.activity与fragment事件共享

一。样例截图
在这里插入图片描述
二。结构
在这里插入图片描述
三。布局文件
1.下面是activity_main.xml布局。


<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="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:baselineAligned="false">

    <fragment
        android:id="@+id/choice_fragment"
        android:name="com.example.zwh.fragmenttest.choice_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/detail_event"
        android:layout_width="match_parent"
        android:layout_height="49dp"
        android:layout_weight="9"
        android:orientation="vertical">

    </FrameLayout>
</LinearLayout>

在这里插入图片描述

2.接下来是fragment_choice_fragment.xml

<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="wrap_content"
    android:orientation="horizontal"
    tools:context=".choice_fragment">

    <TextView
        android:id="@+id/hot_spot"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="热点" />

    <TextView
        android:id="@+id/news"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="新闻" />

    <TextView
        android:id="@+id/accent"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="事实" />

    <TextView
        android:id="@+id/set"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="设置" />
    <!-- TODO: Update blank fragment layout -->
</LinearLayout>

上面的就是下面这四个TextView
在这里插入图片描述

3.fragment_choice_1.xml布局文件

<?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=".Choice_1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第一个fragment"
        android:textSize="20dp"/>
</LinearLayout>

就只有一个TextView而已。
在这里插入图片描述
四。Java类
1.choice_fragment.java

public class choice_fragment extends Fragment {
    OnClick clickListener;
    public TextView textView1;
    public TextView textView2;
    public TextView textView3;
    public TextView textView4;

    public interface OnClick {
        public void onClick(int id);
    }

    public choice_fragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_choice_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        textView1 = getActivity().findViewById(R.id.hot_spot);
        textView2 = getActivity().findViewById(R.id.news);
        textView3 = getActivity().findViewById(R.id.accent);
        textView4 = getActivity().findViewById(R.id.set);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.onClick(v.getId());
            }
        });
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.onClick(v.getId());
            }
        });
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.onClick(v.getId());
            }
        });
        textView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.onClick(v.getId());
            }
        });
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            clickListener = (OnClick) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(" must implement OnArticleSelectedListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

2.MainActivity.java

package com.example.zwh.fragmenttest;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements choice_fragment.OnClick {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager;
        FragmentTransaction fragmentTransaction;
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction=fragmentManager.beginTransaction();
        Choice_1 choice_1=new Choice_1();
        fragmentTransaction.add(R.id.detail_event,choice_1).commit();
    }

    @Override
    public void onClick(int id) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        switch (id) {
            case R.id.hot_spot:
                Choice_1 choice_1 = new Choice_1();
                fragmentTransaction.replace(R.id.detail_event, choice_1);
                Toast.makeText(this,"hotspot",Toast.LENGTH_SHORT).show();
                break;
            case R.id.news:
                Choice_2 choice_2 = new Choice_2();
                fragmentTransaction.replace(R.id.detail_event, choice_2);
                Toast.makeText(this,"news",Toast.LENGTH_SHORT).show();
                break;
            case R.id.accent:
                Choice_3 choice_3 = new Choice_3();
                fragmentTransaction.replace(R.id.detail_event, choice_3);
                Toast.makeText(this,"accent",Toast.LENGTH_SHORT).show();
                break;
            case R.id.set:
                Choice_4 choice_4 = new Choice_4();
                fragmentTransaction.replace(R.id.detail_event, choice_4);
                Toast.makeText(this,"set",Toast.LENGTH_SHORT).show();
                break;
        }
        fragmentTransaction.commit();
    }
}

3.Choice.java
主要差别就是引入布局不同而已,这个是R.layout.fragment_choice_1

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_choice_1, container, false);
}

五。[点这里看]原作者的文章是如何实现activity与fragment事件共享的。(http://www.cnblogs.com/mengdd/archive/2013/01/11/2856374.html)

六。小菜鸡的博客。

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
1. 创建项目:新建一个Android项目,选择Empty Activity模板。 2. 添加Fragment:右键点击app目录,New -> Fragment -> Fragment(Blank)。创建两个Fragment,分别为Fragment1和Fragment2。 3. 编布局文件:在res/layout文件夹下创建activity_main.xml文件,添加一个FrameLayout作为容器,用于切换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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_fragment1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment1" android:layout_weight="1"/> <Button android:id="@+id/btn_fragment2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment2" android:layout_weight="1"/> </LinearLayout> </LinearLayout> ``` 4. 编Fragment:分别在Fragment1和Fragment2的布局文件中添加一个TextView,用于区分两个Fragment。 ``` <!--fragment1.xml--> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Fragment1" android:gravity="center"/> <!--fragment2.xml--> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Fragment2" android:gravity="center"/> ``` 5. MainActivity中添加Fragment:在MainActivity中使用FragmentManager添加Fragment1。 ``` public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Fragment1 fragment1; private Fragment2 fragment2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragment1 = new Fragment1(); fragment2 = new Fragment2(); getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragment1).commit(); findViewById(R.id.btn_fragment1).setOnClickListener(this); findViewById(R.id.btn_fragment2).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_fragment1: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment1).commit(); break; case R.id.btn_fragment2: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment2).commit(); break; } } } ``` 6. 运行程序:点击按钮可以切换Fragment1和Fragment2。 注:以上代码仅供参考,可能存在语法错误或者逻辑问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值