I am following this to set dividers in my gridview but the issue i am getting five items from server and set in my gridview,but in my last griditem it display grey box. See this Output https://imageshack.com/i/poKIvv1Up
Asked
Active
Viewed 415 times
1
-
And what should it be? – xAqweRx May 25 '16 at 10:54
2 Answers
4
public class Best_Product extends BaseAdapter {
View gridView;
private Context context;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();
Function_List fun_lib = new Function_List();
public Best_Product(Context c,ArrayList<HashMap<String, String>> json_value) {
// TODO Auto-generated method stub
context = c;
MyArr = json_value;
}
public int getCount() {
// TODO Auto-generated method stub
int total_size = MyArr.size();
Log.d("size of item", "" + MyArr.size());
if((MyArr.size()%2)==0)
{
// do nothing
}
else
{
total_size = MyArr.size()+1;
}
return total_size;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
HashMap<String, String> mapAtPostion = MyArr.get(position);
return Long.valueOf(MyArr.get(position).get("product_id"));
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
RelativeLayout background_color; // this will be the Relativelayout of the content of GridView. you will be set backgroud color of last item
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = new View(context);
view = inflater.inflate(R.layout.home_best_collection_content, parent, false);
background_color = (RelativeLayout)view.findViewById(R.id.bckground);
//checking last item of the array
if(position ==MyArr.size()-1)
{
//change the last item background color
background_color.setBackgroundColor();
}
else
{
// do the work here
}
}
else
{
}
return view;
}
}
Please try this code and let me know any problem, thanks

Maddy
- 4,525
- 4
- 33
- 53

Ashu Kumar
- 832
- 19
- 38
0
that's the default
behaviour, it should show the background
"empty space" since there is no more rows
to be inserted in row
number 6.
if i were you, i had get rid of GridView
and use RecyclerView
and use StaggeredLayoutManager
or GridLayoutManager
to have more controls over the span counts and could modify them accordingly.

Kosh
- 6,140
- 3
- 36
- 67
-
yes i agree that it will be easy with recycler but why not with gridview? – Aditya Vyas-Lakhan May 25 '16 at 11:01
-
@AdityaVyas cause this is common and usual behavior of GridView and it's impossible to do this, only to overwrite GridView elemen – xAqweRx May 25 '16 at 11:18