Reputation: 89
I have Custom BaseAdapter
. Which i used in my GridView
. I trying to change Item background color by position(also i change Textview's text color)
this is a my baseadapter
source
public class ServiciesGridAdapter extends BaseAdapter {
private Context context;
private List<ServicesListItem> itemList;
public ServiciesGridAdapter(Context context, List<ServicesListItem> mobileValues) {
this.context = context;
this.itemList = mobileValues;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.view_servisies_gridview_items, null);
final LinearLayout mainLayout = (LinearLayout) gridView.findViewById(R.id.gridview_items_layout);
final TextView serviciesID = (TextView) gridView.findViewById(R.id.servicies_grid_id);
final TextView serviciesName = (TextView) gridView.findViewById(R.id.servicies_grid_name);
serviciesID.setText(itemList.get(position).getName());
serviciesName.setText(itemList.get(position).getIdentifier() + " EUR");
mainLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mainLayout.setBackgroundResource(R.drawable.rounded_corners_blue);
serviciesID.setTextColor(Color.parseColor("#ffffff"));
serviciesName.setTextColor(Color.parseColor("#ffffff"));
}
});
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
when i run my app in item click i can change background color, but when i checked another item,first item's background is a same. Simply, i want to change only one items background each click.
How i can solve my problem?
Upvotes: 0
Views: 2167
Reputation: 3906
use of listSelector for change item background by position in gridview ..
add color you color.xml
<resources>
<color name="onClickColor">#FFFFFF</color>
</resources>
add selecter.xml
to drawable
folder
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@color/onClickColor">
</item>
</selector>
add listSelector
to your GridView
<GridView
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/selecter">
</GridView>
Have a look at this post...
Upvotes: 0
Reputation: 774
you have to reset all other view text color and background, so make a loop and do it.. try below code
mainLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<parent.getChildCount();i++){
View c = parent.getChildAt(i);
c.findViewById(R.id.gridview_items_layout).setBackgroundResource(defaultResource);
(TextView) c.findViewById(R.id.servicies_grid_id).setTextColor(defaultColor);
((TextView) c.findViewById(R.id.servicies_grid_name)).setTextColor(defaultColor);
}
mainLayout.setBackgroundResource(R.drawable.rounded_corners_blue);
serviciesID.setTextColor(Color.parseColor("#ffffff"));
serviciesName.setTextColor(Color.parseColor("#ffffff"));
}
});
Upvotes: 1