如何在gridview中设置分隔符

时间:2016-05-25 10:47:32

标签: android android-gridview

我正在关注this在我的gridview中设置分隔符,但问题是我从服务器获取了五个项目并在我的gridview中设置,但在我的上一个griditem中它显示灰色框。请参阅此输出https://imageshack.com/i/poKIvv1Up

2 个答案:

答案 0 :(得分: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;
    }
}

请尝试此代码并告诉我任何问题, 谢谢

答案 1 :(得分:0)

这是default行为,它应该显示background“空格”,因为rows号码6中不再需要插入row
如果我是你,我已经摆脱GridView并使用RecyclerView并使用StaggeredLayoutManagerGridLayoutManager来控制跨度计数,并可以相应地修改它们。