class MainActivity
package org.androidtown.mylist;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listview;
SingerAdapter adapter;
String[] names ={"소녀시대", "걸스데이", "씨스타", "포미닛"};
String[] ages ={"21", "20", "23", "21"};
int[] imgs={R.drawable.a1, R.drawable.a2 , R.drawable.a3, R.drawable.a4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.listview1);
adapter=new SingerAdapter();
adapter.addItem(new SingerItemVO(names[0], ages[0], imgs[0]));
adapter.addItem(new SingerItemVO(names[1], ages[1], imgs[1]));
adapter.addItem(new SingerItemVO(names[2], ages[2], imgs[2]));
adapter.addItem(new SingerItemVO(names[3], ages[3], imgs[3]));
listview.setAdapter(adapter);
}
class SingerAdapter extends BaseAdapter{
ArrayList<SingerItemVO> items =new ArrayList();
public void addItem(SingerItemVO item){
items.add(item);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/* TextView view =new TextView(getApplicationContext());
view.setText(names[position]);
view.setTextSize(50.0f);
view.setTextColor(Color.BLACK);
if(position%2==0){
view.setBackgroundColor(Color.rgb(239,226,15 ) );
}else{
view.setBackgroundColor(Color.rgb(240,236,171) );
}*/
SingerItemView view =null;
//뷰의 재사용용
if(convertView==null){
view=new SingerItemView(getApplicationContext());
}else {
view = (SingerItemView) convertView;
}
view.setName(items.get(position).getName());
view.setAge(items.get(position).getAge());
view.setImage(items.get(position).getImg());
return view;
}
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.androidtown.mylist.MainActivity">
<Button
android:text="아이템 클릭"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:id="@+id/listview1" />
</RelativeLayout>
class SingerItemView
package org.androidtown.mylist;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by choi on 2017-04-08.
*/
public class SingerItemView extends LinearLayout {
TextView nameTextView, ageTextView;
ImageView image;
public SingerItemView(Context context) {
super(context);
init(context);
}
public SingerItemView(Context context, AttributeSet attrs, View convertView) {
super(context, attrs);
init(context);
}
private void init(Context context){
LayoutInflater inflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
//메모리에 올린다, true 라서 바로 올린다.
inflater.inflate(R.layout.singer_itmes, this, true);
image=(ImageView)findViewById(R.id.image1);
nameTextView=(TextView)findViewById(R.id.nameTextView);
ageTextView=(TextView)findViewById(R.id.ageTextView);
/*
에러
image=ViewHolderHelper.get(new ImageView(context), R.id.image1);
nameTextView=ViewHolderHelper.get(new TextView(context), R.id.nameTextView);
ageTextView=ViewHolderHelper.get(new TextView(context), R.id.ageTextView);
*/
}
public void setName(String name){
nameTextView.setText(name);
}
public void setAge(String age){
ageTextView.setText(age);
}
public void setImage(int img){
image.setImageResource(img);
}
}
R.layout.singer_itmes
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/a1"
android:layout_gravity="center_vertical"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
>
<TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30dp"
android:textColor="#ff000000"
/>
<TextView
android:id="@+id/ageTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:textColor="#128930"
/>
</LinearLayout>
</LinearLayout>
class SingerItemVO
package org.androidtown.mylist;
/**
* Created by choi on 2017-04-08.
*/
public class SingerItemVO {
String name;
String age;
int img;
public SingerItemVO() {
}
public SingerItemVO(String name, String age, int img) {
this.name = name;
this.age = age;
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
@Override
public String toString() {
return "SingerItemVO{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", img=" + img +
'}';
}
}
class ViewHolderHelper
package org.androidtown.mylist;
import android.util.SparseArray;
import android.view.View;
/**
* Created by choi on 2017-04-08.
*/
public class ViewHolderHelper {
public static <T extends View> T get(View convertView, int id){
SparseArray<View> viewHolder =(SparseArray<View>)convertView.getTag();
if(viewHolder==null){
viewHolder=new SparseArray<>();
convertView.setTag(viewHolder);
}
View childView=viewHolder.get(id);
if (childView == null) {
childView = convertView.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
}
댓글 ( 4)
댓글 남기기