java - Android:单个 Activity 中的多个页面

标签 java android

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

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

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

我知道在 Android 中我必须遵循完全不同的方法,但是,正如我所说,我很困惑: fragment ?没有 fragment 吗?还有什么?

谢谢!

最佳答案

要在同一个 Activity 中实现多个页面,您需要使用 fragment 。对于您的情况,我们需要 3 个 fragment 。您的 Activity 布局文件应包含一个ViewPager,它将保存 Activity 中的 fragment 。如果您希望 fragment 也可以通过选项卡访问,您还可以添加 TabLayout。 示例 Activity 布局如下:

<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>

完成此操作后,您需要在 Activity 类中创建一个 FragmentPagerAdapter 类。该适配器将管理 ViewPager 的实例化,该 ViewPager 保存 Activity 中的 fragment 。 这是一个示例 FragmentPagerAdapter 子类

public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.

            Log.d(LOG_TAG, "postion " + position);
            if (position == 0) {
                return Fragment1.newInstance(position + 1);
            } else if (position == 1) {
                return Fragment2.newInstance(position + 1);
            } else {
                return Fragment3.newInstance(position + 1);
            }

        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return "Fragment1";
                case 1:
                    return "Fragment2";
                case 2:
                    return "Fragment3";


            }
            return null;
        }

    }

完成后,您现在可以在 Activity onCreate 方法中获取对 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) {

            }
        });


 }

希望您能明白这一点。更多信息您还可以查看here

关于java - Android:单个 Activity 中的多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34996165/

相关文章:

Java - 可以在运行时创建一个没有字节码的新类吗?

java - 如何使用可调整大小的 JButton 制作紧凑的网格?

java - 可绘制路径数组返回 null

android - 可展开的 ListView onGroupExpanded 和 onGroupCollapsed

java.jdbc clojure 执行!插入数

java mysql嵌套搜索实现

java - 使 VideoView 中的视频静音

java - 应用程序运行时不显示任何内容

android - 点击通知后打开应用程序

android - 单击切换按钮时 TextInputLayout (TextInputEditText) NullpointerException