티스토리 뷰

GridAdapter.java

class GridAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List pictureList;

    public GridAdapter (List list) {
        inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        pictureList = list;
    }

    @Override
    public int getCount() {
        return pictureList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.grid_item, parent, false);
        }
        ImageView imageView = convertView.findViewById(R.id.img_grid);

        //onCreate에서 정해준 크기로 이미지를 붙인다.
        Glide.with(mContext).load(pictureList.get(position)).override(mGridItemSize,mGridItemSize).into(imageView);

        return convertView;
    }
}
mGridItemSize - 얘는 그려주는 액티비티에서... 생성자 통해서 받아도 되고. 한 클래스에서 하면 멤버로 써도 되고.. 

DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
int mDisplayWidth = dm.widthPixels - DpToPixel(mContext, 10);
mGridItemSize = mDisplayWidth / 3 ;

/*** DpToPixel 코드

public static int DpToPixel(Context context, float DP) {
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DP, context.getResources()
.getDisplayMetrics());
return (int) px;
}

***/


adapter set 해주는 액티비티 onCreate()

GridView gridView = findViewById(R.id.gridview1);
mSend_btn = findViewById(R.id.btn_send);

ImageAdapter adapter = new ImageAdapter(mPictureList);
gridView.setAdapter(adapter);
mPictureList는 이전 글에 썼던 이미지 경로나.. 동영상 경로를 가져오면 된다. glide에서 친절하게 썸네일을 만들어서 뿌려주니 경로만 담고 있어도 ok







grid_item.xml


http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

            android:id="@+id/img_grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintDimensionRatio="1:1"
        android:scaleType="centerCrop"
        />


constrainDimensionRatio : 이미지의 가로세로 비율을 말한다

scaleType                 : 말그대로 센터 정렬 후 깨지는건 crop.





마지막으로 그리드뷰를 그리는 액티비티의 xml에 담을 그리드 뷰

    android:id="@+id/gridview1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:numColumns="3"
    android:verticalSpacing="3dp"
    android:horizontalSpacing="3dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:layout_weight="1"
    android:layout_margin="5dp"
    />
numColumns      : 가로에 들어갈 데이터의 갯수.

verticalSpacing    : 아이템 간 세로 틈 

horizontalSpacing : 아이템 간 가로 틈

stretchMode : numColumns를 'auto-fit'로 설정했을 경우 남는 좌우 공간을 어떻게 분배할지 결정한다. 'columnWidth'는 남은 공간이 칼럼들에게 골고루 할당되며, 'spacingWidth'는 컬럼들에게 여백으로 남은 공간을 골고루 할당한다. // (검색결과)