How to select an ListView item after long click?

前端 未结 2 2004
忘了有多久
忘了有多久 2021-02-01 07:42

I\'ve got a silly little problem. I\'ve registered a ListFragment both as OnItemClickListener and OnItemLongClickListener of its own

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 08:25

    My solution:(trick)

    final ListView lvMain = (ListView) activity.findViewById(R.id.listTHEMES);
    lvMain.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lvMain.setItemsCanFocus(false);
    
    ArrayAdapter adapter = new ArrayAdapter(activity,
                android.R.layout.simple_list_item_multiple_choice, ArrayTheme);
    lvMain.setAdapter(adapter);
    lvMain.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView arg0, View arg1,
                                           int pos, long id) {
            // TODO Auto-generated method stub
            if (lvMain.isItemChecked(pos)){lvMain.setItemChecked(pos,false);}else{lvMain.setItemChecked(pos,true);}
            Log.v(LOG_TAG,"long clicked pos: " + pos);
            //lvMain.setSelection();
    
            return true;
        }
    });
    lvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View view,
                                    int position, long id) {
            //Log.d(LOG_TAG, "itemClick: position = " + position + ", id = " + id);
            if (lvMain.isItemChecked(position)){lvMain.setItemChecked(position,false);}else{lvMain.setItemChecked(position,true);}
    
        }
    });
    

提交回复
热议问题