I have really searched a lot on google, stack overflow , and found this : OnItemCLickListener not working in listview ANDROID. But seems sunshine's answer not works for my case. Other answers are all similar ones.
I have tried the following approaches:
add android:focusable="false"
to my list item xml
add TextView.setFocusable(false)
and TextView.setClickable(false)
in ViewHolder
using the xml as described in the above link.
But none of them work.
Here's my xml and java code:
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:paddingTop="2dp"
android:gravity="center_vertical" >
<TextView
android:id="@+id/ninegrid_number_list_choice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/ninegrid_number_listchoice_text_size"
android:gravity="center"
>
</TextView>
</LinearLayout>
getView
int list adapter.java:
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
ViewHolder holder;
if (convertView == null ) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.mTextView = (TextView)convertView.findViewById(R.id.ninegrid_number_list_choice);
holder.mTextView.setFocusable(false);
holder.mTextView.setClickable(false);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.mTextView.setText(mList.get(position));
holder.mTextView.setTextColor(mTextColor);
holder.mTextView.setFocusable(false);
holder.mTextView.setClickable(false);
return convertView;
}
Edit: in my activity:
listchoice.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
listChoice.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.v(tag, "sdf");
}
});