Have seen this issue many times, i am using a customer adapter to populate my Listview with my customer layout xml. I understand the click is probably never going to List view and my layout is blocking it, but after trying the possible solutions nothing seems to be trying :
Tried: android:descendantFocusability="blocksDescendants" for layout of my custom xml
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
for each individual elements in the layout
Any other way to solve this
EDIT
Adding the code for where listview is attached to adapter:
if(!allcontactsstring.isEmpty()){
adapter = new MyContactListAdapter(getContext(),R.layout.contact_list_view,imageArry);
listView.setAdapter(adapter);
}
AdapterView.OnItemClickListener mylistViewClicked = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG);
}
};
listView.setOnItemClickListener(mylistViewClicked);
My Custom adapter :
public class MyContactListAdapter extends ArrayAdapter<MySQLiteContact> {
Context context;
int layoutResourceId;
ArrayList<MySQLiteContact> data=new ArrayList<MySQLiteContact>();
public MyContactListAdapter(Context context, int resource, ArrayList<MySQLiteContact> objects) {
super(context, resource, objects);
//this.layoutResourceId = layoutResourceId;
this.layoutResourceId = resource;
this.context = context;
this.data = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//ImageView tmp = (ImageView)row.findViewById(R.id.icon);
//tmp.clearFocus();
//tmp.isClickable();
//TextView tmp1=(TextView)row.findViewById(R.id.Itemname);
//tmp1.clearFocus();
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.Itemname);
holder.imgIcon = (ImageView)row.findViewById(R.id.icon);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
MySQLiteContact picture = data.get(position);
holder.txtTitle.setText(picture.getsFirstName()+" "+picture.getsLastName());
byte[] outImage=picture.getBimage();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
theImage=Bitmap.createScaledBitmap(theImage , 100, 100, true);
theImage= ContactDetailsMain.getRoundedRectBitmap(theImage,100);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
My xml with text view and imageview:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:src="@drawable/ic_contact_picture_holo_light"
/>
<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="@color/black"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:paddingTop="5dp"
/>
</LinearLayout>
EDIT A dirty workaround
As a crud and dirty workaround i have added a on click listener inside the adapter to each and every layout that gets inflated and this seems to work currently to get the positions.
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(position);
}
});
Not clean to have so many listeners , would be nice to have only one but yes for the moment it work..