Last active
April 1, 2017 20:39
-
-
Save maxivak/7459045 to your computer and use it in GitHub Desktop.
GridView with images. Tutorial: http://maxivak.com/showing-images-in-gridview-with-multiple-columns-android/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Product.java | |
package com.example.mysamples; | |
public class Product{ | |
public String id; | |
public String title; | |
public String img_url; | |
public Product(String p_title, String p_img_url) { | |
title = p_title; | |
img_url = p_img_url; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
layout/grid_item.xml | |
--> | |
<?xml version="1.0" encoding="UTF-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" | |
> | |
<ImageView | |
android:id="@+id/image" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:layout_margin="dp" | |
android:contentDescription="icon" | |
android:layout_gravity="center" | |
/> | |
<TextView | |
android:id="@+id/title" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:layout_gravity="center" | |
/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
layout/main_grid.xml | |
--> | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" > | |
<GridView | |
android:id="@+id/grid_products" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:numColumns="auto_fit" | |
android:stretchMode="columnWidth" | |
android:gravity="center" | |
android:background="@android:color/transparent" | |
android:columnWidth="90dp" | |
android:verticalSpacing="8dp" | |
android:horizontalSpacing="8dp" | |
/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Main Activity | |
package com.example.mysamples; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.widget.AbsListView; | |
import android.widget.GridView; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MyActivityGrid extends Activity implements AbsListView.OnScrollListener { | |
List products; | |
GridView gvProducts = null; | |
ProductListAdapterWithCache adapterProducts; | |
private boolean lvBusy = false; | |
/** | |
* Called when the activity is first created. | |
*/ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main_grid); | |
// populate data | |
products = new ArrayList(); | |
products.add(new Product("Orange","http://farm5.staticflickr.com/4142/4787427683_3672f1db9a_s.jpg")); | |
products.add(new Product("Apple","http://farm4.staticflickr.com/3139/2780642603_8d2c90e364_s.jpg")); | |
products.add(new Product("Pineapple","http://farm2.staticflickr.com/1008/1420343003_13eeb0f9f3_s.jpg")); | |
products.add(new Product("Appricot","http://farm5.staticflickr.com/4118/4784687474_0eca8226b0_z.jpg")); | |
// | |
gvProducts = (GridView) findViewById( R.id.grid_products); | |
adapterProducts = new ProductListAdapterWithCache(this, products); | |
gvProducts.setAdapter(adapterProducts); | |
} | |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | |
} | |
public void onScrollStateChanged(AbsListView view, int scrollState) { | |
switch (scrollState) { | |
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: | |
lvBusy = false; | |
adapterProducts.notifyDataSetChanged(); | |
break; | |
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: | |
lvBusy = true; | |
break; | |
case AbsListView.OnScrollListener.SCROLL_STATE_FLING: | |
lvBusy = true; | |
break; | |
} | |
} | |
public boolean isLvBusy(){ | |
return lvBusy; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ProductListAdapterWithCache.java | |
package com.example.mysamples; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import java.util.List; | |
public class ProductListAdapterWithCache extends ArrayAdapter<Product> { | |
private Context mContext; | |
List<Product> mylist; | |
public ProductListAdapterWithCache(Context _context, List<Product> _mylist) { | |
super(_context, R.layout.list_item, _mylist); | |
mContext = _context; | |
this.mylist = _mylist; | |
} | |
public View getView(int position, View convertView, ViewGroup parent) { | |
Product product = getItem(position); | |
ProductViewHolder holder; | |
if (convertView == null) { | |
convertView = new LinearLayout(getContext()); | |
String inflater = Context.LAYOUT_INFLATER_SERVICE; | |
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater); | |
convertView = vi.inflate(R.layout.grid_item, parent, false); | |
// | |
holder = new ProductViewHolder(); | |
holder.img = (ImageView)convertView.findViewById(R.id.image); | |
holder.title = (TextView)convertView.findViewById(R.id.title); | |
// | |
convertView.setTag(holder); | |
} | |
else{ | |
holder = (ProductViewHolder) convertView.getTag(); | |
} | |
// | |
holder.populate(product, ((MyActivityGrid)mContext).isLvBusy()); | |
// | |
return convertView; | |
} | |
static class ProductViewHolder { | |
public ImageView img; | |
public TextView title; | |
void populate(Product p) { | |
title.setText(p.title); | |
// | |
ImageDownloader imageDownloader = new ImageDownloader(); | |
imageDownloader.download(p.img_url, img); | |
} | |
void populate(Product p, boolean isBusy) { | |
title.setText(p.title); | |
if (!isBusy){ | |
// download from internet | |
ImageDownloader imageDownloader = new ImageDownloader(); | |
imageDownloader.download(p.img_url, img); | |
} | |
else{ | |
// set default image | |
img.setImageResource(R.drawable.spinner); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment