gpt4 book ai didi

java - Android 中选项卡的程序结构

转载 作者:行者123 更新时间:2023-12-01 17:51:22 25 4
gpt4 key购买 nike

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

enter image description here

为此,我使用 TabActivity 和 FragmentManager。因此,编译器已经告诉我 TabActivity 已被弃用。如果此结构已被弃用,请告诉我并告诉我使用什么来代替。到目前为止,这是我的代码。它一直有效,直到我按下按钮。什么也没发生(只是按钮消失)。我已经尝试过 FragmentManager 的 add、hide、show 方法。请告诉我是否应该将整个代码放在这里。

MainActivity.java

package com.example.tabstackoverflow;

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.tabstackoverflow;

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

import com.example.tabstackoverflow.Tab1Activity;
import com.example.tabstackoverflow.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.tabstackoverflow;

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.tabstackoverflow;


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.tabstackoverflow.Tab1Fragment1"
android:id="@+id/fragmentPlaceholder"/>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

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

最佳答案

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

关于java - Android 中选项卡的程序结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60789582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com