How to refresh list of the navigation drawer as soon as its open/closed in Android
Asked Answered
W

6

12

I have created navigation drawer and displaying items in list. My list is static but i am displaying points. So when users select the items in the list and do some activity he get the points. So i want to update the points as soon as he gets. How can i do this./

Below is my code:

Activity Class:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_page);
    // Initializing
    dataList = new ArrayList<DrawerItem>();
    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    sharedPreferences = getSharedPreferences(Constant.LOGIN_CHECK,
            Constant.PRIVATE_MODE);
    editSharedPreferences = sharedPreferences.edit();
    if (sharedPreferences.getBoolean(Constant.IS_ALREADY_LOGIN,
            false)) {
        loginInBackground();
    }
    String p1 = sharedPreferences.getString(Constant.USERS_POINTS,
            "");
    String p2 = sharedPreferences.getString(
            Constant.LEVEL_ONE_POINTS, "");
    String p3 = sharedPreferences.getString(
            Constant.LEVEL_TWO_POINTS, "");

    String pointText = " POINTS";
    dataList.add(new DrawerItem("OVERVIEW", R.drawable.ic_action_eye,
            Color.DKGRAY, Color.TRANSPARENT));
    dataList.add(new DrawerItem("EARN POINTS", R.drawable.ic_action_dollor,
            Color.GREEN, Color.TRANSPARENT));
    dataList.add(new DrawerItem("MY POINTS ", p1 + pointText, Color.BLACK,
            Color.BLACK));
    dataList.add(new DrawerItem("REFERRALS POINTS LV1 ", p2 + pointText,
            Color.BLACK, Color.BLACK));
    dataList.add(new DrawerItem("REFERRALS POINTS LV2 ", p3 + pointText,
            Color.BLACK, Color.BLACK));
    dataList.add(new DrawerItem("WITHDRAW REWARDS",
            R.drawable.ic_action_home, Color.DKGRAY, Color.TRANSPARENT));
    dataList.add(new DrawerItem("MY PROFILE", R.drawable.ic_action_settings,
            Color.DKGRAY, Color.TRANSPARENT));
    dataList.add(new DrawerItem("HELP", R.drawable.help_d, Color.DKGRAY,
            Color.TRANSPARENT));

    adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
            dataList);

    mDrawerList.setAdapter(adapter);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        SelectItem(0);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    String pointsLeft = sharedPreferences.getString(
            Constant.POINTS_LEFT, "");
    getMenuInflater().inflate(R.menu.main, menu);
    this.menu = menu;
    MenuItem pointsMenu = menu.findItem(R.id.action_settings);
    pointsMenu.setTitle(pointsLeft + " Points");
    return true;
}

public void SelectItem(int possition) {

    Fragment fragment = null;
    Bundle args = new Bundle();
    switch (possition) {
    case 0:
        fragment = new FragmentWelcome();
        args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
                .getItemName());
        args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
                dataList.get(possition).getImgResID());
        fragment.setArguments(args);
        FragmentManager frgManager0 = getFragmentManager();
        frgManager0.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
        break;
    case 1:
        fragment = new FragmentAppList();
        args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
                .getItemName());
        args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
                dataList.get(possition).getImgResID());
        fragment.setArguments(args);
        FragmentManager frgManager1 = getFragmentManager();
        frgManager1.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
        break;
    case 5:
        fragment = new FragmentRedeemPoints();
        args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
                .getItemName());
        args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
                dataList.get(possition).getImgResID());
        fragment.setArguments(args);
        FragmentManager frgManager2 = getFragmentManager();
        frgManager2.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
        break;
    case 6:
        fragment = new SettingsFragment();
        args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
                .getItemName());
        args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
                dataList.get(possition).getImgResID());
        fragment.setArguments(args);
        FragmentManager frgManager6 = getFragmentManager();
        frgManager6.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
        break;
    case 7:
        fragment = new HelpFragment();
        args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
                .getItemName());
        args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
                dataList.get(possition).getImgResID());
        fragment.setArguments(args);
        FragmentManager frgManager7 = getFragmentManager();
        frgManager7.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
        break;

    default:

        break;
    }

    mDrawerList.setItemChecked(possition, true);
    mDrawerLayout.closeDrawer(mDrawerList);

}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return false;
}

private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        SelectItem(position);

    }
}

CustomDrawerAdapter:

public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {

Context context;
List<DrawerItem> drawerItemList;
int layoutResID;

public CustomDrawerAdapter(Context context, int layoutResourceID,
        List<DrawerItem> listItems) {
    super(context, layoutResourceID, listItems);
    this.context = context;
    this.drawerItemList = listItems;
    this.layoutResID = layoutResourceID;

}

@SuppressWarnings("static-access")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    DrawerItemHolder drawerHolder;
    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        drawerHolder = new DrawerItemHolder();

        view = inflater.inflate(layoutResID, parent, false);
        drawerHolder.ItemName = (TextView) view
                .findViewById(R.id.drawer_itemName);
        drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
        drawerHolder.pointsText = (TextView) view
                .findViewById(R.id.get_login);
        drawerHolder.divider = view.findViewById(R.id.dividerView);
        drawerHolder.itemLayoutColor = (LinearLayout) view
                .findViewById(R.id.itemLayoutColor);
        view.setTag(drawerHolder);

    } else {
        drawerHolder = (DrawerItemHolder) view.getTag();

    }

    DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
    if (dItem.getPointText() == null) {
        drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
                dItem.getImgResID()));
        drawerHolder.pointsText.setVisibility(view.GONE);
    } else {
        drawerHolder.pointsText.setText(dItem.getPointText());
        }
    drawerHolder.itemLayoutColor.setBackgroundColor(dItem.layoutColorCode);
    drawerHolder.divider.setBackgroundColor(dItem.colorCode);
    drawerHolder.ItemName.setText(dItem.getItemName());

    return view;
}

