Java 添加新布局、片段或新城市?
我正在创建一个20页的应用程序,从一页到另一页滑动,在阅读了一些信息后,我终于搞混了什么是最好的解决方案 仅创建新布局并将其添加到同一个MainActivity 每次创建新活动+新布局时 创建片段并将其添加到同一个MainActivity 片段和布局之间的实际区别是什么 Java中用于添加新布局/片段的代码是什么Java 添加新布局、片段或新城市?,java,android,Java,Android,我正在创建一个20页的应用程序,从一页到另一页滑动,在阅读了一些信息后,我终于搞混了什么是最好的解决方案 仅创建新布局并将其添加到同一个MainActivity 每次创建新活动+新布局时 创建片段并将其添加到同一个MainActivity 片段和布局之间的实际区别是什么 Java中用于添加新布局/片段的代码是什么 谢谢。创建一个活动主页,如下所示,用于使用片段 <?xml version="1.0" encoding="utf-8"?> <android.support.des
谢谢。创建一个活动主页,如下所示,用于使用片段
<?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>
//碎片
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中使用片段,这使您能够从左向右滑动,或从左向右滑动,并拥有无限多的页面这可能会让您感兴趣谢谢Majeed:谢谢Isuru。这是否允许保存按下单选按钮后每页的数据并将其存储在内存中,直到第20页?是的,包含所有片段的活动会将其保留在内存中,直到您明确要求它们分离。酷。非常感谢:让我们看看它现在是如何工作的。非常感谢。这是我对你的帖子Sunmugapriya的投票。很高兴听到。。如果你得到答案,请点击勾选。。通过你的投票,其他人也可以受益
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// call MyFragment page here
}
.....
.....
.....
}
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;
}
}