如何在gridview中设置分隔符

How to set divider in gridview

我正在关注 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

那是 default 的行为,它应该显示 background "empty space" 因为没有更多的 rows 可以插入到 row 数字 6 中。
如果我是你,我已经摆脱了 GridView 并使用 RecyclerView 并使用 StaggeredLayoutManagerGridLayoutManager 来更好地控制跨度计数并相应地修改它们。

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;
    }
}

请试试这段代码,有任何问题请告诉我, 谢谢