0

我正在尝试仅使用片段进行构建。该应用程序以一个 Activity 空白打开,该空白只有一个 ActionBar 在单击汉堡包图标时它会打开一个抽屉,为您提供一个菜单选项。

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

现在的问题是细节片段显示主页图标,因为我启用了 setDisplayHomeAsUpEnabled(true) 但是当我点击后退箭头时它什么也没做。硬件后退按钮确实将我带回到上一个(回收器/卡片视图)片段。我在详细信息片段中也有 setHasOptionsMenu(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();
            }
        });
4

1 回答 1

0

对于那些可能有这个问题的人。事实证明,当使用 ActionBarDrawerToggle 时,需要将 setToolbarNavigationClickListener 用于后退箭头。

optionitemselected 中的 R.id.home 似乎已禁用。希望这可以帮助!

所以我所做的是从 onOptionsItemSelected(MenuItem item) 中删除:

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

并修改了在fragmentA和fragmentB的oncreate中调用的主Activity中的resetActionBar(boolean childAction)。

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);
    }
}
于 2017-02-19T15:30:30.230 回答