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

浪尽此生 提交于 2020-01-22 13:44:11

问题


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;
}
 }

回答1:


Finally i have fixed this by using below code.This is not the proper solution. but still 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);
}



回答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.




回答3:


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.




回答4:


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




回答5:


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




回答6:


You can call the item from your navigationView object.

    navigationView.getMenu().findItem(R.id.navigation_item_tal).setTitle("elTitulo");


来源:https://stackoverflow.com/questions/23982472/how-to-refresh-list-of-the-navigation-drawer-as-soon-as-its-open-closed-in-andro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!