actionbar upnavigation from fragmentB to fragmentA



我正在尝试仅使用片段构建。该应用程序打开一个活动空白,仅在单击"汉堡"图标时只有一个动作栏,它打开了一个为您提供菜单选项的抽屉。

单击一个菜单项之一,它打开了带有回收器/卡视图的第一个片段。单击其中一张卡后,它将打开一个新片段,其中包含所选卡的更多详细信息。

现在问题是细节片段显示家庭图标,因为我启用了setDisplayHomeAsupeNabled(true),但是当我单击后箭头时,它不会做任何事情。硬件返回按钮确实将我带回了上一个(回收器/卡视图)片段。我在细节片段中也有sethasasoptionsmenu(true)。我到处都放置日志标签,以查看何时"主页"按钮反应,但什么都没有。

希望有人可以给我一只手。

活动:

public class AppStart extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
ActionBarDrawerToggle actionBarDrawerToggle;
final String TAG = "AppSart: onBackPressed";
final String TAG1 = "AppSart: resetActionBar";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_start);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
@Override
public void onBackPressed() {
    int stack = getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG,Integer.toString(stack));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return false;
}
public void resetActionBar(boolean childAction)
{
    Log.d(TAG1,Boolean.toString(childAction));
    if (childAction) {
        actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    } else {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
    }
}
}

回收片段:

public class ProductFragment extends Fragment {
final String TAG1 = "ProdFrag: onCreate";
final String TAG2 = "ProdFrag: onCreateView";
final String TAG3 = "ProdFrag: onResume";
final String TAG6 = "ProdFrag: ActionSetting";
final String TAG7 = "ProdFrag: home";
public ProductFragment() {
    // Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG1,Integer.toString(stack));
    boolean canback = stack>0;
    ((AppStart)getActivity()).resetActionBar(canback);
}
@Override
public void onResume() {
    super.onResume();
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG3,Integer.toString(stack));
    boolean canback = stack>0;
    ((AppStart)getActivity()).resetActionBar(canback);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.fragment_product,container,false);        
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG2,Integer.toString(stack));
    return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.toolbar_menu, menu);
    super.onCreateOptionsMenu(menu,inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    switch (item.getItemId()) {
        case R.id.action_settings:
            Log.d(TAG6,Integer.toString(stack));
            return true;
        default:
            break;
    }
    return false;
}
}

细节片段:

public class ProductTabsFragment extends Fragment {
final String TAG1 = "TabFrag: onCreate";
final String TAG2 = "TabFrag: onResume";
final String TAG3 = "TabFrag: ActionSettings";
final String TAG4 = "TabFrag: home";
final String TAG5 = "TabFrag: onBckStkChng";
final String TAG6 = "TabFrag: onNavigateUp";
public ProductTabsFragment() {
    // Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG1,Integer.toString(stack));
    boolean canback = stack>0;
    ((AppStart)getActivity()).resetActionBar(canback);
}
@Override
public void onResume() {
    super.onResume();
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    Log.d(TAG2,Integer.toString(stack));
    boolean canback = stack>0;
    ((AppStart)getActivity()).resetActionBar(canback);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_product_tabs, container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.toolbar_menu, menu);
    super.onCreateOptionsMenu(menu,inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int stack = getActivity().getSupportFragmentManager().getBackStackEntryCount();
    switch (item.getItemId()) {
        case R.id.action_settings:
            Log.d(TAG3,Integer.toString(stack));
            return true;
        case android.R.id.home:
            Log.d(TAG4,Integer.toString(stack));
            //getActivity().onBackPressed();
            return true;
        default:
            break;
    }
    return false;
}
}

这也是回收器适配器中的代码,因为单击卡以加载细节片段:

cvProduct.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                ProductTabsFragment productTabsFragment = new ProductTabsFragment();
                AppCompatActivity activity = (AppCompatActivity) view.getContext();
                FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container,productTabsFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

对于那些可能有这个问题的人。事实证明,当使用ActionBardRawerToggle时,需要将SettoolBarnavigationClickListener用于后row。

OptionItemselected中的r.id.home似乎被禁用。希望这会有所帮助!

所以我所做的是从onoptionsitemselected(menuitem item)中删除:

case android.R.id.home:
        Log.d(TAG4,Integer.toString(stack));
        return true;

和修改的RESETACTIONBAR(布尔儿童)在主要活动中被称为Fragmenta和FragmentB的征召。

public void resetActionBar(boolean childAction)
{
    Log.d(TAG1,Boolean.toString(childAction));
    if (childAction) {
        actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.d(TAG4,"Clicked");
                onBackPressed();
            }
        });
    } else {
        getSupportActionBar().setHomeButtonEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
    }
}

相关内容

  • 没有找到相关文章

最新更新