android setenabled 子控件,Android ListView child View setEnabled() and setClickable() do nothing

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值