Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

java - ListView OnItemClickListener Not Responding?

I've looked everywhere for a solution to this, but I can't figure out how to implement it. My OnItemClickListener was disabled somehow on my ListView rows, because I have an ImageButton in the row layout, which takes over the focus. There have been numerous questions I've found, but none of them have gotten me anywhere.

I've checked this question, but I couldn't really make heads or tails of it. I just need a way to get the rows clickable so that I can detect when a row is pressed. Long press and focus work fine.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of an OnItemClickListener, add an OnClickListener to each of your views returned from your adapter. You'll need to use setItemsCanFocus setting up your list:

ListView list = (ListView) findViewById(R.id.myList);
list.setAdapter(new DoubleClickAdapter(this));
list.setItemsCanFocus(true);

and then in your Adapter's getView, this will yield a clickable row. The button is assumed to be in the inflated xml.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = View.inflate(context, R.layout.cell, null);
    view.setClickable(true);
    view.setFocusable(true);
    view.setBackgroundResource(android.R.drawable.menuitem_background);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(context).setTitle("touched").show();
        }

    });
    return view;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...