Reputation: 941
I have a listview with a custom BaseAdapter that fills listview items from json/internet.
In each listview item i have a pseudo buttons(FrameLayouts) with on click event attached to them. When "MAYBE" is clicked for example, I make a call to a server and change event status to "Maybe". After call comes back with success, I would like to change FrameLayouts background to appropriate color (Orange for Maybe).
Where i am having difficulty is to reference other/neighboring FrameLayouts so i can change their backgrounds to "neutral".
All of my events are in BaseAdapter and i set onClick event in GetView method.
My question is: Is there a better way/ how to setup onclick event for each FrameLayout so clicked FrameLayout is aware on click event of other neighboring FrameLayouts so i can change their background to neutral and selected background to respected color.
Thanks.
Upvotes: 2
Views: 457
Reputation: 68
You can use a selector to manipulate the state of the view
1) Create a selector to set the colors and states
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@android:color/black" android:state_selected="false"/>
<item android:drawable="@android:color/white" android:state_selected="true"/>
</selector>
2) On background set the selector
<FrameLayout
...
android:clickable="true"
android:background="@drawable/selector_view">
</FrameLayout>
3) In the code part try to play with this
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
...
}
Upvotes: 1
Reputation: 2771
in your XML add theese attibutes to your FrameLayout(Button)
android:clickable="true"
android:onClick="onMaybeClick"
Then in your baseAdapter getView()
method you find the FrameLayout by
FrameLayout maybeBtn = (FrameLayout)convertView.findViewById(R.id.list_item_maybe_btn);
maybeBtn.setTag(4257 + postion);
You can also set the maybeBtn
onClickable in the getView()
instead of XML, the same goes for the clicklistener.
In your activity you can the create a onButtonClick(View v)
method
public void onMaybeClick(View v)
{
int position = (Integer)((FrameLayout)v).getTag() - 4257; //To get the position of the click
LinearLayout parent = (LinearLayout)v.getParent();
FrameLayout btnYes = (FrameLayout)parent.getChildAt(0); //first button
FrameLayout btnNo = (FrameLayout)parent.getChildAt(2); //second button
ServerCallback(new requestListener(){
ifSuccess(){
v.setBackground(...);
}
]);
}
Upvotes: 0
Reputation: 4182
Register the same onClickListener()
for every item in the row.
Implement the onClickListener()
to the BaseAdapter
class, and place logic there in such a way that if MAYBE was clicked, loop every List<Item> itemList
of your ListView
, set a boolean value (for example otherSelected
) to true for each one of them, then invalidate()
the ListView
, and make sure that for the getView()
callback you handle those Items with otherSelected
set to true, and change their view background before returning them.
Upvotes: 0
Reputation: 3102
Do this inside your adapter
getView()
method. So it is going to applied to all the ListView
items currently on View
// change the row color based on selected position
if(selectedItem == position){
layout.setBackgroundColor(Color.WHITE);
}
else
{
layout.setBackgroundColor(Color.BLACK);
}
Upvotes: 0