您的当前位置:首页正文

Android Function(3)| 共用Fragment

来源:华佗小知识

一.前言

Fragment碎片,可以当成是一个微型的Activity,并且一个Fragment能够嵌套进不同的Activity中。例如许多音乐App的每一个活动的下方都会有一个播放栏,不管你进入到哪一个页面中该播放栏都会存在下方,简单来说就是Fragment的共用。

二.实现

1.创建Fragment

先来看主代码:

public class MyFragment extends Fragment implements View.OnClickListener {

    private Button play;

    private Button next;

    private Button stop;


    public synchronized MyFragment newInstance(){
        return new MyFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreateView(inflater,container,savedInstanceState);
        View view = inflater.inflate(R.layout.fragment,container,false);
        play = (Button)view.findViewById(R.id.play);
        stop = (Button)view.findViewById(R.id.stop);
        next = (Button)view.findViewById(R.id.next);

        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        next.setOnClickListener(this);
        return view;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.play:
                Toast.makeText(getContext(), "play", Toast.LENGTH_SHORT).show();
                break;
            case R.id.stop:
                Toast.makeText(getContext(), "stop", Toast.LENGTH_SHORT).show();
                break;
            case R.id.next:
                Toast.makeText(getContext(), "next", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

接着是Fragment的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:background="#fff">

    <TextView
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="嘻唰唰"/>

    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="播放"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="停止"/>

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="下一首"/>

</LinearLayout>

2.创建Activity

我们在项目中创建出许多个Activity,然后在Activity的布局文件中来写布局,这里为了简单,就只用纯色来对Activity进行填充:

<!--MainActivity.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    
    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#78b"
    tools:context="com.example.yzbkaka.onefragment.MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.yzbkaka.onefragment.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="50dp">
    </fragment>
    
    <Button
        android:id="@+id/start_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="start second"/>

</LinearLayout>
<!--SecondActivity.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    
    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fff"
    tools:context="com.example.yzbkaka.onefragment.MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.yzbkaka.onefragment.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="50dp">
    </fragment>


    <Button
        android:id="@+id/start_third"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start_third"/>

</LinearLayout>
<!--ThirdActivity.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    
    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#076"
    tools:context="com.example.yzbkaka.onefragment.MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.yzbkaka.onefragment.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="50dp">
    </fragment>

</LinearLayout>

在activity的布局里面我们将创建好的MyFragment引入,然后设置一个按钮用于启动其他的Activity。需要注意的是在每一个Activity的布局文件中,引用的fragment的id都必须是相同的,在这里我用的id是andoird:id="@id/fragment"

接着我们来修改Activity的主代码,这里以MainActivity为例,后面的Activity都是相似的:

public class MainActivity extends FragmentBaseActivity {

  private Button start;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      start = (Button)findViewById(R.id.start_second);
      start.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent = new Intent(MainActivity.this,SecondActivity.class);
              startActivity(intent);
          }
      });
  }
}

这里我们让MainActivity继承FragmentBaseActivity,其他的Activity也是同样。FragmentBaseActivity代码如下:

public abstract class FragmentBaseActivity extends AppCompatActivity {

    private MyFragment myFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        showFragment();
    }


    private void showFragment(){
       FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
       if(myFragment == null){
           myFragment = MyFragment.newInstance();
           
       }else{
           
       }
    }
}

可以很清晰的看到,当每一个继承它的Activity在调用onCreate()方法时,都会调用父类的showFragment()方法,在该方法里面我们就是动态的将MyFragment进行添加。

最终效果