是否有一个android标准徽章或方法来显示动作栏通知图标,其中包含来自android Fragment Button Click的计数

时间:2016-05-09 08:23:03

标签: android android-fragments menuitem

enter image description here

我想从片段的Button onClick事件

更新此计数通知

为此我这样做: 我使用自定义按钮来显示此计数獾:

请告诉我您是否知道需要更改的内容 就我而言,我有一个按钮。

我的菜单上的自定义项 - menumain.xml

 <item
     android:id="@+id/badge"
     android:actionLayout="@layout/feed_update_count"
     android:icon="@drawable/shape_notification"
     android:showAsAction="always">
 </item>

自定义形状drawable(背景方块) - shape_notification.xml

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:color="#22000000" android:width="2dp"/>
    <corners android:radius="5dp" />
    <solid android:color="#CC0001"/>
</shape>

我的观点布局 - feed_update_count.xml

 <?xml version="1.0" encoding="utf-8"?>
 <Button xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/notif_count"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:minWidth="32dp"
     android:minHeight="32dp"
      android:background="@drawable/shape_notification"
     android:text="0"
     android:textSize="16sp"
     android:textColor="@android:color/white"
     android:gravity="center"
     android:padding="2dp"
     android:singleLine="true">    
</Button>

我的片段:

    private OnAddProductListener callback;
    public int Count = 0;

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }  
    }

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_product_detail, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    cartView = (View)v.findViewById(R.id.cartView);
    buyNow = (View)v.findViewById(R.id.buyView);

    cartView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(getActivity(),"Added into Cart", Toast.LENGTH_SHORT).show();
      //      myFragment = CartFragment.newInstance("", "");
      //      ActivityUtils.launchFragementWithAnimation(myFragment, getActivity());

            int count = 5;
            callback.OnProductAdd(count);
        }
    });
}

public interface OnAddProductListener {
    public void OnProductAdd(int params);
}

MainActivity:

  static Button cartCount;
  static int mCartCount = 0;

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    View count = menu.findItem(R.id.action_cart).getActionView();
    cartCount = (Button) count.findViewById(R.id.cart_count);
    cartCount.setText(String.valueOf(mCartCount));
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
        case android.R.id.home:
            FragmentManager fm = getFragmentManager();
            if (fm.getBackStackEntryCount() > 1) {
                if (drawerToggle.onOptionsItemSelected(item))
                    return true;
            } else if (fm.getBackStackEntryCount() == 1) {
                if (drawerToggle.onOptionsItemSelected(item))
                    return true;
            } else if (fm.getBackStackEntryCount() == 0) {
                if (drawerToggle.onOptionsItemSelected(item))
                    return true;
            }
            return true;

        case R.id.action_cart:
            myFragment = CartFragment.newInstance("", "");
            ActivityUtils.launchFragementWithAnimation(myFragment, MainActivity.this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

 @Override
public void OnProductAdd(int params) {
    mCartCount = params;
    invalidateOptionsMenu();
}

请告诉我如何在错误的情况下完成这项任务。

1 个答案:

答案 0 :(得分:2)

回拨Mainactivity并使用界面,在MainActivity中扩展界面,点击方法