MultiChoiceModeListener无法正常工作

时间:2016-05-11 16:22:50

标签: java android listview

我正在尝试为MultiChoiceModeListener中的选择多项实施ListView。我目前的进展情况如下。但它并没有奏效。即使长按也没有任何动作。我的代码中遗漏了什么东西或者有什么不对吗?任何帮助将不胜感激。

public class FragmentFavorite extends Fragment {

    ListView lvFavoriteItems;
    Activity mActivity = null;
    Context mContext;
    ArrayList<String> names = new ArrayList<>(Arrays.asList("My name is a", "My name is b",
            "My name is c", "My name is d", "My name is e", "My name is f"));
    ArrayList<String> phone = new ArrayList<>(Arrays.asList("9895653263", "9895653264", "9895653265",
            "9895653266", "9895653267", "9895653267"));

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(mActivity == null) {
            this.mActivity = getActivity();
        }
        this.mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.favorite_list, container, false);
        lvFavoriteItems = (ListView) root.findViewById(R.id.lv_item_list);
        FavoriteAdapter fav = new FavoriteAdapter();
        lvFavoriteItems.setAdapter(fav);
        lvFavoriteItems.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        lvFavoriteItems.setMultiChoiceModeListener(new FavMultiChoiceModeListener());
        return root;
    }

    private class FavoriteAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return names.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            View listItem;
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                listItem = inflater.inflate(R.layout.contact_item_2_line, null);
            } else {
                listItem = convertView;
            }
            TextView tv1 = (TextView) listItem.findViewById(R.id.tv_item_name);
            tv1.setText(names.get(position));
            TextView tv2 = (TextView) listItem.findViewById(R.id.tv_phone);
            tv2.setText(phone.get(position));
            listItem.setId(position);
            return listItem;
        }    }

    private class FavMultiChoiceModeListener implements ListView.MultiChoiceModeListener {

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int i, long l, boolean b) {
            final int checkedCount = lvFavoriteItems.getCheckedItemCount();
            mode.setSubtitle("" + checkedCount + " items selected ");
        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = mActivity.getMenuInflater();
            inflater.inflate(R.menu.selection_menu, menu);
            actionMode.setTitle("Select Items");
            return true;

        }

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

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            Toast.makeText(mActivity, "Clicked " + menuItem.getTitle(),
                    Toast.LENGTH_SHORT).show();
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

由于您要从BaseAdapter扩展,因此可能需要在convertView.setLongClickable(true)方法中调用getView