如何从其他活动或类更新tabhost android中的徽章

如何从其他活动或类更新tabhost android中的徽章,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我正在使用android badgeviewer在选项卡中显示通知数。我有下面的代码 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_homepage_x,c

我正在使用android badgeviewer在选项卡中显示通知数。我有下面的代码

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_homepage_x,container, false);


    mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("HOME").setIndicator(getTabIndicatorhome(mTabHost.getContext(), "HOME")),
            homepage.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("NOTIFICATIONS").setIndicator(getTabIndicator(mTabHost.getContext(), "NOTIFICATIONS")),
            notificationfragment.class, null);
    return rootView;

}

private View getTabIndicator(Context context, String title) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
    TextView tv = (TextView) view.findViewById(R.id.tab_text);
    tv.setText(title);
    TextView tv_counter = (TextView) view.findViewById(R.id.tab_counter);
    BadgeView badge = new BadgeView(getActivity(), tv_counter);
    badge.setText("1");
    badge.show();
    return view;
}
private View getTabIndicatorhome(Context context, String title) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_layout_home, null);
    TextView tv = (TextView) view.findViewById(R.id.tab_text_home);
    tv.setText(title);
    return view;
}

现在我需要从其他活动或课程中更新我的徽章。我该怎么做?

我认为您可以使用此处记录的活动/片段通信:

例如:

public class SomeFragment extends Fragment {
    OnDataChanged mCallback;

    // Container Activity must implement this interface
    public interface OnDataChangedListener {
        public void onDataChanged(int numberOfThingsChanged);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnDataChangedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnDataChangedListener");
        }
    }

    // When your data actually changes call back to the tab fragment
    private void somethingChanged() {
       // No need to check if we have a callback, onAttach already did that
       // notif change
       mCallback(27);  // 27 things changed.
    }
}
然后在主选项卡片段中实现该接口(问题中的类):


Android示例在一个片段中定义了接口。如果您有多个选项卡,所有选项卡都将获得徽章,我将在其自己的类/文件中定义接口。

我认为您可以使用此处记录的活动/片段通信:

例如:

public class SomeFragment extends Fragment {
    OnDataChanged mCallback;

    // Container Activity must implement this interface
    public interface OnDataChangedListener {
        public void onDataChanged(int numberOfThingsChanged);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnDataChangedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnDataChangedListener");
        }
    }

    // When your data actually changes call back to the tab fragment
    private void somethingChanged() {
       // No need to check if we have a callback, onAttach already did that
       // notif change
       mCallback(27);  // 27 things changed.
    }
}
然后在主选项卡片段中实现该接口(问题中的类):

Android示例在一个片段中定义了接口。如果你有多个标签都会得到徽章,我会在自己的类/文件中定义接口