项目选择后,Android语境操作栏不显示 [英] Android Contextual Action Bar not appearing after item selection

查看:276
本文介绍了项目选择后,Android语境操作栏不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我试图表明上下文操作栏(CAB)的列表视图的项删除。问题是,每当一个项目是pssed长$ P $,该项目被选择,但是,CAB不会出现。我已经有这么多的教程验证,想不通我错过了什么。任何帮助将是AP preciated。

适配器类别

 公共类DuasListAdapter扩展ArrayAdapter<串GT; {    //类变量
    私人最终上下文的背景下; //保存上下文
    私人最终名单<串GT;杜阿斯; //保存店名单
    LayoutInflater气筒; //
    私人SparseBooleanArray selectedItemsIds;    公共DuasListAdapter(上下文CTX,INT RESOURCEID,列表与LT;弦乐> duasList){        超(CTX,RESOURCEID,duasList);
        selectedItemsIds =新SparseBooleanArray();
        上下文= CTX; //保存上下文
        杜阿斯= duasList; //保存店名单
        吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //保存吹气布局    } * /
    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){    最后ViewHolder持有人;    如果(convertView == NULL){
        持有人=新ViewHolder();
        convertView = inflater.inflate(R.layout.adapter_list_duas,NULL);
        convertView.setTag(保持器);
        holder.duaIndex =位置;
        holder.background =(的LinearLayout)convertView.findViewById(R.id.ll_duas_list);
        holder.iv_duatype =(ImageView的)convertView.findViewById(R.id.iv_duatype);
        holder.tv_dua_arabic =(TextView中)convertView.findViewById(R.id.tv_dua_arabic);
        holder.tv_dua_no =(TextView中)convertView.findViewById(R.id.dua_no);
    }    其他{
        支架=(ViewHolder)convertView.getTag();
    }    返回convertView;
    }
静态类ViewHolder {         INT duaIndex;
        背景的LinearLayout; //背景中显示已读和未读邮件的颜色
        ImageView的iv_duatype;
        TextView的tv_dua_arabic; //消息的标题
        TextView的tv_dua_ref; //消息由和消息上创建
        TextView的tv_dua_no;        / **
         * @description构造保存的门店意见情况和清单
         * @参数v到参考视图
         * @返回
         * /    }    公共无效删除(列表<串GT;对象){        duas.remove(对象);
        notifyDataSetChanged();
    }公开名单<串GT; getDuas(){    返回杜阿斯;
    }公共无效toggleSelection(INT位置){
    选择查看(位置,selectedItemsIds.get(位置)!);
}公共无效removeSelection(){
    selectedItemsIds =新SparseBooleanArray();
    notifyDataSetChanged();
}公共无效选择查看(INT位置,布尔值){
    如果(值)
        selectedItemsIds.put(位置,值);
    其他
        selectedItemsIds.delete(位置);
    notifyDataSetChanged();
}公众诠释getSelectedCount(){
    返回selectedItemsIds.size();
}公共SparseBooleanArray getSelectedIds(){
    返回selectedItemsIds;
}}

片段级

 上下文的背景下;
    的LinearLayout ll_back_duas_list_header;
    TextView的ll_back_duas_list_header_title;
    ListView的lvDuaas;
公共静态DuasListAdapter duasAdapter;
ActionMode mAction;
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        上下文= inflater.getContext();
        查看查看= inflater.inflate(R.layout.fragment_list_duas,集装箱,FALSE);
        ll_back_duas_list_header =(的LinearLayout)view.findViewById(R.id.ll_back_duas_list_header);
        ll_back_duas_list_header_title =(TextView中)view.findViewById(R.id.ll_back_duas_list_header_title);
        lvDuaas =(ListView控件)view.findViewById(R.id.lv_duas);
        setHasOptionsMenu(真);
        返回视图。
    }    @覆盖
    公共无效onActivityCreated(捆绑savedInstanceState){
        字符串的诗句=新的共享preferencesSupplication()读取(SingletonClass.keyListOfVerses,A1)。
        串[] versesList = verses.split(,);
        最终的ArrayList<串GT;杜阿斯=新的ArrayList<串GT;();
        的for(int i = 0; I< versesList.length;我++){
            如果(versesList [I]。长度()大于0)
                duas.add(versesList [I]);
        }        duasAdapter =新DuasListAdapter(背景下,R.layout.adapter_list_duas,杜阿斯);
        lvDuaas.setAdapter(duasAdapter);
        lvDuaas.setOnItemLongClickListener(新OnItemLongClickListener(){                @覆盖
                公共布尔onItemLongClick(适配器视图<>母公司,
                        查看查看,INT位置,长的id){
                    // TODO自动生成方法存根                    lvDuaas.setChoiceMode(lvDuaas.CHOICE_MODE_MULTIPLE);
                    lvDuaas.setItemChecked(位置,真正的);                mAction = getActivity()startActionMode(modeCallBack)。
                view.setSelected(真);
                    返回true;
                }
            });
super.onActivityCreated(savedInstanceState);
    }
