Android 九宫格布局

作者: sugarkawhi | 来源:发表于2018-08-17 11:22 被阅读149次

    源码地址

    演示

    image

    需求

    满足0-9个图的适配

    图数量 演示
    1 image
    2 image
    3 image
    4 image
    5 image
    6 image
    7 image
    8 image
    9 image

    使用

    手动设置

    
     <me.sugarkawhi.ninegridlayout.NineGridLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:ngl_gridSpace="10dp"
                app:ngl_oneChildHeight="300dp">
                
                 <ImageView
                    style="@style/Img"
                    android:src="@drawable/ic_fox" />
                    
                 <ImageView
                    style="@style/Img"
                    android:src="@drawable/ic_deef" />
    
    </me.sugarkawhi.ninegridlayout.NineGridLayout>
    
    

    在RecyclerView中使用通过设置 NineGridAdapter

    NineGridAdapter源码:

    
    public abstract class NineGridAdapter {
    
        protected abstract View getItemView(ViewGroup parent, int position);
    
        protected abstract void bindView(View view, int position);
    
        protected abstract int getItemCount();
    
        protected void onItemClick(View view, int position) {
        }
    
    }
    
    

    eg:通过继承NineGridAdapter 详见demo

       private static class NineImageAdapter extends NineGridAdapter {
    
            private List<String> mUrls;
    
            public NineImageAdapter(List<String> urls) {
                mUrls = urls;
            }
    
            @Override
            protected View getItemView(ViewGroup parent, int position) {
                return LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_img, parent, false);
            }
    
            @Override
            protected void bindView(View view, int position) {
                String url = mUrls.get(position);
                ImageView imageView = view.findViewById(R.id.iv);
                Picasso.with(view.getContext())
                        .load(url)
                        .placeholder(R.drawable.ic_heart)
                        .into(imageView);
            }
    
            @Override
            protected int getItemCount() {
                return mUrls == null ? 0 : mUrls.size();
            }
    
            @Override
            protected void onItemClick(View view, int position) {
                super.onItemClick(view, position);
                Toast.makeText(App.getInstance(), "position " + position, Toast.LENGTH_SHORT).show();
            }
        }
    
    

    属性

    
    app:ngl_gridSpace="10dp"
    app:ngl_oneChildHeight="200dp"
    
    

    属性说明

    属性值 说明
    ngl_gridSpace 间距 默认20px
    ngl_oneChildHeight 当只有一个图的时候的高度 默认400px

    相关文章

      网友评论

      • 有点健忘:能继承viewgroup的感觉都比较厉害,我是写不了。
        这个效果感觉可以用GridLayoutManager的setSpanSizeLookup方法处理下,onBindViewHolder里单独处理下余数为1和2的情况下前2个的高度就可以了。

      本文标题:Android 九宫格布局

      本文链接:https://www.haomeiwen.com/subject/fcyzbftx.html