Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:一个活动包含多个页面_Java_Android - Fatal编程技术网

Java Android:一个活动包含多个页面

Java Android:一个活动包含多个页面,java,android,Java,Android,我是一名Android初学者(从1995年起我就是一名Windows开发者),我正在开发我的第一个应用程序。 我有一个二维数据数组(类似于[2][])。 基本上,它表示需要在设备屏幕上表示的三组不同数据。 不幸的是,虽然其中两个可以用网格表示,但第三个集合必须用完全不同的方式表示。 所以我需要的是一个三页的活动;在前两个页面中,我希望使用一个通用的布局,填充所需的数据,而在第三个页面中,我必须使用一个完全不同的布局 我发现了这一点:,但不幸的是,我对在三个页面上放置代码以处理用户输入感到困惑 作

我是一名Android初学者(从1995年起我就是一名Windows开发者),我正在开发我的第一个应用程序。 我有一个二维数据数组(类似于[2][])。 基本上,它表示需要在设备屏幕上表示的三组不同数据。 不幸的是,虽然其中两个可以用网格表示,但第三个集合必须用完全不同的方式表示。 所以我需要的是一个三页的活动;在前两个页面中,我希望使用一个通用的布局,填充所需的数据,而在第三个页面中,我必须使用一个完全不同的布局

我发现了这一点:,但不幸的是,我对在三个页面上放置代码以处理用户输入感到困惑

作为一个Android的初学者,我实际上对在这种情况下应该采取什么样的一般方法有点困惑。 在我通常的开发经验中,我会创建一个带有选项卡控件、三个选项卡、前两个网格和第三个自定义控件的窗口。 这三个控件将位于同一“作用域”(我的窗口)中,并将在窗口加载期间填充

我知道在安卓系统中,我必须遵循一种完全不同的方法,但是,正如我所说的,我很困惑:片段?没有碎片?还有什么


谢谢大家!

您可以在活动中处理用户输入,也可以将其分解为片段。下文对这两种情况进行了说明

活动

问题通常是,在活动的
onCreate()
方法期间,您在布局中获取对
视图的引用,并执行任何设置(添加
OnClickListener
s等),但在这种情况下,这是不可能的,因为此时您的
ViewPager
还没有加载任何页面,并且这些页面上的视图还不在视图层次结构中。(当涉及到
ViewPager
s时,Android的视图和布局系统可能会相当混乱)

相反,您必须获取引用并在适配器的
instantiateItem()
中执行设置,并可能在
destroyItem()
中使这些引用无效

片段

上述问题可能是框架中直接支持片段的原因(使用
FragmentPagerAdapter
FragmentStatePagerAdapter
)。使用每个页面的
片段
,您可以获取对该页面上视图的引用,并在
onCreateView()
(或
onViewCreated()
)中执行设置,业务逻辑也可以封装在片段中。基本上,与活动的
onCreate()
不同,您不必担心视图的存在与这些回调的时间有关


另一个好处是,通过将逻辑封装在单独的片段中,您的应用程序具有一定程度的模块化。如果你可以设想你的应用程序在一个更大的屏幕上同时显示两个(甚至全部三个)你的“页面”,那么最好从一开始就使用片段。

你可以在活动中处理用户输入,也可以将其分解成片段。下文对这两种情况进行了说明

活动

问题通常是,在活动的
onCreate()
方法期间,您在布局中获取对
视图的引用,并执行任何设置(添加
OnClickListener
s等),但在这种情况下,这是不可能的,因为此时您的
ViewPager
还没有加载任何页面,并且这些页面上的视图还不在视图层次结构中。(当涉及到
ViewPager
s时,Android的视图和布局系统可能会相当混乱)

相反,您必须获取引用并在适配器的
instantiateItem()
中执行设置,并可能在
destroyItem()
中使这些引用无效

片段

上述问题可能是框架中直接支持片段的原因(使用
FragmentPagerAdapter
FragmentStatePagerAdapter
)。使用每个页面的
片段
,您可以获取对该页面上视图的引用,并在
onCreateView()
(或
onViewCreated()
)中执行设置,业务逻辑也可以封装在片段中。基本上,与活动的
onCreate()
不同,您不必担心视图的存在与这些回调的时间有关


另一个好处是,通过将逻辑封装在单独的片段中,您的应用程序具有一定程度的模块化。如果你可以想象你的应用程序在一个更大的屏幕上同时显示两个(甚至全部三个)你的“页面”,那么最好从一开始就使用片段。

要在同一活动中实现多个页面,你需要使用片段。在你的情况下,我们需要3个碎片。您的活动布局文件应该包含一个查看页面,它将保存活动中的片段。如果希望片段也可以通过选项卡访问,也可以添加表格布局。 示例活动布局可以如下所示:

<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="your.package.name.MainActivity">

    <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="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:titleTextColor="@color/colorAccent"
            />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="fill"
            android:background="@color/grey"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorHeight="5dp"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabTextColor="@color/black"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


</android.support.design.widget.CoordinatorLayout>
完成此操作后,您现在可以在ActivityonCreate方法中获取对ViewPager的引用,并按如下所示设置ViewPager适配器

@Override
    protected void onCreate(Bundle savedInstanceState) {

ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
       SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mSectionsPagerAdapter);

// setting the views to be accessed through tabs
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int currentTab = tab.getPosition();
                viewPager.setCurrentItem(currentTab);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


 }

希望你明白这一点。有关更多信息,您还可以选中

要在同一活动中实现多个页面,您需要使用片段。在你的情况下,我们需要3个碎片。您的活动布局文件应该包含一个查看页面,它将保存活动中的片段。如果希望片段也可以通过选项卡访问,也可以添加表格布局。 示例活动布局可以如下所示:

<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="your.package.name.MainActivity">

    <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="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:titleTextColor="@color/colorAccent"
            />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="fill"
            android:background="@color/grey"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorHeight="5dp"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabTextColor="@color/black"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


</android.support.design.widget.CoordinatorLayout>
完成后,您现在可以使用ge