对片段使用上下文操作栏 [英] Using Contextual action bar with fragments

查看:82
本文介绍了对片段使用上下文操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事android项目,并使用了片段和ListViews/ListFragments.我有用于标准活动(如ListActivity)的上下文操作栏.

I am currently working on android project and making use of fragments and ListViews/ListFragments. I have contextual action bars working on standard activities such as a ListActivity.

现在,我正在尝试做同样的事情,但片段布局.我有一个MainActivity,它扩展了Activity,该Activity使包含两个片段A和B的布局的XML膨胀.

Now I am trying to do the same sort of thing but on a fragment layout. I have a MainActivity which extends Activity which inflates the XML for the layout that contains the 2 fragments, fragment A and fragment B.

Fragment A扩展了ListFragment,并包含一个ListView,该列表视图是从SQLite数据库中的数据填充的.当我有一个上下文操作栏在标准ListActivity上工作时,我有一个扩展ListView.MultiChoiceModeListener的类,但是对于ListFragment类或标准活动不可用,所以我将如何实现它.

Fragment A extends ListFragment and contains a ListView which is populated from data within an SQLite Database. When I have got a contextual action bar working on a standard ListActivity I have a class that Extends ListView.MultiChoiceModeListener but this isn't available for a ListFragment class or a standard activity so how would I go about implementing this.

我要实现的基本功能是,当某人长按FragmentA中扩展ListFragment的ListView中的项目时,操作栏会随上下文发生变化,然后用户可以从ListView中选择多个项目.

The basic thing I want to achieve is when someone long presses the item within a ListView within FragmentA which extends ListFragment, the action bar contextually changes and the user can then select multiple items from within the ListView.

感谢您可以提供的任何帮助.

Thanks for any help you can provide.

推荐答案

当我在标准中使用上下文操作栏时 ListActivity我有一个扩展的类 ListView.MultiChoiceModeListener,但这不适用于 ListFragment类或标准活动,所以我应该怎么做 实施.

When I have got a contextual action bar working on a standard ListActivity I have a class that Extends ListView.MultiChoiceModeListener but this isn't available for a ListFragment class or a standard activity so how would I go about implementing this.

我看不到MultiChoiceModeListener不可用(也许我不明白您要怎么做).根据您的评论,我假设您使用兼容性包中的片段. 下面是一个带有两个静态片段的FragmentActivity的示例,每个片段都使用其自己的菜单选项触发上下文操作栏.

I don't see how MultiChoiceModeListener isn't available (maybe I didn't understand what you try to do). From your comment I assumed you use the fragments from the compatibility package. Below is an example with a FragmentActivity with two static fragments and each of those fragments triggers the contextual action bar with their own menus options.

FragmentActivity非常简单,它仅包含以下两个片段:

The FragmentActivity is very simple, it just holds the two fragments below:

// the list fragment
public class ListCABFragment extends ListFragment {

    private String[] mCountries = { "Romania", "Germany", "England", "USA",
            "Japan", "France" };
    private static final boolean POST_HONEYCOMB = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (POST_HONEYCOMB) {
            // make sure we are on a version above Honeycomb otherwise will
            // access things that aren't available
            postHoneycombCAB();
        } else {
            // probably do nothing and implement the normal context menu?!?
        }
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, mCountries));
    }

    @SuppressLint({ "NewApi", "NewApi" })
    private void postHoneycombCAB() {
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                ((ListView) parent).setItemChecked(position,
                        ((ListView) parent).isItemChecked(position));
                return false;
            }
        });
        getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {

            private int nr = 0;

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                getActivity().getMenuInflater().inflate(R.menu.listcab_menu,
                        menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch (item.getItemId()) {
                case R.id.item1:
                    Toast.makeText(getActivity(), "Option1 clicked",
                            Toast.LENGTH_SHORT).show();
                    break;
                case R.id.item2:
                    Toast.makeText(getActivity(), "Option2 clicked",
                            Toast.LENGTH_SHORT).show();
                    break;

                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                nr = 0;
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode,
                    int position, long id, boolean checked) {
                if (checked) {
                    nr++;
                } else {
                    nr--;
                }
                mode.setTitle(nr + " rows selected!");
            }
        });
    }
}

Fragment的另一个片段,该片段的布局由RadioGroup组成,该布局在选择RadioButton时触发CAB:

and the other fragment for a Fragment which has a layout composed from a RadioGroup which triggers the CAB when a RadioButton is selected:

public class SimpleCABFragment extends Fragment implements Callback {

    private static final boolean POST_HONEYCOMB = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RadioGroup rg = (RadioGroup) getView().findViewById(R.id.radioGroup1);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (POST_HONEYCOMB) {
                    // this could be improved so we don't need to create the
                    // option
                    // menu if it is already available
                    getActivity().startActionMode(SimpleCABFragment.this);
                } else {
                    // something else
                }
            }
        });
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        if (item.getItemId() == R.id.itemradio) {
            Toast.makeText(getActivity(), "CAB for Radiogroup!",
                    Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        getActivity().getMenuInflater().inflate(R.menu.simplecab_menu, menu);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }
}

查看这是否是您要查找的内容(您可以找到完整的示例,包括布局和菜单文件

See if this is what you're looking for (you can find a full sample including the layouts and menus files in my github project).

这篇关于对片段使用上下文操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