Last active
July 12, 2016 18:27
-
-
Save gorrotowi/dfcae1aa835ef36883ad to your computer and use it in GitHub Desktop.
List 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"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:gravity="center"> | |
<ImageView | |
android:id="@+id/imgItemList" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:contentDescription="@string/hello_world" | |
android:src="@drawable/ic_launcher" /> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="4" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/txtItemListTitle" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textSize="20sp" | |
android:text="Item Text" /> | |
<TextView | |
android:id="@+id/txtItemListSubTitle" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textSize="16sp" | |
android:text="Item Text" /> | |
</LinearLayout> | |
</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
public class ItemList { | |
String title, subtitle; | |
int img; | |
public ItemList(String title, String subtitle, int img) { | |
this.title = title; | |
this.subtitle = subtitle; | |
this.img = img; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getSubtitle() { | |
return subtitle; | |
} | |
public void setSubtitle(String subtitle) { | |
this.subtitle = subtitle; | |
} | |
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; | |
public class ItemListAdapter extends BaseAdapter { | |
private Context ctx; | |
private int layoutResource; | |
private ArrayList<ItemList> data = new ArrayList<>(); | |
public ItemListAdapter(Context ctx, int layoutResource, ArrayList<ItemList> 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, txtSubtitle; | |
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.txtItemListTitle); | |
holder.txtSubtitle = (TextView) fila.findViewById(R.id.txtItemListSubTitle); | |
holder.img = (ImageView) fila.findViewById(R.id.imgItemList); | |
fila.setTag(holder); | |
} else { | |
holder = (HolderView) fila.getTag(); | |
} | |
ItemList item = (ItemList) data.get(position); | |
holder.txtTitle.setText(item.getTitle()); | |
holder.txtSubtitle.setText(item.getSubtitle()); | |
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.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
public class MainActivity extends ActionBarActivity { | |
ItemListAdapter adapter; | |
ListView listView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
listView = (ListView) findViewById(R.id.customList); | |
adapter = new ItemListAdapter(this, R.layout.item_list, getData()); | |
listView.setAdapter(adapter); | |
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
TextView txtTitle = (TextView) view.findViewById(R.id.txtItemListTitle); | |
Toast.makeText(MainActivity.this, txtTitle.getText().toString(), Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
private ArrayList<ItemList> getData() { | |
ArrayList<ItemList> item = new ArrayList<>(); | |
item.add(new ItemList("Titulo 1", "Subtitulo 1", R.drawable.ic_launcher)); | |
item.add(new ItemList("Titulo 2", "Subtitulo 2", R.drawable.ic_launcher)); | |
item.add(new ItemList("Titulo 3", "Subtitulo 3", R.drawable.ic_launcher)); | |
item.add(new ItemList("Titulo 4", "Subtitulo 4", R.drawable.ic_launcher)); | |
item.add(new ItemList("Titulo 5", "Subtitulo 5", R.drawable.ic_launcher)); | |
item.add(new ItemList("Titulo 6", "Subtitulo 6", R.drawable.ic_launcher)); | |
item.add(new ItemList("Titulo 7", "Subtitulo 7", R.drawable.ic_launcher)); | |
item.add(new ItemList("Titulo 8", "Subtitulo 8", R.drawable.ic_launcher)); | |
item.add(new ItemList("Titulo 9", "Subtitulo 9", R.drawable.ic_launcher)); | |
item.add(new ItemList("Titulo 10", "Subtitulo 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