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

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

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 simplified the click listener to contain only this method, but it still doesn't do anything for me, the view looks the same and it lets itself be happily clicked again, much to my annoyance.

public void onItemClick(AdapterView> parent, View clickedView,   int position, long id) {   item = (Episode) parent.getItemAtPosition(position);   clickedView.setClickable(false);   clickedView.setEnabled(false);   clickedView.invalidate(); } 

My View for each row is a custom LinearLayout with two TextViews.

回答1:

So, you may be using a custom adapter too. If you do, override these methods:

public boolean areAllItemsEnabled() {     return false; }  public boolean isEnabled(int position) {     // return false if position == position you want to disable } 

Then, when you receive a click tell the adapter what was the last item clicked and return false on isEnabled for that position. For instance, you can have a method like this in your adapter:

private int mLastClicked; public void setLastClicked(int lastClicked){     mLastClicked = lastClicked; } 


回答2:

If you want disabling item click in list view use clickedView.setClickable(true);



回答3:

Your problem is not completely clear. I am interpreting your question as that you are expecting onItemClick() to not be called based upon your setEnabled() and setClickable() calls.

I'm not surprised that doesn't work, as onItemClick() is something ListView does, not the child view. Try overriding areAllItemsEnabled() and isEnabled() in your ListAdapter instead.



回答4:

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 

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  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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!