Reputation: 3734
I have a ListView populated with custom XML ListItems, this is the XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
...
</RelativeLayout>
The listView shows correctly on screen and if i click or hold on an item it becomes blue (I'm using Holo Light Theme)
the problem comes when i try to assign an OnClickListener to the view, inside getView in my Activity that extends BaseAdapter:
@Override
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
...
convertView = InflateUtils.inflate(mContext, R.layout.list_item);
...
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "Test", 2000).show();
}
});
...
}
after doing that, the list item highlight color is no more shown, when i click or hold on a list item it's background stays white, anyway the onClickListener is perfectly working.
Do you have any suggestion to get the highlight color while keeping the default styles of HoloLight?
Upvotes: 5
Views: 6208
Reputation: 1118
You can keep the highlighting behaviour and have multiple OnClickListener
, if you
OnClickListener
OnClickListener
OnClickListener
from an OnItemClickListener
.To do so, add setClickable(false)
to your code:
@Override
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
...
convertView = InflateUtils.inflate(mContext, R.layout.list_item);
...
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "Test", 2000).show();
}
});
// make non-clickable again, like before setting the OnClickListener
convertView.setClickable(false);
...
}
and call the listener programmatically, e.g.:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
final View view = inflater.inflate(R.layout.fragment_with_listview, container, false);
final ListView your_listview = (ListView) view.findViewById(R.id.your_listview);
// delegate to specific callbacks
your_listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{ view.callOnClick();
}
});
...
}
Note the documented behaviour of callOnClick()
:
Directly call any attached OnClickListener. Unlike
performClick()
, this only calls the listener, and does not do any associated clicking actions like reporting an accessibility event.
Upvotes: 0
Reputation: 2622
Add this to the layout attributes for the parent RelativeLayout:
android:addStatesFromChildren="true"
Alternatively, in code call:
convertView.setAddStatesFromChildren(true);
In either case, be sure that the child layouts do not have the android:duplicateParentState
attribute set (and do not call setDuplicateParentState(true)
), or you will get an exception at run time.
Upvotes: 0
Reputation: 21036
You may want to use OnItemClickListener
on the ListView
itself, instead of separate click listeners for item views.
Also, highlight problem has already been described here: Android listview no longer highlights selection onclick
Upvotes: 3