[英]Android: setOnItemClickListener doesn't work for ListView in soft keyboard
我正在为Android开发自定义软键盘。 我在普通键盘的顶部添加了一个列表。 该列表只是一个普通的ListView。 我需要做的是检测并响应主程序SoftKeyboard.java中的click事件。 ListView在onCreateCandidatesView()方法中初始化。 这是我的代码。
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
}
但是ListView似乎根本不响应setOnItemClickListener()。 有人能在这个问题上帮助我吗? 非常感谢!
更新:
我发现即使我使用普通的ListView,也仍然无法正常工作。 我更新了上面的代码以使用普通的ListView。
尝试添加
android:descendantFocussability=blocksDescendants
最顶层线性布局的android:descendantFocussability=blocksDescendants
。它可能会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.