티스토리 뷰

안드로이드 커스텀리스트뷰 CustomListView를 만들어 볼게요. 

인터넷에서 다운받은 국기들을 drawable 폴더안에 넣으세요. 

 

레이아웃 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </ListView>


</LinearLayout>

레이아웃 row.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"
    android:padding="15dp"
    >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"

        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text = "메인 타이틀"
            android:textColor="#000"
            android:textStyle="bold"
            android:layout_margin="5dp"
            android:textSize="20sp"
            />

        <TextView
            android:id="@+id/textview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text = "서브 타이틀"
            android:textColor="#a9a9a9"

            android:layout_margin="5dp"
            android:textSize="15sp"
            />

    </LinearLayout>


</LinearLayout>

자바 MainActivity

package com.example.customlistview;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
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 strTitle[] = {"한국","미국","일본","중국","영국"};
    String strDescription[] = {"한국의 수도는 서울","미국의 수도는 워싱턴","일본의 수도는 도쿄","중국의 수도는 베이징","영국의 수도는 런던"};
    int image[] = {R.drawable.korea, R.drawable.usa, R.drawable.japan,R.drawable.china,R.drawable.england};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);

        MyAdapter adapter = new MyAdapter(this,strTitle, strDescription,image);
        listView.setAdapter(adapter);

        //리스트뷰 아이템 클릭시
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, Integer.toString(position),Toast.LENGTH_LONG).show();

                if(position == 0 ){
                    Toast.makeText(MainActivity.this, "한국의 수도는 서울",Toast.LENGTH_LONG).show();
                }
                else if(position == 1 ){
                    Toast.makeText(MainActivity.this, "미국의 수도는 워싱턴",Toast.LENGTH_LONG).show();
                }
                else if(position == 2 ){
                    Toast.makeText(MainActivity.this, "일본의 수도는 도쿄",Toast.LENGTH_LONG).show();
                }
                else if(position == 3 ){
                    Toast.makeText(MainActivity.this, "중국의 수도는 베이징",Toast.LENGTH_LONG).show();
                }
                else if(position == 4 ){
                    Toast.makeText(MainActivity.this, "영국의 수도는 런던",Toast.LENGTH_LONG).show();
                }


            }
        });


    }

    class MyAdapter extends ArrayAdapter<String>{

        Context context;
        String rTitle[];
        String rDescriptionp[];
        int rImgs[];

        MyAdapter(Context con, String sTitle[], String sDescription[], int imgs[]){
            super(con, R.layout.row, R.id.textview1, sTitle);
            this.context = con;
            this.rTitle = sTitle;
            this.rDescriptionp = sDescription;
            this.rImgs = imgs;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = layoutInflater.inflate(R.layout.row, parent, false);
            ImageView images = row.findViewById(R.id.imageView);
            TextView myTitle = row.findViewById(R.id.textview1);
            TextView myDescription = row.findViewById(R.id.textview2);

            images.setImageResource(rImgs[position]);
            myTitle.setText(rTitle[position]);
            myDescription.setText(rDescriptionp[position]);


            return row;
        }
    }
}

 

https://youtu.be/nGtQGd50kic

 

- YouTube

 

www.youtube.com

 

댓글
최근에 달린 댓글
글 보관함
«   2024/04   »
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
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글