Android ListView child View setEnabled() and setClickable() do nothing

前端 未结 4 885
有刺的猬
有刺的猬 2020-11-30 13:12

I\'m doing some AsyncTask work after user clicks an item in my ListView. I\'d like to disable the item so it can\'t be clicked twice. I\'ve simplif

4条回答
  •  有刺的猬
    2020-11-30 13:23

    There are multiple reasons why your approach will not work.

    1) onItemClick is only called due to keyboard events. Specifically KeyKevent.KEYCODE_ENTER. It is not called via any other code path. So, handling that even is only useful if you are attempting to provide keyboard/trackball support.

    Android source code for AbsListView relevant methods:

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
            if (!isEnabled()) {
                return true;
            }
            if (isClickable() && isPressed() &&
                    mSelectedPosition >= 0 && mAdapter != null &&
                    mSelectedPosition < mAdapter.getCount()) {
    
                final View view = getChildAt(mSelectedPosition - mFirstPosition);
                if (view != null) {
                    performItemClick(view, mSelectedPosition, mSelectedRowId);
                    view.setPressed(false);
                }
                setPressed(false);
                return true;
            }
            break;
        }
        return super.onKeyUp(keyCode, event);
    }
    
    public boolean performItemClick(View view, int position, long id) {
        if (mOnItemClickListener != null) {
            playSoundEffect(SoundEffectConstants.CLICK);
            mOnItemClickListener.onItemClick(this, view, position, id);
            return true;
        }
    
        return false;
    }
    

    2) You are setting the clickable information directly on the view. The views displayed via any AdapterView are ethereal. They are created at the request of the AdapterView and only exist as long as the AdapterView needs them. You should not set any data on them that you want to keep. You can call setEnabled and setClickable on them for immediate effect but if you want that information to persist you need to store it somewhere the Adapter has access to so it can be recreated when the AdapterView recreates the View for that position.

    3) You need to handle the onClick event for the actual View being clicked. Where you handle this is up to you. The best place is probably your Adapter which then may or may not pass it up to your Activity depending on what your design requirements are. That is where you need to handle your touch events.

    See this code for a simple Activity:

    public class PhoneTesting extends Activity {
        private static final String TAG = "PhoneTesting";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Log.d(TAG, "onCreate()");
    
            List strings = new ArrayList();
            for(int i = 0 ; i < 20 ; i++) {
                strings.add(Integer.toString(i));
            }
    
            ListView list = (ListView) this.findViewById(R.id.list);
    
            list.setAdapter(new SimpleAdapter(this, 0, 0, strings));
            list.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    Log.d(TAG, "onItemClick: " + id);
                }
            });
    
        }
    
        class SimpleAdapter extends ArrayAdapter implements OnClickListener {
            SimpleAdapter(Context context, int resource, int textViewResourceId, List objects) {
                super(context, resource, textViewResourceId, objects);
            }
    
            SimpleAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
                super(context, resource, textViewResourceId, objects);
            }
    
            SimpleAdapter(Context context, int resource, int textViewResourceId) {
                super(context, resource, textViewResourceId);
            }
    
            SimpleAdapter(Context context, int textViewResourceId, List objects) {
                super(context, textViewResourceId, objects);
            }
    
            SimpleAdapter(Context context, int textViewResourceId, String[] objects) {
                super(context, textViewResourceId, objects);
            }
    
            SimpleAdapter(Context context, int textViewResourceId) {
                super(context, textViewResourceId);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView b = position % 2 == 0 ? new Button(this.getContext()) : new TextView(this.getContext());
                b.setText(this.getItem(position));
    
                b.setOnClickListener(this);
    
                return b;
            }
    
            @Override
            public void onClick(View v) {
                TextView t = (TextView) v;
                Log.d(TAG, "onClick: " + t.getText());
            }
    
            @Override
            public boolean isEnabled(int position) {
                return position % 2 == 0 ? false : true;
            }
    
        }
    }
    

    If you execute this code and click on any of the Views in the ListView you will notice in the logcat output that only onClick is being called. onItemClick is never called.

    Also note that isEnabled in the adapter does not seem to effect if the View is clickable or not. I am not sure how to interpret that. What that means though is that if you want to control that property of the View the Adapter needs set that when the View is created and to somehow maintain that information.

提交回复
热议问题