因此,我有一个“主页”活动,其中包含一个导航抽屉,我有一个名为“ Trade”的标签,同样,我还有一个名为“ Trade”的标签(以及其他几个标签,可以通过一个viewpager)。当用户在导航抽屉中选择“交易”时,将创建一个名为Trade.java的片段,并激活其布局。问题是,当触发片段的布局时,它不会在活动布局的顶部创建,因此当我与其片段布局(片段的)进行交互时,它不会干扰活动布局。相反,它正在做的是创建片段的布局并在屏幕上显示它,但是我仍然可以在活动布局中的选项卡上滑动(通过viewpager)。我将使Trade类成为活动,但是由于TabLayout也正在使用它(不仅是导航抽屉),所以我不能这样做。
这是首页活动
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.alexl.silkroadja.NavigationDrawer.Account;
import com.example.alexl.silkroadja.NavigationDrawer.Categories;
import com.example.alexl.silkroadja.NavigationDrawer.Messages;
import com.example.alexl.silkroadja.NavigationDrawer.Notifications;
import com.example.alexl.silkroadja.NavigationDrawer.Settings_Preferences;
import com.example.alexl.silkroadja.NavigationDrawer.Trade;
import com.example.alexl.silkroadja.NavigationDrawer.TradeHistory;
import com.example.alexl.silkroadja.NavigationDrawer.WatchList;
import com.google.firebase.database.FirebaseDatabase;
public class Homepage extends AppCompatActivity {
private DrawerLayout drawerLayout;
private FirebaseDatabase fb;
private TabLayout tabLayout;
private ViewPager viewPager;
public TabsPagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
Toast.makeText(this, "You are now in the " + getClass().getName() + " class",Toast.LENGTH_LONG).show();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.hamburger_menu);
tabLayout = findViewById(R.id.tabs);
viewPager = findViewById(R.id.viewPager);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
item.setChecked(true); // Item will stay highlighted when clicked
switch(item.getItemId())
{
case R.id.Homepage:
drawerLayout.closeDrawers();
break;
case R.id.Account:
startActivity(new Intent(getApplicationContext(), Account.class));
drawerLayout.closeDrawers();
break;
case R.id.Notifications:
startActivity(new Intent(getApplicationContext(), Notifications.class));
drawerLayout.closeDrawers();
break;
case R.id.Messages:
startActivity(new Intent(getApplicationContext(), Messages.class));
drawerLayout.closeDrawers();
break;
case R.id.Categories:
startActivity(new Intent(getApplicationContext(), Categories.class));
drawerLayout.closeDrawers();
break;
case R.id.Trade:
openFragment(new Trade());
drawerLayout.closeDrawers();
break;
case R.id.WatchList:
openFragment(new WatchList());
drawerLayout.closeDrawers();
break;
case R.id.TradeHistory:
startActivity(new Intent(getApplicationContext(), TradeHistory.class));
drawerLayout.closeDrawers();
break;
case R.id.Settings:
startActivity(new Intent(getApplicationContext(), Settings_Preferences.class));
drawerLayout.closeDrawers();
break;
default:
break;
}
return true;
}
});
setUpViewPager();
}
public void openFragment(Fragment fragment)
{
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, fragment);
ft.addToBackStack(null);
ft.commit();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
}
return super.onOptionsItemSelected(item);
}
private void setUpViewPager()
{
pagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(new Items());
pagerAdapter.addFragment(new Services());
pagerAdapter.addFragment(new Trade());
pagerAdapter.addFragment(new WatchList());
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setText("Items");
tabLayout.getTabAt(1).setText("Services");
tabLayout.getTabAt(2).setText("Trade");
tabLayout.getTabAt(3).setText("Watch List");
}
}
这是首页的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="@+id/main_content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"></android.support.v7.widget.Toolbar>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
tools:context=".NavigationDrawer.Categories">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/TestColor"
android:elevation="10dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabMode="scrollable"
app:layout_collapseMode="parallax"></android.support.design.widget.TabLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:menu="@menu/nav_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
交易片段类
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.alexl.silkroadja.R;
public class Trade extends Fragment
{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.trade_layout, container, false);
return view;
}
}
交易片段布局
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello There "
android:textSize="20dp"
android:layout_centerInParent="true"
android:gravity="center_horizontal"/>
</RelativeLayout>
这是显示我的问题的几个屏幕截图