Android Function(3)| 共用Fragment

一.前言

Fragment碎片,可以當(dāng)成是一個微型的Activity,并且一個Fragment能夠嵌套進不同的Activity中。例如許多音樂App的每一個活動的下方都會有一個播放欄,不管你進入到哪一個頁面中該播放欄都會存在下方,簡單來說就是Fragment的共用。

二.實現(xiàn)

1.創(chuàng)建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
    xmlns:android="http://schemas.android.com/apk/res/android"
    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.創(chuàng)建Activity

我們在項目中創(chuàng)建出許多個Activity,然后在Activity的布局文件中來寫布局,這里為了簡單,就只用純色來對Activity進行填充:

<!--MainActivity.xml-->
<?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"
    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 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"
    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 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"
    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的布局里面我們將創(chuàng)建好的MyFragment引入,然后設(shè)置一個按鈕用于啟動其他的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();
           fragmentTransaction.add(R.id.fragment,myFragment).commit();
       }else{
           fragmentTransaction.show(myFragment).commit();
       }
    }
}

可以很清晰的看到,當(dāng)每一個繼承它的Activity在調(diào)用onCreate()方法時,都會調(diào)用父類的showFragment()方法,在該方法里面我們就是動態(tài)的將MyFragment進行添加。

最終效果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容