Reputation: 115
I've searched around for a few hours now and haven't been able to find any thing that fixes my issue. I have an application where I use a pop-up window. Within that popup Window I want to display a list of contacts on my phone, this part I have working. I created an a adapter that extends ArrayAdapter and I have it populating the list with the phones contacts.
But now I want to interact with these contacts, ie. click on one and show the contact's information to the user. I add an OnItemClickListener() to the list but it never gets called. I've tried making the cells not focusable, I've tried adding onClickListeners to each Item, but this doesn't give highlights to the cells. I am stuck.
Below is the code I use to setup the ListView and add the onItemClick listener to the list view.
ListView contactList = (ListView) contentView.findViewById(R.id.contacts_list);
ContactsProcessing processContacts = new ContactsProcessing();
contactList.setAdapter(processContacts.getListAdapter());
contactList.setItemsCanFocus(false);
contactList.setFocusableInTouchMode(false);
contactList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
// TODO Auto-generated method stub
// super.onListItemClick(l, v, position, id);
Toast toast = new Toast(MainActivity.this);
toast.setText("Item Clicked");
toast.show();
m_ButtonFeedback.hapticFeedback();
Log.d("MainActivity", "Item Clicked");
}
});
In the same pop-up window I also have an exit button. It works the way I expect.
ImageButton exit = (ImageButton) contentView.findViewById(R.id.contacts_close_button);
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View _v) {
window.dismiss();
}
});
Here is my cell's XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/transparent"
android:id="@+id/contact_cell_root" >
<TextView
android:id="@+id/contact_cell_headerText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="15dp"
android:scrollbars="none"
android:textColor="#29abe2"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="gone" />
<View
android:id="@+id/contact_cell_header_divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#29abe2"
android:visibility="gone"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:gravity="center"
android:paddingRight="10dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/contact_cell_user_image"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:scaleType="centerInside"
android:src="@drawable/contact_default"
android:focusable="false" />
<LinearLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/contact_cell_titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:singleLine="true"
android:text="John Smith"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/contact_cell_companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/titleText"
android:ellipsize="end"
android:text="Company Name"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
<ImageView
android:id="@+id/contact_cell_imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:gravity="center_vertical"
android:src="@drawable/ic_launcher"
android:scaleType="centerInside"
android:focusable="false"
/>
</LinearLayout>
<View
android:id="@+id/contact_cell_divider"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#e7e7e7"
/>
</LinearLayout>
Upvotes: 2
Views: 9177
Reputation: 10717
Looks like my solution is one of those Scott already tried but in the case someone still looking for some kind of implementation I'm posting this here.
So I had the same problem, but in my case setFocusble(false)
was required (and using ListPopupWindow
was not a solution in my case as a lot of stuff in the project already used base PopupWindow
's functionality including extending).
If someone in the same situation there is a kind of workaround based on bug discusson here (post #9)
The main idea is that ListView
's hierarchy is still receives touch events so we can manually trigger onItemClick()
.
However this approach is not 100% identical to real ListView
's touch handling (like there is no glow of selection while tapping a row) this done pretty well for me for the moment.
If someone has more precise solution of this problem, please share.
So, here is complete Adapter
's code which can be used with ListView
inside PopupWindow
which is setFocusable(false)
:
private class CustomAdapter extends ArrayAdapter {
private LayoutInflater mInflater;
private ListView mOwningListView;
public CustomAdapter(Context context, List<String> objects, ListView listView) {
super(context, android.R.layout.simple_list_item_1, objects);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOwningListView = listView;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.font_pick_row, null);
}
// this is the key point of workaround
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*
* as every row is still receiving their touches
* we can use this to manually trigger onItemClick
* since it doesn't firing in popupWindow.setFocusable(false)
*/
mOwningListView.getOnItemClickListener().onItemClick(mOwningListView, v, position, getItemId(position));
}
});
//... other stuff
return convertView;
}
}
Upvotes: 2
Reputation: 115
It turns out that Popup Windows are created not Focusable. The easy fix is to tell the Popup Window to be focusable.
PopupWindow window;
window = new PopupWindow(context);
window.setFocusable(true);
Once focusable, all the components in the view are active.
Upvotes: 8
Reputation: 324
An way to fix this is to make a dialog, and then use the setItems() command, if you look here:
http://developer.android.com/guide/topics/ui/dialogs.html
scroll down to "Adding a list" and there is an example, here on each item you can define onClick()
Upvotes: 0