添加新的布局,片段或新的敏捷性



我正在创建具有20页的应用程序(从页面上划过),在阅读了一些信息后,我终于混淆了什么是最佳解决方案。

  1. 仅创建新的布局并将它们添加到同一MainActivity?

  2. 每次创建新活动 新布局?

  3. 创建片段并将其添加到同一MainActivity?

片段和布局之间的实际区别是什么?

用于添加新布局/片段的Java中的代码是什么?

谢谢。

创建一个活动主页,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.example.myproj.activity.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay">
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <FrameLayout
                android:id="@+id/container"
                android:name="com.example.HomeFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </RelativeLayout>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

//设计您的片段

<?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"
    android:background="@color/white"
    android:clickable="true">
.....
.....
.....
</LinearLayout>

//java

  public class MainActivity extends AppCompatActivity{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
             ...
              // call MyFragment page here
            }
    .....
    .....
    .....
    }

//fragment

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

布局只是XML资源,它定义了布局在屏幕上的外观。碎片完全不同。将碎片视为具有与之相关的布局的迷你活动。就像活动一样,当创建碎片时,它们会夸大布局,然后呈现该布局。但是,片段需要附加到一项活动中,没有它就不可能存在。

对您来说,最好的解决方案是在ViewPager中使用片段,这使您可以从左到右滑动(反之亦然),并具有无限数量的页面

最新更新