본문 바로가기
학교 강의/안드로이드 교양

안드교양 13주차

by hoshi03 2023. 11. 30.

1교시 리스트뷰, 어댑터뷰를 한 것 같은데 수업을 안들었음

 

2교시 그리드뷰

 

 

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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <GridView
        android:id="@+id/grid"
        android:numColumns="4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

 

dialog

<?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="vertical"
    android:gravity="center">

    <ImageView
        android:id="@+id/ivposter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

 

main

package com.example.aap;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {

    GridView grid;

    View dialogv;
    ImageView ivposter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        grid = findViewById(R.id.grid);
        dialogv = View.inflate(MainActivity.this, R.layout.dialog, null);
        ivposter = dialogv.findViewById(R.id.ivposter);
        Myadapter myadapter= new Myadapter(this);
        grid.setAdapter(myadapter);


    }

     public class Myadapter extends BaseAdapter {

        Integer[] posterID = {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};

         String[] postername = {"토이 스토리 4", "호빗", "제이슨 본", "반지의 제왕", "정직한 후보",
             "나쁜 녀석들", "겨울 왕국", "알라딘", "극한 직업", "스파이더맨", "레옹", "주먹왕 랄프 2",
             "타짜", "걸캅스", "도굴왕", "어벤져스","엑시트", "캡틴마블"};


        Context context;

        Myadapter(Context context){
            this.context = context;
        }

        @Override
        public int getCount() {
            return posterID.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView image = new ImageView(MainActivity.this);
            image.setLayoutParams(new ViewGroup.LayoutParams(200, 300));
            image.setImageResource(posterID[position]);
            image.setScaleType(ImageView.ScaleType.FIT_CENTER);
            image.setPadding(5,5,5,5);

            final int pos = position;
            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
                    dlg.setTitle(postername[pos]);
                    dlg.setIcon(R.drawable.movie_icon);
                    dlg.setView(dialogv);
                    dlg.setNegativeButton("닫기", null);
                    ivposter.setImageResource(posterID[pos]);
                    dlg.show();
                }
            });




            return image;
        }
    }

}

'학교 강의 > 안드로이드 교양' 카테고리의 다른 글

안드교양 15주차  (0) 2023.12.14
14주차  (0) 2023.12.07
안드교양 12주차  (2) 2023.11.23
안드교양 11주차  (0) 2023.11.16
안드교양 10주차  (0) 2023.11.09