Java tutorial
/* * Copyright (C) 2016 Amit Shekhar * Copyright (C) 2011 Android Open Source Project * * 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. */ package com.networking.fragments; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import com.androidnetworking.widget.ANImageView; import com.networking.R; import com.networking.provider.Images; /** * Created by amitshekhar on 23/03/16. */ public class ImageGridFragment extends Fragment implements AdapterView.OnItemClickListener { private static final String TAG = "ImageGridFragment"; private int mImageThumbSize; private int mImageThumbSpacing; private ImageAdapter mAdapter; public ImageGridFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size); mImageThumbSpacing = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_spacing); mAdapter = new ImageAdapter(getActivity()); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View v = inflater.inflate(R.layout.image_grid_fragment, container, false); final GridView mGridView = (GridView) v.findViewById(R.id.gridView); mGridView.setAdapter(mAdapter); mGridView.setOnItemClickListener(this); mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onGlobalLayout() { if (mAdapter.getNumColumns() == 0) { final int numColumns = (int) Math .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing)); if (numColumns > 0) { final int columnWidth = (mGridView.getWidth() / numColumns) - mImageThumbSpacing; mAdapter.setNumColumns(numColumns); mAdapter.setItemHeight(columnWidth); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } } } } }); return v; } @Override public void onResume() { super.onResume(); mAdapter.notifyDataSetChanged(); } @Override public void onPause() { super.onPause(); } @Override public void onDestroy() { super.onDestroy(); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { } private class ImageAdapter extends BaseAdapter { private final Context mContext; private int mItemHeight = 0; private int mNumColumns = 0; private int mActionBarHeight = 0; private GridView.LayoutParams mImageViewLayoutParams; public ImageAdapter(Context context) { super(); mContext = context; mImageViewLayoutParams = new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); TypedValue tv = new TypedValue(); if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { mActionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics()); } } @Override public int getCount() { if (getNumColumns() == 0) { return 0; } return Images.imageThumbUrls.length + mNumColumns; } @Override public Object getItem(int position) { return position < mNumColumns ? null : Images.imageThumbUrls[position - mNumColumns]; } @Override public long getItemId(int position) { return position < mNumColumns ? 0 : position - mNumColumns; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { return (position < mNumColumns) ? 1 : 0; } @Override public boolean hasStableIds() { return true; } @Override public View getView(int position, View convertView, ViewGroup container) { if (position < mNumColumns) { if (convertView == null) { convertView = new View(mContext); } convertView.setLayoutParams( new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight)); return convertView; } ANImageView imageView; if (convertView == null) { imageView = new ANImageView(mContext); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(mImageViewLayoutParams); } else { imageView = (ANImageView) convertView; } if (imageView.getLayoutParams().height != mItemHeight) { imageView.setLayoutParams(mImageViewLayoutParams); } imageView.setDefaultImageResId(R.drawable.ic_toys_black_24dp); imageView.setErrorImageResId(R.drawable.ic_error_outline_black_24dp); imageView.setImageUrl(Images.imageThumbUrls[position - mNumColumns]); return imageView; } public void setItemHeight(int height) { if (height == mItemHeight) { return; } mItemHeight = height; mImageViewLayoutParams = new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mItemHeight); notifyDataSetChanged(); } public void setNumColumns(int numColumns) { mNumColumns = numColumns; } public int getNumColumns() { return mNumColumns; } } }