※ 커스텀 뷰
- ListView를 구성하는 뷰를 개발자가 직접 만들어 사용한다.
※ 커스텀뷰 구현 소스
activity_main.xml (어댑터 뷰가 리스트를 보여줄 화면)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.androidex.MainActivity" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:textSize ="30px"
android:textStyle="bold"
android:text="Weather"></TextView>
<ListView
android:id="@+id/lv_weather"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="9"></ListView>
</LinearLayout>
custom_layout.xml (커스텀할 리스트 뷰의 한줄 양식)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_day"
android:layout_width="wrap_content"
android:layout_height="50px"
android:paddingLeft="10px">
</TextView>
<ImageView
android:id="@+id/lv_weather"
android:layout_width="wrap_content"
android:layout_height="50px"/>
<TextView
android:id="@+id/tv_comment"
android:layout_width="300px"
android:layout_height="50px"
android:gravity="center_vertical"
android:paddingLeft="20px"/>
<Button
android:id="@+id/bt_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private ArrayList<Weather> data = null;
private CustomAdapter costomAdapter =null;
private ListView lv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList<Weather>();
data.add(new Weather("월", R.drawable.ic_launcher, "맑음"));
data.add(new Weather("화", R.drawable.ic_launcher, "화창함"));
data.add(new Weather("수", R.drawable.ic_launcher, "흐림"));
data.add(new Weather("목", R.drawable.ic_launcher, "비/구름"));
data.add(new Weather("금", R.drawable.ic_launcher, "비"));
data.add(new Weather("토", R.drawable.ic_launcher, "많이 흐림"));
data.add(new Weather("일", R.drawable.ic_launcher, "맑음"));
//R.layout.custom_layout : 내가 만들어 놓은 레이아웃 한줄을 나타낸다.
costomAdapter = new CustomAdapter(MainActivity.this, R.layout.custom_layout, data);
lv = (ListView)findViewById(R.id.lv_weather);
lv.setAdapter(costomAdapter);
}
}
Weather.java (리스트뷰를 세팅할 값의 객체 - 한줄 정보)
public class Weather {
private String day;
private int icon;
private String comment;
public Weather(String day, int icon, String comment){
this.day = day;
this.icon = icon;
this.comment = comment;
}
public String getDay() {
return day;
}
public int getIcon() {
return icon;
}
public String getComment() {
return comment;
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter{
private Context context;
private int layout =0;
private ArrayList<Weather> data = null;
//레이아웃을 가지고 실제로 화면상에 UI를 만들어주는 객체
private LayoutInflater inflater= null;
public CustomAdapter(Context context, int layout, ArrayList<Weather> data) {
// TODO Auto-generated constructor stub
this.context = context;
this.layout = layout;
this.data = data;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position).getDay();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//필수
if(convertView == null){
convertView = inflater.inflate(this.layout, parent, false);
}
TextView tv_day= (TextView)convertView.findViewById(R.id.tv_day);
ImageView iv_icon = (ImageView) convertView.findViewById(R.id.lv_weather);
TextView tv_comment = (TextView)convertView.findViewById(R.id.tv_comment);
Button bt_select = (Button) convertView.findViewById(R.id.bt_select);
tv_day.setText(data.get(position).getDay() + " | ");
iv_icon.setImageResource(data.get(position).getIcon());
tv_comment.setText(data.get(position).getComment());
//격줄로 배경색을 다르게 한다
if((position%2)==1){
convertView.setBackgroundColor(0x500ff00);
}else{
convertView.setBackgroundColor(0x200ff00);
}
return convertView;
}
}
※ 소스분석
- Layout을 받을 때 int 형으로 받는다. 그 이유는 R에서 layout의 형태가 int형이기 때문이다.
- MainActivity - 메인함수
- Activity_Main - 실제 사용되는 레이아웃 (리스트 뷰를 보여주는 곳)
- Custom_layout - Activity_Main의 ListView의 한줄 한줄에 세팅될 레이아웃의 양식( 이 레이아웃을 Activity_Main의 ListView 한줄에 넣어준다)
- Weather - 데이터 객체( 아이콘이 int형인 이유는 R에서 이미지를 int형으로 세팅해놓기 때문이다)
- CustomAdapter - 사용자가 직접만든 어댑터
'플밍 is 뭔들 > 안드로이드_인강' 카테고리의 다른 글
05-4. 어댑터 뷰 (0) | 2016.11.27 |
---|---|
05-3. 어댑터 뷰 (0) | 2016.11.27 |
05-1. 어댑터 뷰 (0) | 2016.11.27 |
04. 커스텀 뷰 (0) | 2016.11.27 |
03-2. 레이아웃 (0) | 2016.11.27 |