Android Fragments 刷新导航抽屉中的标题

hmtdttj4  于 11个月前  发布在  Android
关注(0)|答案(2)|浏览(139)

虽然我一直在寻找周围,无法找到正确的答案。在我的导航抽屉标题我提供用户的形象和姓名,但我也允许用户更改他的/她的名字和图像从应用程序.这些变化存储在会话管理器.现在我想反映在我的导航抽屉标题这些变化.一切都工作正常,因为当我关闭应用程序,并再次运行它显示的变化.所以现在我需要的是一种刷新导航抽屉标题的方法。
从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);
            }
        });
    }

ffx8fchx

ffx8fchx1#

虽然我真的很抱歉这么晚才公布答案,但我解决了这个问题,每当我打开抽屉时,或者当调用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()
        }
    };

字符串

4ktjp1zp

4ktjp1zp2#

这个问题已经被问了很多年了,但是如果你找到了这个答案,我的解决方案如下

drawer.addDrawerListener( new DrawerLayout.DrawerListener() {

    @Override
    public void onDrawerStateChanged(int newState) {
        if(newState == 2){
            // do your things here
        }
    }

    // ...
    // several other override functions

});

字符串
newState 2表示“drawer about to move”afaik,包括打开和关闭。这样,用户不会看到你改变drawer内容的过程,这与onDrawerOpened方法不同。但是你可能想利用onDrawerOpened方法来检查drawer的实际状态。

相关问题