微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 导航抽屉中的刷新标题

虽然我一直在四处寻找,但找不到正确的答案.
在我的导航抽屉标题中,我向用户提供了他的图像和名称,但我也允许用户从应用程序中更改他/她的名字和图像.
这些更改存储在会话管理器中.
现在我想在导航抽屉标题中反映这些变化.
一切都运行正常,因为当我关闭应用程序并再次运行它时,它会显示更改.
所以现在我需要的是一种刷新导航抽屉头的方法.

从Profile Fragment中的库中选择图像.

 private void onSelectFromgalleryResult(Intent data) {

        String selectedImagePath = getPathFromCameraData(data, getActivity());

        Bitmap bm;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(selectedImagePath, options);
        final int required_SIZE = 200;
        int scale = 1;
        while (options.outWidth / scale / 2 >= required_SIZE
                && options.outHeight / scale / 2 >= required_SIZE)
            scale *= 2;
        options.inSampleSize = scale;
        options.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(selectedImagePath, options);
        bitmap = bm;
        profilephoto = BitMapToString(bm);

        session.setprofilepic(profilephoto);// make changes in session.
          profilepic.setimageBitmap(bm);

    }

主页活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepageactivity);
        session = new SessionManager(getApplicationContext());

        mTitle = mDrawerTitle = getTitle();

        topToolBar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(topToolBar);
        //topToolBar.setlogo(R.drawable.logo);
        topToolBar.setlogoDescription(getResources().getString(R.string.logo_desc));


        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        LayoutInflater inflater = getLayoutInflater();
        listHeaderView = inflater.inflate(R.layout.header_list, null, false);

        //ImageView profile = (ImageView)listHeaderView.findViewById(R.id.profile_picture);

        TextView name = (TextView)listHeaderView.findViewById(R.id.headername);
        profilepic = (ImageView)listHeaderView.findViewById(R.id.profile);

        user = session.getUserDetails();
        profilepic.setimageBitmap(StringToBitMap(user.get(SessionManager.KEY_PROFILEPIC)));
        name.setText(user.get(SessionManager.KEY_NAME));
        mDrawerList.addHeaderView(listHeaderView); ////// HEADER ADDED


        List<ItemObject> listViewItems = new ArrayList<ItemObject>();
       listViewItems.add(new ItemObject("Attendance", R.drawable.attendance));
      //  listViewItems.add(new ItemObject("Time table", R.drawable.tiMetable));
       // listViewItems.add(new ItemObject("Class 1", R.drawable.classicon));

        adapter = new CustomAdapter(this, listViewItems);


        mDrawerList.setAdapter(new CustomAdapter(this, listViewItems));

        mDrawerToggle = new ActionBarDrawerToggle(Homepageactivity.this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle(mDrawerTitle);
                profilepic.setimageBitmap(StringToBitMap(user.get(SessionManager.KEY_PROFILEPIC)));
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.setDrawerIndicatorEnabled(true);

        getSupportActionBar().setdisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawerList.setonItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // make Toast when click
                Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
                selectItemFragment(position);
            }
        });
    }

解决方法:

虽然我很抱歉这个问题迟到了.
每当我打开抽屉时我都会通过刷新标题解决我的问题,或者我们可以在调用onDrawerOpened时解决问题.

    mDrawerToggle = new ActionBarDrawerToggle(Homepageactivity.this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle(mDrawerTitle);
            session = new SessionManager(getApplicationContext());
            user = session.getUserDetails();
            profilepic.setimageBitmap(StringToBitMap(user.get(SessionManager.KEY_PROFILEPIC)));
            name.setText(user.get(SessionManager.KEY_NAME));
            lastsynced.setText(lastsynced());
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