ActionMode.Callback modeCallBack =新ActionMode.Callback(){        @覆盖
        公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
            // TODO自动生成方法存根
             。mode.getMenuInflater()膨胀(R.menu.activity_main,菜单);
            返回false;
        }        @覆盖
        在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
            // TODO自动生成方法存根
            返回false;
        }        @覆盖
        公共布尔onActionItemClicked(ActionMode模式,
                菜单项项){
            // TODO自动生成方法存根
             开关(item.getItemId()){
                案例R.id.delete:
                    //调用从ListViewAdapter类getSelectedIds方法
                    SparseBooleanArray选择= duasAdapter
                            .getSelectedIds();
                    //捕获所有选定的IDS使用循环
                    的for(int i =(selected.size() - 1); I> = 0;我 - ){
                        如果(selected.valueAt(ⅰ)){
                            字符串将selectedItem = duasAdapter.getItem(selected.keyAt(I));
                            //删除下面的ID选择的项目
                            duasAdapter.remove(将selectedItem);
                        }
                    }
                    //关闭CAB
                    mode.finish();
                    返回true;
                默认:
                    返回false;
                }        }        @覆盖
        公共无效onDestroyActionMode(ActionMode模式){
            // TODO自动生成方法存根
            duasAdapter.removeSelection();
        }
    };
}

XML文件

 <?XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 > <项目
        机器人:ID =@ + ID /删除
        机器人:标题=@字符串/删除
       />&所述; /菜单>


解决方案

从看你的code,我可以看到在 onCreateActionMode 的回调函数返回false。


  

onCreateActionMode应该返回true,如果操作模式应该被创建,false,如果进入此模式应该被中止。


所以,当在列表项和事件的用户点击长触发它达到的 onCreateActionMode 并返回false。告诉它中止。因此,没有出现CAB菜单。试着将其更改为真,也增加了一些日志,看它是否能做出入回调函数。

这将低于变化:

  @覆盖
    公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
        // TODO自动生成方法存根
         Log.d(测试,在onCreateActionMode);
         。mode.getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true; // code在这里更新
    }

Android的链接引用:<一href=\"http://developer.android.com/reference/android/view/ActionMode.Callback.html#onCreateActionMode(android.view.ActionMode\" rel=\"nofollow\">http://developer.android.com/reference/android/view/ActionMode.Callback.html#onCreateActionMode(android.view.ActionMode, android.view.Menu)

In my android application, I am trying to show contextual action bar (CAB) in listview for item delete. The problem is that whenever an item is pressed for long, the item gets selected, but CAB doesn't appear. I have verified with so many tutorials, can't figure out what am I missing. Any help would be appreciated.

Adapter Class

public class DuasListAdapter extends ArrayAdapter<String>{

    // class variables
    private final Context context;// to save context
    private final List<String> duas;// to save list of stores
    LayoutInflater inflater;// 
    private SparseBooleanArray selectedItemsIds;

    public DuasListAdapter(Context ctx,int resourceId, List<String> duasList) {

        super(ctx, resourceId, duasList);
        selectedItemsIds = new SparseBooleanArray();
        context = ctx;// save context
        duas = duasList;// save list of stores
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// save inflater layout

    }

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

    final   ViewHolder holder;

    if(convertView==null){


        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.adapter_list_duas, null);
        convertView.setTag(holder);
        holder.duaIndex = position;
        holder.background = (LinearLayout) convertView.findViewById(R.id.ll_duas_list);
        holder.iv_duatype = (ImageView) convertView.findViewById(R.id.iv_duatype);
        holder.tv_dua_arabic = (TextView) convertView.findViewById(R.id.tv_dua_arabic);
        holder.tv_dua_no = (TextView) convertView.findViewById(R.id.dua_no);


    }

    else {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;




    }


static class ViewHolder {

