ListFragment多选择不显示的选择 [英] ListFragment multi-choice does not show selection

查看:210
本文介绍了ListFragment多选择不显示的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的应用程序上下文动作吧。我伸出的ListFragment,我有一个自定义的ArrayAdapter和列表项的XML定义。点击项目工作在列表项细和背景颜色的变化。在那里我遇到的麻烦是选择多个项目时。上下文操作栏来了,我可以告诉我其实是在选择项目时,我,因为我有它记录这些选定的项目触摸它们,但在列表项目的背景高亮颜色不会改变!这似乎像其他一些人使用片段API以及时正在运行到这个问题,他们想出了一个排序的黑客得到它才能正常工作。不过,我想知道如果任何人有一个明确的答案,为什么我的列表不显示选定的项目。

有关良好的措施,这是我的code:

  ListView控件列表= getListView();
    list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    list.setSelector(R.drawable.list_selector);    //配置上下文行动起来吧
    list.setMultiChoiceModeListener(新AbsListView.MultiChoiceModeListener(){
        @覆盖
        公共无效onItemCheckedStateChanged(ActionMode actionMode,INT位置,长的ID,布尔B){
            Log.i(调试,项+位置+改变的状态);
        }        @覆盖
        公共布尔onCreateActionMode(ActionMode actionMode,菜单菜单){
            MenuInflater吹气= actionMode.getMenuInflater();
            inflater.inflate(R.menu.context_menu,菜单);
            返回true;
        }        @覆盖
        在prepareActionMode(ActionMode actionMode,菜单菜单){公共布尔
            返回false;
        }        @覆盖
        公共布尔onActionItemClicked(ActionMode actionMode,菜单项项){
            开关(item.getItemId()){
                案例R.id.delete:
                    Log.i(调试,删除的东西);
                    返回true;
                默认:
                    返回false;
            }
        }        @覆盖
        公共无效onDestroyActionMode(ActionMode actionMode){        }
    });


解决方案

我提供一个背景选择我的自定义列表项视图。

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:exitFadeDuration =@安卓整数/ config_mediumAnimTime>
<项目的android:state_activated =真正的机器人:可绘制=@绘制/ number_bg_ pressed/>
<项目机器人:可绘制=@机器人:彩色/透明/>

我指的是APIdemos查看/列表/ List16例子。

I'm trying to implement the contextual action bar in my app. I'm extending a ListFragment and I have a custom ArrayAdapter and list item xml defined. Clicking on items works fine and the background color changes on the list item. Where I'm running into trouble is when selecting multiple items. The contextual action bar comes up and I can tell I'm actually selecting items when I touch them because I'm having it log which items are selected, but the background highlight color on the list item does not change! It seems like some other people were running into this problem when using the Fragments API as well and they had come up with a sort-of hack to get it to work properly. However, I was wondering if anyone has a definitive answer as to why my list doesn't show selected items.

For good measure, here's my code:

ListView list = getListView();
    list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    list.setSelector(R.drawable.list_selector);

    // configure contextual action bar
    list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean b) {
            Log.i("debug", "item " + position + " changed state");
        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = actionMode.getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            return true;  
        }

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

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.delete:
                    Log.i("debug", "delete stuff");
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {

        }
    });

解决方案

I provide my custom list item view with a background selector.

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:state_activated="true" android:drawable="@drawable/number_bg_pressed" />
<item android:drawable="@android:color/transparent" />

I refer to APIdemos View/List/List16 example.

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

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