简体   繁体   中英

Android: setOnItemClickListener doesn't work for ListView in soft keyboard

I am developing a custom soft keyboard for Android. I have added a list on the top of the normal keyboard. The list is just a normal ListView. What I need to do is to detect and respond to the click event inside the main program, which is SoftKeyboard.java . The ListView is initialised inside onCreateCandidatesView() method. Here's my code.

suggestion_list.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/candidate_background"
    android:divider="#BBCFCFCF"
    android:dividerHeight="1dp"
    android:drawSelectorOnTop="false" />

SoftKeyboard.java

public class SoftKeyboard extends InputMethodService 
        implements KeyboardView.OnKeyboardActionListener {

    //dummy data for the list
    private static final String[] items = {"1","2","3"};
    //some other variables

    @Override 
    public View onCreateCandidatesView() {     
        LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ListView ls = (ListView)mInflater.inflate(R.layout.suggestion_list,null);

        ls.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items));

        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.v("index", "Clicked");
            }
        });

        return ls;
    }

    //some other methods
}

But The ListView doesn't seem to respond to the setOnItemClickListener() at all. Could anyone kindly help me on this issue? Thanks a lot!


Update:

I found that even if I use a normal ListView, it still doesn't work. I updated my code above for using a normal ListView.

Try adding

android:descendantFocussability=blocksDescendants to the top most linear layout.It might work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM