在任何新更新上通知导航抽屉的列表项

时间:2015-02-28 01:27:08

标签: android android-fragments navigation-drawer

我想在导航抽屉的一个列表项上使用徽章,并在我提出请求时更新它。我已经完成了xml部分以及制作抽屉。但问题是我想改变子片段的值,该子片段位于父片段的旁边,而父片段与导航抽屉一起位于主容器活动中

Image

NavMenuListAdapter

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.nav_drawer_list_adapter, null);
        holder.navImags = (ImageView) convertView.findViewById(R.id.nav_list_img);
        holder.navText = (TextView) convertView.findViewById(R.id.nav_list_txt);
        holder.numOfOffers = (TextView) convertView.findViewById(R.id.badge);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.navImags.setImageResource(items.get(position).getIcon());
    holder.navText.setText(items.get(position).getTitle());

    if (items.get(position).getCounterVisibility()) {
        holder.numOfOffers.setVisibility(TextView.VISIBLE);
        holder.numOfOffers.setText(items.get(position).getCount());
    } else {

        holder.numOfOffers.setVisibility(TextView.INVISIBLE);
    }
    return convertView;
}

NavigationDrawerFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);

    navMenuList = (ListView) view.findViewById(R.id.nav_menu_list);
    navSubMenuList = (ListView) view.findViewById(R.id.nav_sub_menu_list);
    navMenuItems = new ArrayList<>();
    navMenuItems.add(new NavDrawerItem("Offer In Range", R.drawable.ic_location_normal, true, "0"));
    navMenuItems.add(new NavDrawerItem("All Offers", R.drawable.ic_all_offers_normal));
    navMenuItems.add(new NavDrawerItem("Favorites", R.drawable.ic_favorite_normal));
    navMenuItems.add(new NavDrawerItem("Registeration", R.drawable.ic_register_normal));
    navMenuAdapter = new NavMenuAdapter(getActivity(), navMenuItems);

    navSubMenuItems = new ArrayList<>();
    navSubMenuItems.add(new NavDrawerItem("Settings", R.drawable.ic_action_settings));
    navSubMenuItems.add(new NavDrawerItem("About", R.drawable.ic_action_info));
    navSubMenuItems.add(new NavDrawerItem("Help", R.drawable.ic_action_help));
    navSubMenuAdapter = new NavMenuAdapter(getActivity(), navSubMenuItems);

    return view;
}

MapContainerFragment

  @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_map_container, container, false);
    tabs = (SlidingTabLayout) view.findViewById(R.id.tabs);
    viewPager = (ViewPager) view.findViewById(R.id.viewpager);

    allFragments.add(new AppMapFragment());
    allFragments.add(new ListFragment());
    return view;
}

AppMapFragment (我想要更新该徽章)

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_map,container,false);
    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    initilizeMap();

}

和MainContainerActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.app_bar_main);
    setSupportActionBar(toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mFragmentContainerView = findViewById(R.id.navigation_drawer);
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            supportInvalidateOptionsMenu();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            supportInvalidateOptionsMenu();

        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
    drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.container,
                    new MapContainerFragment()).commit();
}

我希望我粘贴所有代码,这些代码可以帮助你们理解我的问题,如果没有,请告诉我。 感谢。

0 个答案:

没有答案