《你的第一行Android代码》菜鸡的自学日记-第十二天

APP 当然是可以下载到平板电脑上面的!
Fragment —— 碎片!

1.简单的使用碎片(碎片类似于活动)

新建一个左侧碎片布局 left_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button"
        />

</LinearLayout>

新建右侧碎片布局 right_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#44a59a">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_gravity="center_horizontal"
        android:text="this is right fragment"
        />

</LinearLayout>

新建一个 LeftFragment 类,在头文件选择的 Fragment 应该是:

import android.support.v4.app.Fragment;

它能让碎片在所有的 Android 系统再保持一致性。

LeftFragment 的代码:

public class LeftFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.left_fragment,container,false);//加载left_fragment布局
        return  view;
    }
}

同理新建 RightFragment 类:

public class RightFragment extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.right_fragment,container,false);
        return view;
    }
}

修改 activity_main.xml 中的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

   <fragment
       android:id="@+id/left_fragment"
       android:name="com.example.a28222.fragmentteat.LeftFragment"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="1"/>

    <fragment
        android:id="@+id/right_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:name="com.example.a28222.fragmentteat.RightFragment"
        android:layout_weight="1"
        />

</LinearLayout>

如图:
手机运行效果

2.动态添加碎片

新建 another_right_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#c630ce"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="this is another right fragment"
        />

</LinearLayout>

新建 AnotherRightFrgment 作为另外一个右侧碎片:

public class AnotherRightFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.another_right_fragment,container,false);//加载left_fragment布局
        return  view;
    }
}

修改 activity_main.xml 中的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

   <fragment
       android:id="@+id/left_fragment"
       android:name="com.example.a28222.fragmentteat.LeftFragment"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="1"/>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/right_layout">
    </FrameLayout>

</LinearLayout>

修改 MainActivity 中的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(this);
        replaceFragment(new RightFragment());
    }

    public  void onClick (View v){
        switch (v.getId()){
            case R.id.button:
                replaceFragment(new AnotherRightFragment());
                break;
            default:
                break;
        }

    }

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout,fragment);
        transaction.addToBackStack(null);		//在碎片中模拟返回栈
        transaction.commit();
    }
}

由此可见动态添加碎片分五步:

  1. 创建待添加的碎片实例
  2. 获取 FragmentManager ,在活动中直接通过调用 getSupportFragmentManager() 方法得到
  3. 开启一个事务,通过调用 beginSupportFragment() 方法开启
  4. 向容器内添加或替换碎片,使用 replace() 方法实现,需要传入容器的ID待添加的碎片实例
  5. 提交事务,调用 commit() 方法来完成

运行代码:

手机运行效果

点击按钮

手机运行效果

小结:认识了碎片 Fragment ,碎片真的和活动太相像了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值