728x90
정답이 아닐 수 있습니다. 오류가 있으면 알려주세요ㅎ
1.
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
2. Adapter
3.
ArrayAdapter - 배열
CursorAdapter - 데이터베이스
4. 주석 붙이면 됨
5.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
listitem.xml
<?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:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:src="@drawable/android" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
MainActivity.java
package kr.co.company.ch8;
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.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] values = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new CustomList(MainActivity.this);
list = findViewById(R.id.list);
list.setAdapter(adapter);
}
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
public CustomList(Activity context) {
super(context, R.layout.listitem, values);
this.context = context;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.listitem, null, true);
TextView textView = rowView.findViewById(R.id.text);
textView.setText(values[position]);
return rowView;
}
}
}
결과
6. columns = 1; //제 예상입니다. 직접 안해봄.
7.
activity_main.xml
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="90dp">
<ImageView
android:id="@+id/image"
android:layout_width="80dp"
android:layout_height="80dp" />
</RelativeLayout>
MainActivity.java
package kr.co.company.ch8;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.rview);
int columns = 4;
recyclerView.setLayoutManager(new GridLayoutManager(this, columns));
adapter = new MyAdapter(this);
recyclerView.setAdapter(adapter);
}
}
MyAdapter.java
package kr.co.company.ch8;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import androidx.recyclerview.widget.RecyclerView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private LayoutInflater mInflater;
int[] images = {
R.drawable.movie1,
R.drawable.movie2,
R.drawable.movie3,
R.drawable.movie4
};
MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.imageView.setImageResource(images[position % 4]);
}
@Override
public int getItemCount() {
return 40;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image);
}
}
}
결과
728x90
'Android > 그림으로 쉽게 설명하는 안드로이드 프로그래밍 개정6판' 카테고리의 다른 글
그림으로 쉽게 설명하는 안드로이드 프로그래밍(개정6판) 연습문제 11장 (0) | 2022.12.03 |
---|---|
그림으로 쉽게 설명하는 안드로이드 프로그래밍(개정6판) 연습문제 9장 (0) | 2022.11.30 |
그림으로 쉽게 설명하는 안드로이드 프로그래밍(개정6판) 연습문제 10장 (0) | 2022.11.08 |
그림으로 쉽게 설명하는 안드로이드 프로그래밍(개정6판) 연습문제 7장 (0) | 2022.10.31 |
[예제]PingPong 응용 - 볼을 비트맵으로 바꾸기(과제) (2) | 2022.10.18 |
댓글