private static class DrawerItemHolder {
    TextView ItemName;
    ImageView icon;
    TextView pointsText;
    View divider;
    LinearLayout itemLayoutColor;
}
 }
Walking answered 1/6, 2014 at 17:22 Comment(2)
you want to refresh the navigation drawer list as soon as it opens or closed??Iinden
@Iinden yeah this is what i want.Walking
W
11

Finally i have fixed this by using below code. I want to answer to this so that others can get the reference from this. @user3154663. May be this will help you to solve your problem:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    if (drawerOpen) {
        dataList.remove(2);
        p1 = sharedPreferences.getString(Constant.USERS_POINTS, "");
        p2 = sharedPreferences.getString(Constant.LEVEL_ONE_POINTS,
                "");
        p3 = sharedPreferences.getString(Constant.LEVEL_TWO_POINTS,
                "");
        String pointText = " POINTS";
        dataList.add(2, new DrawerItem("MY POINTS ", p1 + pointText,
                Color.BLACK, Color.BLACK));
        dataList.remove(3);
        dataList.add(3, new DrawerItem("REFERRALS POINTS LV1 ", p2
                + pointText, Color.BLACK, Color.BLACK));
        dataList.remove(4);
        dataList.add(4, new DrawerItem("REFERRALS POINTS LV2 ", p3
                + pointText, Color.BLACK, Color.BLACK));


        adapter.notifyDataSetChanged();
        mDrawerList.setAdapter(adapter);
    }
    return super.onPrepareOptionsMenu(menu);
}
Walking answered 5/6, 2014 at 18:24 Comment(2)
this one works, but it slows down the toggling of navigation drawer. It creates a lag / pause, when the ND is in the middle of togglingBelch
if you dont have that drawerList but a menu you need to pass to the isDrawerOpen(..) the id of second childe of main DrawerLayout xml . Like R.id.nav_viewWittenberg
W
2

Calling of notifyDatasetChanged() still applies to ListView adapter, regardless of ListView parent. you have to reset the adapter using notifyDatasetChanged() in when your drawer is open and close and update the listview using setadapter.

Wofford answered 5/6, 2014 at 4:35 Comment(6)
please have a look on my above code and please tell me which place i need to put this.Iinden
onDrawerOpen and onDrawerClosedWofford
in this code you want me to change mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { getActionBar().setTitle(mTitle); invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); } };Iinden
still stuck in same pageIinden
i am very new please tell in my above code what are the changes are required?Iinden
please tell me how to do that. I have added adapter.notifyDatasetChanged(); in my below code: mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { getActionBar().setTitle(mTitle); invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); } };Iinden
P
1

Whenever you want to update the Drawer Layout, update the custom adapter by calling, adapter.notifyDataSetChanged(); and then call mDrawerLayout.invalidate();

Make sure that the getView() method inside your adapter picks up the latest points value.

Pedro answered 5/6, 2014 at 4:9 Comment(2)
please have a look on my updated code and tell me what and where i need to change.Iinden
its comes from the server when i open drawer menu where in also displaying points is getting change but not in drawer.Iinden
K
1

If you want to update the drawer items when user has selected some item in the drawer. Then

  1. you should refresh the adapter for the listview in onItemClick.
  2. call notifydatasetchange() to reflect the changes.

But, if you want if user has selected some item in the list, then on closing of drawer after making selection, you want to update the list, for this case you

  1. Listen for closing of drawer event -

    public void onDrawerClosed(View view) {

    // refresh adapter and call notify data set change.

    }

This link explains the various events for drawer

Kukri answered 5/6, 2014 at 4:21 Comment(0)
F
1

You can call the item from your navigationView object.

    navigationView.getMenu().findItem(R.id.navigation_item_tal).setTitle("elTitulo");
Farman answered 19/1, 2016 at 9:16 Comment(0)
E
0

One way is,

In the adapter of the listview that you displayed in the drawer, within getView(), while creating each view you can register a listener by passing the view to update. When you get the points check whether the view is null(may be it is not in screen), and update the view if not. :)

UPDATE

If you want to refresh your list on open/close you may need something like this

Drawer Listener Which contains the following callbacks

onDrawerClosed(View drawerView)
onDrawerOpened(View drawerView)
onDrawerSlide(View drawerView, float slideOffset)
onDrawerStateChanged(int newState)

On these callbacks you call call notifyDataSetChanged to update listView

Epidaurus answered 5/6, 2014 at 4:0 Comment(6)
could please give me a sample or if i will paste my code will help me on this?Iinden
please have a look on my latest code and tell me what and where i need to change.Iinden
Which view in the layout you want to alter dynaimically ?Epidaurus
i did not understand what you asked?Iinden
Instead of refreshing whole view, you can update the rows in listview. It is the efficient one. So let me know the view that you need to update in each rowEpidaurus
No only these three rows dataList.add(new DrawerItem("MY POINTS ", p1 + pointText, Color.BLACK, Color.BLACK)); dataList.add(new DrawerItem("REFERRALS POINTS LV1 ", p2 + pointText, Color.BLACK, Color.BLACK)); dataList.add(new DrawerItem("REFERRALS POINTS LV2 ", p3 + pointText, Color.BLACK, Color.BLACK));Iinden

© 2022 - 2024 — McMap. All rights reserved.