I saw many variation of this question in SO but none of the answers fix my problem so I guess my implementation is a bit different than the rest. (I put all the recommendation like
android:descendantFocusability="blocksDescendants"
etc.)
I am trying to listen to a click event of a listView item. I managed to listen to longClick event but I couldn't replace it by regular click.
Here is what I did, (I tried to put down here the relevant parts to the problem only):
The ListView Definition (saved as my_list_row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="gm.activities.ViewAllActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"/>
</LinearLayout>
The listItem definition:
<?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="30dp"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
android:longClickable="true"
android:clickable="true">
<TextView
android:id="@+id/productNameText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:background="@color/unselected_row"
android:focusable="false"
android:longClickable="false"
android:clickable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/expiryDate"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:background="@color/unselected_row"
android:focusable="false"
android:longClickable="false"
android:clickable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
In my activity to set the ListView:
public class ViewAllActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_all_activity);
...
ListView listView=(ListView)findViewById(R.id.list);
listView.setAdapter(new DataListAdapter(dataList));
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View row,
int pos, long id) {
Log.d("ViewAllActivity", "Item clicked pos: " + pos);
});
}
class DataListAdapter extends BaseAdapter {
private List<GuaranteeObj> GaList;
DataListAdapter(List<GuaranteeObj> gaList) {
GaList = gaList;
}
@Override
public int getCount() {
return GaList.size();
}
@Override
public Object getItem(int position) {
return GaList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.my_list_row, parent, false);
return row;
}
}
The problem is I don't see that the ListItem get clicked - I don't see the expected log message. Anyone has idea why? )-:
Change Your listview item layout/view like below and you are done.
<?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="30dp"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/productNameText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:background="@color/unselected_row"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/expiryDate"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:background="@color/unselected_row"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
For more Info Check My Answer
Also you can set both onitemclicklistener and onlongitemclicklistner on listview like below:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos: " + pos);
return true;
}
});
you just have to return true instead of false.