728x90
반응형
1. 리스트 뷰의 표준 레이아웃
1) simple_list_item_1 : 하나의 텍스트 뷰 사용
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list">
</ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:padding="8dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/image"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:layout_alignTop="@+id/image"
android:layout_toRightOf="@+id/image"
android:textSize="17dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/rating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/image"
android:textSize="15dip" />
<TextView
android:id="@+id/genre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rating"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/image"
android:textColor="#666666"
android:textSize="13dip" />
<TextView
android:id="@+id/releaseYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textColor="#888888"
android:textSize="12dip" />
</RelativeLayout>
package kr.co.moviecustomlistview2;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView listView;
String[] titles = { "토이스토리1", "호빗2", "제이슨 본3", "반지의 왕4", "정직한 후보5",
"토이스토리6", "호빗7", "제이슨 본8", "반지의 왕9", "정직한 후보10",
"토이스토리11", "호빗12", "제이슨 본13", "반지의 왕14", "정직한 후보15",
"토이스토리16", "호빗17", "제이슨 본18", "반지의 왕19", "정직한 후보20",
"토이스토리21", "호빗22", "제이슨 본23", "반지의 왕24", "정직한 후보25",
"토이스토리26", "호빗27", "제이슨 본28", "반지의 왕29", "정직한 후보30",
"토이스토리31", "호빗32", "제이슨 본33", "반지의 왕34", "정직한 후보35",
"토이스토리36", "호빗37", "제이슨 본38", "반지의 왕39", "정직한 후보40",
"토이스토리41", "호빗42", "제이슨 본43", "반지의 왕44", "정직한 후보45",
"토이스토리46", "호빗47", "제이슨 본48", "반지의 왕49", "정직한 후보50",
"토이스토리51", "호빗52", "제이슨 본53", "반지의 왕54", "정직한 후보55",
"토이스토리56", "호빗57", "제이슨 본58", "반지의 왕59", "정직한 후보60",
"토이스토리61", "호빗62", "제이슨 본63", "반지의 왕64", "정직한 후보65",
"토이스토리66", "호빗a67", "제이슨 본68", "반지의 왕69", "정직한 후보70",
"토이스토리71", "호빗72", "제이슨 본73", "반지의 왕74", "정직한 후보75",
"토이스토리76", "호빗77", "제이슨 본78", "반지의 왕79", "정직한 후보80",
"토이스토리81", "호빗82", "제이슨 본83"};
Integer[] images = {
R.drawable.mov01, R.drawable.mov02, R.drawable.mov03, R.drawable.mov04, R.drawable.mov05,
R.drawable.mov06, R.drawable.mov07, R.drawable.mov08, R.drawable.mov09, R.drawable.mov10,
R.drawable.mov11, R.drawable.mov12, R.drawable.mov13, R.drawable.mov14, R.drawable.mov15,
R.drawable.mov16, R.drawable.mov17, R.drawable.mov18, R.drawable.mov19, R.drawable.mov20,
R.drawable.mov21, R.drawable.mov22, R.drawable.mov23, R.drawable.mov24, R.drawable.mov25,
R.drawable.mov26, R.drawable.mov27, R.drawable.mov28, R.drawable.mov29, R.drawable.mov30,
R.drawable.mov31, R.drawable.mov32, R.drawable.mov33, R.drawable.mov34, R.drawable.mov35,
R.drawable.mov36, R.drawable.mov37, R.drawable.mov38, R.drawable.mov39, R.drawable.mov40,
R.drawable.mov41, R.drawable.mov42, R.drawable.mov43, R.drawable.mov44, R.drawable.mov45,
R.drawable.mov46, R.drawable.mov47, R.drawable.mov48, R.drawable.mov49, R.drawable.mov50,
R.drawable.mov51, R.drawable.mov52, R.drawable.mov53, R.drawable.mov54, R.drawable.mov55,
R.drawable.mov56, R.drawable.mov57, R.drawable.mov58, R.drawable.mov59, R.drawable.mov60,
R.drawable.mov61, R.drawable.mov62, R.drawable.mov63, R.drawable.mov64, R.drawable.mov65,
R.drawable.mov66, R.drawable.mov67, R.drawable.mov68, R.drawable.mov69, R.drawable.mov70,
R.drawable.mov71, R.drawable.mov72, R.drawable.mov73, R.drawable.mov74, R.drawable.mov75,
R.drawable.mov76, R.drawable.mov77, R.drawable.mov78, R.drawable.mov79, R.drawable.mov80,
R.drawable.mov81, R.drawable.mov82, R.drawable.mov83 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
CustomList adaptor = new CustomList(MainActivity.this);
listView = findViewById(R.id.list);
listView.setAdapter(adaptor);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), titles[i], Toast.LENGTH_SHORT).show();
}
});
}
//내부 클래스 정의
private class CustomList extends ArrayAdapter<String> {
private final Activity context;
public CustomList(Activity context){
super(context, R.layout.listitem, titles);
this.context = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.listitem, null, true);
ImageView imageView = rowView.findViewById(R.id.image);
TextView title = rowView.findViewById(R.id.title);
TextView rating = rowView.findViewById(R.id.rating);
TextView genre = rowView.findViewById(R.id.genre);
TextView year = rowView.findViewById(R.id.releaseYear);
title.setText(titles[position]);
imageView.setImageResource(images[position]);
rating.setText("9.0"+position);
genre.setText("DRAMA");
year.setText(1950+position+"");
return rowView;
}
}
}
[결과 출력]
728x90
반응형
'App > Android Java' 카테고리의 다른 글
Android_Java(26) - GalleryApp (0) | 2022.02.14 |
---|---|
Android_Java(25) - MyGridView (0) | 2022.02.14 |
Android_Java(23) - MovieCustomListView (0) | 2022.02.13 |
Android_Java(22) - ActivityLifeCycle (0) | 2022.02.13 |
Android_Java(21) - ImplicitIntent (0) | 2022.02.11 |
댓글