Search code examples
androidandroid-gridview

How to set divider in gridview


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


Solution

  • 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