【发布时间】:2020-07-10 20:30:07
【问题描述】:
我有一个 Main Activity,它为我的不同 Fragment 托管 tabsAdapter 和 ViewPager。我正在尝试从我的主要活动中打开第二个片段(特殊片段)。下面是代码,无法启动特殊片段,其次,无法从特殊片段中引用方法。请帮忙!
主要活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Day Events"));
tabLayout.addTab(tabLayout.newTab().setText("Special Events"));
tabLayout.addTab(tabLayout.newTab().setText("Notifications"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final TabsAdapter tabsAdapter = new TabsAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(tabsAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.getString("fragment") != null) {
SpecialFragment specialFragment = (SpecialFragment) tabsAdapter.getItem(1);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.view_pager, specialFragment).commit();
specialFragment.setLocation("TEST");
特殊片段sn-p:
public void setLocation(String location)
{
txtLocation = v.findViewById(R.id.txtLocationSpecialEvent);
txtLocation.setText(location);
}
activity_main.xml
<RelativeLayout 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:orientation="vertical"
android:id="@+id/fragment_container"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tool_bar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
【问题讨论】:
-
请添加logcat
-
你想让第二个片段默认打开吗?
-
您好,请尽一切可能提供帮助。谢谢。第二个片段只能在单击通知时打开。如果应用默认打开,则必须打开第一个片段。
标签: java android android-fragmentactivity