Last active
July 12, 2016 18:27
-
-
Save gorrotowi/a2fdba9ec74bc735db6f to your computer and use it in GitHub Desktop.
Grid Custom 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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:gravity="center"> | |
<ImageView | |
android:layout_width="100dp" | |
android:layout_height="100dp" | |
android:src="@drawable/ic_launcher" | |
android:contentDescription="@string/hello_world" | |
android:layout_centerHorizontal="true" | |
android:id="@+id/imgGrid" /> | |
<TextView | |
android:id="@+id/txtGrid" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:textSize="20sp" | |
android:text="Texto Item Grid" | |
android:layout_alignBottom="@+id/imgGrid" | |
android:layout_centerHorizontal="true" /> | |
</RelativeLayout> |
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
package com.gorrotowi.listandgrid; | |
public class ItemGrid { | |
String title; | |
int img; | |
public ItemGrid(String title, int img) { | |
this.title = title; | |
this.img = img; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public int getImg() { | |
return img; | |
} | |
public void setImg(int img) { | |
this.img = img; | |
} | |
} |
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
import android.app.Activity; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
/** | |
* Created by Richie on 2/4/15. | |
*/ | |
public class ItemGridAdapter extends BaseAdapter { | |
private Context ctx; | |
private int layoutResource; | |
private ArrayList<ItemGrid> data = new ArrayList<>(); | |
public ItemGridAdapter(Context ctx, int layoutResource, ArrayList<ItemGrid> data) { | |
this.ctx = ctx; | |
this.layoutResource = layoutResource; | |
this.data = data; | |
} | |
@Override | |
public int getCount() { | |
return data.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
return data.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
public class HolderView { | |
TextView txtTitle; | |
ImageView img; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View fila = convertView; | |
HolderView holder = null; | |
if (fila == null) { | |
LayoutInflater inflater = ((Activity) ctx).getLayoutInflater(); | |
fila = inflater.inflate(layoutResource, parent, false); | |
holder = new HolderView(); | |
holder.txtTitle = (TextView) fila.findViewById(R.id.txtGrid); | |
holder.img = (ImageView) fila.findViewById(R.id.imgGrid); | |
fila.setTag(holder); | |
} else { | |
holder = (HolderView) fila.getTag(); | |
} | |
ItemGrid item = (ItemGrid) data.get(position); | |
holder.txtTitle.setText(item.getTitle()); | |
holder.img.setImageResource(item.getImg()); | |
return fila; | |
} | |
} |
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
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.GridView; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
public class MainActivity extends ActionBarActivity { | |
GridView gridView; | |
ItemGridAdapter adapterGrid; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
gridView = (GridView) findViewById(R.id.customGrid); | |
adapterGrid = new ItemGridAdapter(this, R.layout.item_grid, getDataGrid()); | |
gridView.setAdapter(adapterGrid); | |
} | |
private ArrayList<ItemGrid> getDataGrid() { | |
ArrayList<ItemGrid> item = new ArrayList<>(); | |
item.add(new ItemGrid("Titulo 1", R.drawable.ic_launcher)); | |
item.add(new ItemGrid("Titulo 2", R.drawable.ic_launcher)); | |
item.add(new ItemGrid("Titulo 3", R.drawable.ic_launcher)); | |
item.add(new ItemGrid("Titulo 4", R.drawable.ic_launcher)); | |
item.add(new ItemGrid("Titulo 5", R.drawable.ic_launcher)); | |
item.add(new ItemGrid("Titulo 6", R.drawable.ic_launcher)); | |
item.add(new ItemGrid("Titulo 7", R.drawable.ic_launcher)); | |
item.add(new ItemGrid("Titulo 8", R.drawable.ic_launcher)); | |
item.add(new ItemGrid("Titulo 9", R.drawable.ic_launcher)); | |
item.add(new ItemGrid("Titulo 10", R.drawable.ic_launcher)); | |
return item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment