上下文操作栏未显示选择

时间:2015-03-16 05:35:41

标签: android android-listview contextual-action-bar

我正在尝试实施上下文操作栏(CAB),但每当我长时间点击某个项目时,它都没有将项目显示为已选中(突出显示),因此我无法选择多个项目进行批处理删除。下面是尝试使用CAB的片段。

package com.garciaericn.memoryvault.main;

import android.app.Fragment;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.garciaericn.memoryvault.R;
import com.garciaericn.memoryvault.data.Memory;
import com.garciaericn.memoryvault.data.MemoryAdapter;
import com.parse.DeleteCallback;
import com.parse.FindCallback;
import com.parse.ParseException;

import java.util.List;

public class MemoriesFragment extends Fragment implements AbsListView.MultiChoiceModeListener, AdapterView.OnItemClickListener {

    ListView memoriesListView;
    MemoryAdapter memoryAdapter;

    public MemoriesFragment() {
        // Mandatory empty constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);
        memoryAdapter = new MemoryAdapter(getActivity());
    }

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

        memoriesListView = (ListView) view.findViewById(R.id.listView);
        memoriesListView.setAdapter(memoryAdapter);
        memoriesListView.setOnItemClickListener(this);
        memoriesListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        memoriesListView.setMultiChoiceModeListener(this);

        return view;
    }

    private void refreshMemories() {
        Memory.getQuery().findInBackground(new FindCallback<Memory>() {
            @Override
            public void done(List<Memory> memoryList, ParseException e) {
                memoryAdapter.loadObjects();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        refreshMemories();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_sync: {
                refreshMemories();
                Toast.makeText(getActivity(), "Refreshed from fragment", Toast.LENGTH_SHORT).show();
                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
        // Here you can do something when items are selected/de-selected,
        // such as update the title in the CAB
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context_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.action_delete:
                Toast.makeText(getActivity(), "Delete!", Toast.LENGTH_SHORT).show();
                // TODO: Delete item
                mode.finish(); // Action picked, so close the CAB
                return true;
        }
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // Here you can make any necessary updates to the activity when
        // the CAB is removed. By default, selected items are deselected/unchecked.
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Memory memory = memoryAdapter.getItem(position);
        /*// Un-comment to delete item
        memory.deleteInBackground(new DeleteCallback() {
            @Override
            public void done(ParseException e) {
                refreshMemories();
            }
        });
        */
        Toast.makeText(getActivity(), memory.toString() + " was tapped", Toast.LENGTH_SHORT).show();
    }
}

2 个答案:

答案 0 :(得分:0)

-Hey纠正我,如果我错了,我在你的代码中发现你正在实现 OnItemClickListener ,而是使用 AdapterView.OnItemLongClickListener ,因为你希望你对长期采取行动点击项。

答案 1 :(得分:0)

如果要实现自定义适配器,则需要为listview项实现Checkable布局。 请检查此Github page

解释了使用演示适配器实现Checkable布局。

在自定义列表项布局中使用,例如:

<your.package.CheckableLayout ... />