         int duaIndex;
        LinearLayout background;// background to display color for read and unread messages
        ImageView iv_duatype;
        TextView tv_dua_arabic;// title of message
        TextView tv_dua_ref;// message by and message created on
        TextView tv_dua_no;

        /**
         * @description constructor to save context and list of stores views
         * @param v to refer the view
         * @return
         */

    }



    public void remove(List<String> object){

        duas.remove(object);
        notifyDataSetChanged();
    }

public List<String> getDuas(){

    return duas;
    }

public void toggleSelection(int position) {
    selectView(position, !selectedItemsIds.get(position));
}

public void removeSelection() {
    selectedItemsIds = new SparseBooleanArray();
    notifyDataSetChanged();
}

public void selectView(int position, boolean value) {
    if (value)
        selectedItemsIds.put(position, value);
    else
        selectedItemsIds.delete(position);
    notifyDataSetChanged();
}

public int getSelectedCount() {
    return selectedItemsIds.size();
}

public SparseBooleanArray getSelectedIds() {
    return selectedItemsIds;
}



}

Fragment Class

    Context context;
    LinearLayout ll_back_duas_list_header;
    TextView ll_back_duas_list_header_title;
    ListView lvDuaas;
public static   DuasListAdapter duasAdapter;
ActionMode mAction;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = inflater.getContext();
        View view = inflater.inflate(R.layout.fragment_list_duas, container, false);
        ll_back_duas_list_header = (LinearLayout) view.findViewById(R.id.ll_back_duas_list_header);
        ll_back_duas_list_header_title = (TextView) view.findViewById(R.id.ll_back_duas_list_header_title);
        lvDuaas = (ListView) view.findViewById(R.id.lv_duas);
        setHasOptionsMenu(true);
        return view;
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {


        String verses = new SharedPreferencesSupplication().read(SingletonClass.keyListOfVerses, "a1");
        String[] versesList = verses.split(",");
        final ArrayList<String> duas = new ArrayList<String>();
        for (int i = 0; i < versesList.length; i++) {
            if (versesList[i].length() > 0)
                duas.add(versesList[i]);
        }

        duasAdapter = new DuasListAdapter(context,R.layout.adapter_list_duas,duas);
        lvDuaas.setAdapter(duasAdapter);


        lvDuaas.setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> parent,
                        View view, int position, long id) {
                    // TODO Auto-generated method stub

                    lvDuaas.setChoiceMode(lvDuaas.CHOICE_MODE_MULTIPLE);
                    lvDuaas.setItemChecked(position, true);

                mAction = getActivity().startActionMode(modeCallBack);
                view.setSelected(true);
                    return true;
                }
            });
super.onActivityCreated(savedInstanceState);
    }


ActionMode.Callback modeCallBack = new ActionMode.Callback() {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
             mode.getMenuInflater().inflate(R.menu.activity_main, menu);
            return false;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode,
                MenuItem item) {
            // TODO Auto-generated method stub
             switch (item.getItemId()) {
                case R.id.delete:
                    // Calls getSelectedIds method from ListViewAdapter Class
                    SparseBooleanArray selected = duasAdapter
                            .getSelectedIds();
                    // Captures all selected ids with a loop
                    for (int i = (selected.size() - 1); i >= 0; i--) {
                        if (selected.valueAt(i)) {
                            String selecteditem = duasAdapter.getItem(selected.keyAt(i));
                            // Remove selected items following the ids
                            duasAdapter.remove(selecteditem);
                        }
                    }
                    // Close CAB
                    mode.finish();
                    return true;
                default:
                    return false;
                }

        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub
            duasAdapter.removeSelection();
        }




    };


}

XML file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
 >

 <item
        android:id="@+id/delete"
        android:title="@string/delete"
       />

</menu>

解决方案

From looking at your code I can see the onCreateActionMode callback function returns false.

onCreateActionMode should return true if the action mode should be created, false if entering this mode should be aborted.

So when the user long clicks on the ListItem and the event is triggered it reaches onCreateActionMode and returns false. Telling it to abort. Hence no CAB menu appears. Try changing it to true and also adding in some logs to see if it ever makes it into the callback function.

This would be the change below:

  @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
         Log.d("Test","In onCreateActionMode");
         mode.getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;//code updated here
    }

Android Link to reference : http://developer.android.com/reference/android/view/ActionMode.Callback.html#onCreateActionMode(android.view.ActionMode, android.view.Menu)

这篇关于项目选择后,Android语境操作栏不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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