NineGridImageView

996.icu

中文版说明点我

This is a custom widget for Android, which uesd for showing grid pictures, such as you seeing in weibo or wechat.

Update Log

Sample

Download NineGridImageView-Demo

Features

Usage

1. Add the dependencies to your build.gradle file, NineGridImageView is avaiable in JCenter:
compile 'com.jaeger.ninegridimageview:library:1.1.1'
2. Add the NineGridImageView to your layout XML:
<com.jaeger.ninegridimageview.NineGridImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    app:imgGap="4dp"
    app:showStyle="fill"
    app:singleImgSize="120dp"/>
3. set a NineGridImageViewAdapter for NineGridImageView
nineGridImageView.setAdapter(nineGridViewAdapter);

Here is NineGridImageViewAdapter.class source code: ​

public abstract class NineGridImageViewAdapter<T> {

    protected abstract void onDisplayImage(Context context, ImageView imageView, T t);

    protected void onItemImageClick(Context context, ImageView imageView, int index, List<T> list) {

    }

    protected ImageView generateImageView(Context context) {
        GridImageView imageView = new GridImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return imageView;
    }
}           

Here is sample code: ​

private NineGridImageViewAdapter<Photo> mAdapter = new NineGridImageViewAdapter<Photo>() {
    @Override
    protected void onDisplayImage(Context context, ImageView imageView, Photo photo) {
        Picasso.with(context)
            .load(photo.getSmallUrl)
            .placeholder(R.drawable.ic_default_image)
            .into(imageView);
    }

    @Override
    protected ImageView generateImageView(Context context) {
        return super.generateImageView(context);
    }

    @Override
    protected void onItemImageClick(Context context, ImageView imageView, int index, List<Photo> photoList) {
       showBigPicture(context, photoList.get(index).getBigUrl());
    }
};

...
    mNineGridImageView.setAdapter(mAdapter);
...
4. set pictures data for NineGridImageView
nineGridImageView.setImagesData(List<T> imageDataList);
5. set ItemImageClickListener for NineGridImageView
mNineImageView.setItemImageClickListener(new ItemImageClickListener<String>() {
    @Override
    public void onItemImageClick(Context context, ImageView imageView, int index, List<String> list) {

    }
});

Credits

License

Copyright 2016 Jaeger Chen

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.