gpt4 book ai didi

java - 为什么 fragment 不在我的抽屉导航中显示布局

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:04 25 4
gpt4 key购买 nike

我创建了一个包含草稿并已发送的抽屉导航,我在我的 MainActivity 中设置了 SentFragment 和 DraftFragment,就像下面的代码一样,但它没有 Intent 或显示布局?我没有任何错误。发生了什么事?

草图 fragment

package com.test.mypc.androidtestme;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class DraftFragment extends Fragment {

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

发送 fragment

package com.test.mypc.androidtestme;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class SentFragment extends Fragment {


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

}
}

主要 Activity

   mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new SentFragment()).commit();


mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();

if (menuItem.getItemId() == R.id.nav_item_sent) {

FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new SentFragment()).commit();

}
if (menuItem.getItemId() == R.id.nav_item_draft) {
FragmentTransaction ft_drafts = mFragmentManager.beginTransaction();
ft_drafts.replace(R.id.containerView, new DraftFragment()).commit();
}
return false;
}

});

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();

最佳答案

在您的 MainActivity 中使用此代码:-

  public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
fragment = null;
int id = item.getItemId();
String get_title = item.getTitle().toString();

if (id == R.id.nav_item_sent) {

fragment = new SentFragment();

} else if (id == R.id.nav_item_draft) {
fragment = new DraftFragment();

}

if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}

title_tv.setText(get_title);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

关于java - 为什么 fragment 不在我的抽屉导航中显示布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35620575/

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