android - 如何使用 Android Studio 的 Tablayout Activity 在没有 fragment 的情况下更改布局?

标签 android android-layout android-fragments android-activity bluetooth

我希望 1 个 Activity 上有 3 个选项卡。每一个都包含通过 BT 发送字符串的不同按钮。 我会使用 fragment ,但我有蓝牙连接,每个教程等都告诉我使用 onAttach(Activity Activity) ,它已被废弃,我无法理解 Context 版本将蓝牙连接从主 Activity 传递到 fragment .

那么实现这一目标的最佳选择是什么?我不知道如何将 Tablayout Activity 更改为不使用 fragment 。

这是基本的activity_main.xml

<?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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/colorBackground"
    tools:context="com.example.rauhalamika.rcontrol.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:gravity="center|bottom">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

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

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

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.rauhalamika.rcontrol.MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

最后是MainActivity.class

package com.example.rauhalamika.rcontrol;

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    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.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "PRESETS";
                case 1:
                    return "MANUAL";
                case 2:
                    return "SETTINGS";
            }
            return null;
        }
    }
}

我已经清理了有关蓝牙的所有内容以简化我的问题。

但是如果你们认为以某种方式将 BT 连接传递到我的 fragment 会更容易,那么该怎么做呢?

提前致谢!

最佳答案

我设法这样做:

我创建了布局 a_layout、b_layout 和 c_layout (xml)

在我的 onCreateView 中我这样做了:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View A = inflater.inflate(R.layout.a_layout, container, false);
            View B = inflater.inflate(R.layout.b_layout, container, false);
            View C = inflater.inflate(R.layout.c_layout, container, false);
            switch (getArguments().getInt(ARG_SECTION_NUMBER)){
                case 1:
                    return a_layout;
                case 2:
                    return b_layout;
                case 3:
                    return c_layout;
            }
            return null;
        }

我休息了一会儿,去慢跑了。之后,我头脑清醒,意识到这是多么容易:)

关于android - 如何使用 Android Studio 的 Tablayout Activity 在没有 fragment 的情况下更改布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35380727/

相关文章:

android - onActivityResult 第二次调用时返回intent data = null

java - 通话录音,通话多个(重复)电话阶段并创建多个音频文件

Android:在更改选项卡时更改 tabLayout 中选项卡的自定义 View

java - 查找在参数方法中传递的对象的类型

Android:使用动画显示/隐藏 View

android - 一个 Activity 是否可以有多个 xml 布局?

android - 使用 fragment 时 viewModelFactory 变量尚未初始化

java - 动态列表从未加载 - 使用 parse.com

android - AlertDialog 按钮文本颜色与支持库 v24.2.1

android - 背景颜色( ListView ?)