android-碎片布局

基本使用

  • 首先创建对应的碎片
    New => Fragment => Fragment(Blank)

    取消勾选include

    java代码如下,注意继承的Frament需要建议使用support-v4库中的Frament,可以更好的保持一致性,在fragment_test.xml写入布局
    public class TestFragment extends Fragment {
        public TestFragment() {
            // Required empty public constructor
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            // 通过LayoutInflater的inflate()方法加载对应的布局
            return inflater.inflate(R.layout.fragment_test, container, false);
        }
    
    }
    
  • Activity添加碎片
    aicivity布局代码如下
    <?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="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
        <FrameLayout
            android:id="@+id/framLay"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <fragment
                android:id="@+id/fragment"
                android:name="app.mrquan.test2019.FirstFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>
    </LinearLayout>
    
    声明一个FrameLayout布局是为了以后可以动态添加碎片
    注意:使用frament必须要加id!!!,否则会报错

动态加载

  • 创建待添加的碎片实例。
  • 获取到FragmentManager,在活动中可以直接调用getFragmentManager()方法得到。
  • 开启一个事务,通过调用beginTransaction()方法开启。
  • 向容器内加入碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。
  • 提交事务,调用commit()方法来完成。
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.framLay,new TestFragment());
    transaction.addToBackStack(null);//用于将一个事务添加到返回栈中,即返回键,回到上一碎片中
    transaction.commit();
    

碎片之间传递数据

  • 使用Fragment类的setArguments()方法
  • Bundle类设置传递的数据
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    TestFragment testFragment = new TestFragment();
    Bundle bundle = new Bundle();
    bundle.putString("name","tom");
    testFragment.setArguments(bundle);    
    transaction.replace(R.id.framLay,testFragment);
    transaction.addToBackStack(null);//用于将一个事务添加到返回栈中,即返回键,回到上一碎片中
    transaction.commit();
    
  • 在Fragment类中使用getArguments()方法获取到Bundle对象,获取数据
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            Bundle bundle= getArguments();
            if (bundle != null){
                String name = bundle.getString("name");
                Toast.makeText(getContext(),name,Toast.LENGTH_LONG).show();
            }
            return inflater.inflate(R.layout.fragment_test, container, false);
    }
    
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容