Can't get the selected item highlight before API level 11


#1

Here is the solution to the second challenge, except using the appcompat support v7 instead of the ABS. I think the support v7 has the same features as the ABS.

In CrimeListFragment.java, I’ve ovriden the method onCreateView()


    private ActionMode mActionMode;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_crime_list, container, false);

        Button button = (Button) v.findViewById(R.id.emptyButton);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Crime c = new Crime();
                CrimeLab.getCrimeLab(getActivity()).addCrime(c);
                Intent i = new Intent(getActivity(), CrimePagerActivity.class);
                i.putExtra(CrimeFragment.EXTRA_CRIME_ID, c.getId());
                startActivityForResult(i, 0);
            }
        });

        if (mSubtitleVisible) {
            ((ActionBarActivity)getActivity()).getSupportActionBar().setSubtitle(R.string.subtitle);
        }

        ListView listView = (ListView) v.findViewById(android.R.id.list);

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                if (mActionMode != null) {
                    return false;
                }

                getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                getListView().setItemChecked(position, true);

                ((ActionBarActivity)getActivity()).startSupportActionMode(new ActionMode.Callback() {
                    @Override
                    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                        MenuInflater inflater = actionMode.getMenuInflater();
                        inflater.inflate(R.menu.crime_list_item_context, menu);
                        mActionMode = actionMode;
                        return true;
                    }

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

                    @Override
                    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                        switch (menuItem.getItemId()) {
                            case R.id.menu_item_delete_crime:
                                CrimeAdapter adapter = (CrimeAdapter) getListAdapter();
                                CrimeLab crimeLab = CrimeLab.getCrimeLab(getActivity());
                                for (int i = adapter.getCount() - 1; i >= 0; i--) {
                                    if (getListView().isItemChecked(i)) {
                                        crimeLab.deleteCrime(adapter.getItem(i));
                                    }
                                }
                                actionMode.finish();
                                adapter.notifyDataSetChanged();
                                return true;
                            default:
                                return false;
                        }
                    }

                    @Override
                    public void onDestroyActionMode(ActionMode actionMode) {
                        getListView().clearChoices();

                        for (int i = 0; i < getListView().getChildCount(); i++) {
                            getListView().getChildAt(i).getBackground().setState(new int[] {0});
                        }

                        getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
                        mActionMode = null;
                    }
                });

                return true;
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mActionMode != null) {
                    if (getListView().isItemChecked(position)) {
                        getListView().setItemChecked(position, false);
                    } else {
                        getListView().setItemChecked(position, true);
                    }
                }
            }
        });

        return v;
    }

background_activated.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_activated="true"
        android:drawable="@android:color/darker_gray"/>
</selector>

Because android:state_activated is only used in API level 11 and higher, I can’t get the same highlight effect in level 8.

Can someone help me?