繁体   English   中英

如何在导航抽屉中反印后更新当前所选项目

[英]How to Update current selected item after backpress in navigation drawer

我已经成功实现了导航抽屉,但我遇到了问题。

选择片段后,单击后退按钮,弹出当前片段,显示上一个片段。

但是显示片段的标题仍然是前一个片段的标题。

此外,在抽屉中检查的项目仍然是前一个片段的项目。

请问,你知道如何更新标题和检查项目吗?

我的活动

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private final String TAG = "MainActivity";
    NavigationView navigationView;


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        String about_blog = "http://example.com/page/json/urls/p-about";
        String contri = "http://example.com/page/json/urls/contribute";
        String privy = "http://example.com/page/json/urls/privacy-rules";
        String contact = "http://example.com/page/json/urls/contact_us";
        String advert = "http://example.com/page/json/urls/advertise-here";
        String spons = "http://example.com/page/json/urls/sponsor-us";

        Fragment fragment = null;

        if (id == R.id.menu_home) {
            fragment = new PostFragment();
        }

        if (id == R.id.about_blog) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_contri) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_contact) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_privy) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_advert) {
            fragment = new PageFragment();

        } else if (id == R.id.spons) {
            fragment = new PageFragment();

        } else if (id == R.id.app_about) {
            //fragment = new abtFragment();
        } else if (id == R.id.settings) {
            //fragment = new SettingFragment;
        }


        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();

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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Device rotated and onCreate called");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        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) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setCheckedItem(R.id.menu_home);
        navigationView.getMenu().performIdentifierAction(R.id.menu_home, 0);
    }

UPDATE

我已经解决了在每次背压后更新每个片段的标题的问题。 我做的是将标题作为字符串从MainActivity发送到片段,然后在片段中我收到它并调用:

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);

这是我的onNavigationItemSelected Code之后的样子

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    id = item.getItemId();
    Bundle bundle = new Bundle();
    String about_blog = "http://example.com/page/json/urls/p-about";
        String contri = "http://example.com/page/json/urls/contribute";
        String privy = "http://example.com/page/json/urls/privacy-rules";
        String contact = "http://example.com/page/json/urls/contact_us";
        String advert = "http://example.com/page/json/urls/advertise-here";
        String spons = "http://example.com/page/json/urls/sponsor-us";

    Fragment fragment = null;

    if (id == R.id.menu_home) {
        fragment = new PostFragment();
        bundle.putString("title", getString(R.string.home));
        fragment.setArguments(bundle);
    }

    if (id == R.id.about_blog) {
        bundle.putString("title", getString(R.string.about));
        fragment = new PageFragment();
        fragment.setArguments(bundle);


    } else if (id == R.id.nav_contri) {
        bundle.putString("title", getString(R.string.contri));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.nav_contact) {
        bundle.putString("title", getString(R.string.contac));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.nav_privy) {
        bundle.putString("title", getString(R.string.privy_p));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.nav_advert) {
        bundle.putString("title", getString(R.string.advert));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.spons) {
        bundle.putString("title", getString(R.string.spons));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.app_about) {
        //fragment = new abtFragment();
    } else if (id == R.id.settings) {
        //fragment = new SettingFragment;
    }



    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, fragment, "visible_fragment");
    ft.addToBackStack(null);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();

    //setTitle(item.getTitle()); // No longer needed.

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

在片段的onCreateView中,我收到了String:

String title = getArguments().getString("title");

然后仍然在onCreateView我做了:

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);

标题现在正在更新。

现在唯一的问题是导航抽屉项目拒绝更新。

Android Studio也在抱怨方法调用((AppCompatActivity)getActivity())。getSupportActionBar()。setTitle(title); mayproduce java.lang.NullPointerException

你也可以这样做。

   @Override
    public void onBackPressed() {
        super.onBackPressed();
         getSupportActionBar().setTitle(getResources().getString(R.string.titlefragment));
    }

您需要为每个Fragment Transaction设置Title

尝试通过更改onNavigationItemSelected此操作。

Fragment fragment = null;
String title = "Test";

if (id == R.id.menu_home) {
    fragment = new PostFragment();
    title = "Post";
}

if (id == R.id.about_blog) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.nav_contri) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.nav_contact) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.nav_privy) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.nav_advert) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.spons) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.app_about) {
    //fragment = new abtFragment();
} else if (id == R.id.settings) {
    //fragment = new SettingFragment;
}

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

   **// set the toolbar title
   getSupportActionBar().setTitle(title);**
}

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

编辑1:

OverRide onResume of your MainActivity

使用此代码。

@Override
public void onResume() {
    super.onResume();
    // Set title
    getActivity().getActionBar()
        .setTitle("Your Title Goes Here");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM