gpt4 book ai didi

android - 如何将导航菜单更改为后退箭头?

转载 作者:搜寻专家 更新时间:2023-11-01 08:27:03 24 4
gpt4 key购买 nike

我正在创建一个使用导航菜单的应用程序。当我点击菜单时,抽屉导航打开,我点击一个项目并打开一个 fragment 。我想要这样,当 fragment 打开的导航菜单变为后退箭头时。

这是我的 Activity 代码:

public class Container_Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

FrameLayout frameLayout;
private Boolean exit = false;
FloatingActionButton floatingActionButton;

static Container_Activity activity;

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

if (android.os.Build.VERSION.SDK_INT >= 21) {
Window window = this.getWindow();

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.statusbarcolor));

}
activity = this;

if (savedInstanceState == null) {
Container_Activity.showFragment(new Home_fragment(), "");

}


frameLayout = (FrameLayout) findViewById(R.id.container_frame);
floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back));
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}


DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Container_Activity.showFragment(new ConctactUs_fragment(), "");

}

});

}

public void showFloatingActionButton() {
floatingActionButton.show();
}

;

public void hideFloatingActionButton() {
floatingActionButton.hide();
}

;

@Override
public void onBackPressed() {

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);

} else {

super.onBackPressed();

}
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);

switch (item.getItemId()) {

case R.id.home_item:

showFragment(new Home_fragment(), "Home");

break;
case R.id.services:

showFragment(new Services_fragment(), "Services");
break;
case R.id.portfolio_item:

showFragment(new Portfolio_fragment(), "Portfolio");
break;
case R.id.training_item:

showFragment(new Training_fragment(), "Training");
break;
case R.id.gallery_item:

showFragment(new Gallery_Fragment(), "Gallery");
break;

case R.id.share_item:

shareurl();
break;
case R.id.reach_us:

showFragment(new ConctactUs_fragment(), "Contact");
break;
}

return false;
}

private void shareurl() {

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,
"Hey check out my app at: https://play.google.com/store/apps/details?id=com.techenrage");
sendIntent.setType("text/plain");
startActivity(sendIntent);
}


public static void showFragment(Fragment fragment, String Tag) {


FragmentManager manager = activity.getSupportFragmentManager();
FragmentTransaction transcation = manager.beginTransaction();
transcation.replace(R.id.container_frame, fragment, Tag);
transcation.addToBackStack(Tag);
transcation.commit();

}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int) ev.getRawX(), (int) ev.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(ev);
}


}

fragment 类:

public class Portfolio_fragment extends Fragment {

TabLayout tabLayout;
ViewPager viewPager;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.portfolio_fragment, container, false);

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Portfolio");

tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);

tabLayout.addTab(tabLayout.newTab().setText("Websites"));
tabLayout.addTab(tabLayout.newTab().setText("Mobile Application"));
tabLayout.addTab(tabLayout.newTab().setText("Graphics"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

viewPager = (ViewPager) view.findViewById(R.id.portfolio_pager);

Portfolio_Adapter adapter = new Portfolio_Adapter(getChildFragmentManager(), tabLayout.getTabCount());

viewPager.setAdapter(adapter);

tabLayout.setOnTabSelectedListener(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) {

}
});

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
return view;
}

最佳答案

试试这个

@Override
protected void onCreateView(Bundle savedInstanceState) {

((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_button);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

关于android - 如何将导航菜单更改为后退箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43863861/

24 4 0