Follow code:
Fragment1.cs:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.Inflate(Resource.Layout.fragment_1, container, false);
gridView = (GridView)view.FindViewById(Resource.Id.grid_view);
gridView.ItemClick += (sender, e) =>
{
if (view_1 != null)
{
view_1.SetBackgroundColor(Color.Transparent);
}
view_1 = e.View;
e.View.SetBackgroundColor(Color.ParseColor("#FFC107"));
};
adapter = new ArrayAdapter<string>(Activity, Resource.Layout.item_1, numbers);
gridView.Adapter = adapter;
return view;
}
item_1.axml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
fragment_1.axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:columnWidth="40dp"
android:verticalSpacing="15dp"
android:horizontalSpacing="1dp"
android:paddingTop="2dp"
android:stretchMode="columnWidth"
android:layout_centerInParent="true" />
</RelativeLayout>
Example picture:
I want to paint yellow in the first position.
I want to make the background in the first position in OnCreateView
, is it possible?
I have a gridview with numbers I need to load a background in the first position which is the number 1. How can I do this?
Just Like ListView
, GridView
also reuses its children. So if use View.GetChildAt(position)
and set the background color. you will get multiple views rendered with background color.
So, if you only want the first child to be rendered with background color, you have to rewrite the Adapter to SetBackgroundColor
when position==0
and clear the color if position!=0
:
The GetView
method of your custom adapter:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view=null;
if (convertView != null)
{
view = convertView;
}
else
{
view = LayoutInflater.FromContext(parent.Context).Inflate(Resource.Layout.item1,null);
}
if (position == 0)
{
view.SetBackgroundColor(Android.Graphics.Color.Yellow);
}
else
{
view.SetBackgroundColor(Android.Graphics.Color.Transparent);
}
(view as TextView).Text = _items[position];
return view;
}
Update:
Here is the complete codes of Adapter:
public class MyGridViewAdapter : BaseAdapter
{
string[] _items;
public MyGridViewAdapter(string[] items)
{
_items = items;
}
public override int Count => _items.Length;
public override Java.Lang.Object GetItem(int position)
{
return _items[position];
}
public override long GetItemId(int position)
{
return 0;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view=null;
if (convertView != null)
{
view = convertView;
}
else
{
view = LayoutInflater.FromContext(parent.Context).Inflate(Resource.Layout.item1,null);
}
if (position == 0)
{
view.SetBackgroundColor(Android.Graphics.Color.Yellow);
}
else
{
view.SetBackgroundColor(Android.Graphics.Color.Transparent);
}
(view as TextView).Text = _items[position];
return view;
}
}
You must select the item to which you want to apply the color in the OnCreate event
int itemposition = 0;
View tv = gridView.GetChildAt(itemposition);
tv.SetBackgroundColor(Color.ParseColor("#FFC107"));
Since GridView doesn't get populated from the adapter after the first layout pass, you have to change the background color using a ViewTreeObserver
. To get your View
's ViewTreeObserver
, you can use the View.ViewTreeObserver
property. Add a ViewTreeObserver.IOnGlobalLayoutListener
to the ViewTreeObserver
by using it's AddOnGlobalLayoutListener()
method. Your GridView should be populated by the first time the listener gets invoked.
Get the cell's view by using GridView
's GetChildAt(int position)
method in the OnGlobalLayoutListener
and set the background color just like you did in your code sample.