Android-碎片

碎片

嵌入在活动中的UI片段。

最简单的

建立一个左碎片布局,有一个按钮

<?xml version="1.0" encoding="utf-8"?>
<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>

再建立一个右碎片布局,显示文本信息。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#00ff00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="this is right fragment"/>
</LinearLayout>

建一个类,继承自Fragment。
注意:选用support-v4库。

public class LeftFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return view;
    }
}

同样建立一个关于右边的继承

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

对主活动布局中代码进行修改,引入两个碎片,并加入android:name属性,引入碎片类名。完成。

动态添加碎片

首先添加另一个布局(要动态加入的)
只是更改了背景颜色和文本信息。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#ffff00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <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>

同样,我们要为他新建一个类,继承自Fragment。

public class AnotherRightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.another_right_fragment,container,false);
        return view;
    }
}

然后,在主活动布局中,将上个程序的右碎片替换为一个帧布局。

<?xml version="1.0" encoding="utf-8"?>
<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.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

    </FrameLayout>

最后在主活动中注册点击事件,动态加入碎片:

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());
    }
    @Override
    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.commit();
    }
}

另外可以添加transaction.addToBackStack(null);返回栈。
通信:
RightFragment rightFragment = (RightFragment)getSupportFragmentManager().findFragmentById(R.id.left_fragment);

碎片的生命周期

  • 运行:碎片可见,活动运行,碎片运行
  • 暂停:活动暂停,碎片暂停
  • 停止:活动停止,碎片停止
  • 销毁:活动销毁,碎片销毁

在活动的基础上,碎片又有以下方法:

  • onAttach()活动碎片建立关联
  • onCreateView()为碎片创建视图
  • onActivityCreated()活动与碎片创建完毕
  • onDestoryView()碎片视图移除
  • onDeatach()碎片活动解除关联

猜你喜欢

转载自blog.csdn.net/WangJiaiaia/article/details/86614564
今日推荐