Android 中选项卡的程序结构

Program Structure for Tabs in Android

我正在尝试构建一个简单的应用程序,它通过选项卡导航(并通过例如按钮进行进一步导航)。 我画了一个我想要构建的小场景。 场景描述

为此,我使用了 TabActivity 和 FragmentManager。 因此,编译器已经告诉我 TabActivity 已被弃用。如果不推荐使用此结构,请告诉我并告诉我应该使用什么。到目前为止,这是我的代码。它一直有效,直到我按下按钮。没有任何反应(只是按钮消失了)。我已经尝试了 FragmentManager 的添加、隐藏、显示方法。请让我知道我是否应该将我的全部代码放在这里。

MainActivity.java

package com.example.tabWhosebug;

import android.app.TabActivity;
import android.os.Bundle;

import android.widget.TabHost;

public class MainActivity extends TabActivity {

    HandleTabActivity tabAct;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_layout);
        tabAct = new HandleTabActivity();
        TabHost tabHost = tabAct.getTabActivity(this);
        tabHost.setCurrentTab(0);

    }
}

HandleTabActivity.java

package com.example.tabWhosebug;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.widget.TabHost;

import com.example.tabWhosebug.Tab1Activity;
import com.example.tabWhosebug.Tab2Activity;

public class HandleTabActivity {
    HandleTabActivity() {

    }

    public TabHost getTabActivity (TabActivity curActivity) {
        Resources ressources = curActivity.getResources();
        TabHost tabHost = curActivity.getTabHost();

        //Tab 1
        Intent intentOwn = new Intent().setClass(curActivity, Tab1Activity.class);
        TabHost.TabSpec tabSpecOwn = tabHost
                .newTabSpec("Tab 1")
                .setIndicator("", ressources.getDrawable(R.drawable.pic_tab1))
                .setContent(intentOwn);

        //Tab 2
        Intent intentGet = new Intent().setClass(curActivity, Tab2Activity.class);
        TabHost.TabSpec tabSpecGet = tabHost
                .newTabSpec("Tab 2")
                .setIndicator("", ressources.getDrawable(R.drawable.pic_tab2))
                .setContent(intentGet);

        // add all tabs
        tabHost.addTab(tabSpecOwn);
        tabHost.addTab(tabSpecGet);
        return tabHost;
    }
}

Tab1Activity.java

package com.example.tabWhosebug;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;


public class Tab1Activity extends AppCompatActivity implements Tab1Fragment1.SetViewToTab2 {

    private final String TAG = "<GetHelpTAG>";

    private Fragment fragment1;
    private Fragment fragment2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab1);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        fragment1 = Tab1Fragment1.newInstance();
        fragment2 = Tab1Fragment2.newInstance();

        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        transaction.replace(R.id.fragmentPlaceholder, fragment2);
        transaction.commit();

    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        if (fragment instanceof Tab1Fragment1) {
            Tab1Fragment1 getHelpSearchFragment = (Tab1Fragment1) fragment;
            getHelpSearchFragment.setListener(this);
        }
    }

    @Override
    public void onButtonClicked() {
        Toast.makeText(getBaseContext(),
                "OnClickListener !!!",
                Toast.LENGTH_SHORT).show();

        fragment2 = Tab1Fragment2.newInstance();

        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.show(fragment2);
        transaction.replace(R.id.fragmentPlaceholder, fragment2);

        transaction.commit();
    }

}

Tab1Fragment1.java

package com.example.tabWhosebug;


import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class Tab1Fragment1 extends Fragment {

    private Button mSearchButton;
    private final String TAG = "<Tab1Fragment1TAG>";
    SetViewToTab2 callback;


    public static Tab1Fragment1 newInstance() {
        Bundle args = new Bundle();
        Tab1Fragment1 fragment = new Tab1Fragment1();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_tab1_view1, container, false);

        addListenerOnButton(v);
        return v;
    }

    public void addListenerOnButton(View v) {

        mSearchButton = v.findViewById(R.id.button);

        mSearchButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "pushed");

                callback.onButtonClicked();

            }

        });
    }

    public void setListener (SetViewToTab2 callback) {
        this.callback = callback;
    }

    public interface SetViewToTab2 {
        public void onButtonClicked();

    }
}

activity_tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
    tools:context=".Tab1Activity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </com.google.android.material.appbar.AppBarLayout>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.tabWhosebug.Tab1Fragment1"
        android:id="@+id/fragmentPlaceholder"/>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

我会根据需要更新我的问题。谢谢指教!

我发现我拥有的选项卡实际上与 Navigation 不同。使用提供的 NavController 非常简单,甚至还有一个 UI 来可视化应用程序的导航。