혁신을 이룹니다, 오딘박스(OdinBOX)

언제나 어디서나 오딘박스와 함께!

[안드로이드스튜디오] 커스텀리스트뷰 활용하기

간지뽕빨리턴님 2020. 5. 22. 19:46
반응형

안드로이드스튜디오, 모바일앱개발, 아이폰,애플 object-c, kotlin, java, 대학생활, 코딩일기

커스텀리스트뷰를 활용하여, 어플 개발하기

과제 일시 : 2020-05-22

이번에는 저번에 만들었던 MoviePost와 흡사하지만 Context등을 활용하여, 영화소개 어플을 간단하게 만들어보았습니다. 기존의 MoviePost와 비슷하지만 기능이 추가되어져있습니다.

 

[실행화면]

실행화면

[소스코드]

* MainActivity.java

package com.example.moviepost;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    String postStr;
    String descStr;
    String manStr;

    int pos;
    ListViewItem lvitem;

    ListView list;
    ListViewAdapter adapter;

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



        list = (ListView)findViewById(R.id.lvMovie);
        adapter = new ListViewAdapter();

        list.setAdapter(adapter);

        registerForContextMenu(list);

        adapter.additem(ContextCompat.getDrawable(this, R.drawable.movie01), "실미도", "북파공작원의 이야기", "설경구");
        adapter.additem(ContextCompat.getDrawable(this, R.drawable.movie02), "태극기 휘날리며", "한국전쟁의 이야기", "원빈, 잗오건");
        adapter.additem(ContextCompat.getDrawable(this, R.drawable.movie03), "친구", "마이묵었다이가", "장동건");


    }

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu,menu);
        pos = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;
        lvitem = (ListViewItem)  list.getItemAtPosition(pos);

        postStr = lvitem.getTitle();
        descStr = lvitem.getDescript();
        manStr = lvitem.getMovieman();
    }


    public boolean onContextItemSelected(MenuItem item) {

        Toast toast = Toast.makeText(this,"",Toast.LENGTH_SHORT);


        switch (item.getItemId()){
            case R.id.text1:
                toast.setText(postStr + " 라는 제목입니다.");
                break;
            case R.id.text2:
                toast.setText(descStr + " 라는 이야기입니다.");
                break;
            case  R.id.text3:
                toast.setText(manStr);
                break;
        }
        toast.show();

        return super.onContextItemSelected(item);
    }
}


* ListViewAdapter.java
package com.example.moviepost;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class ListViewAdapter extends BaseAdapter {
    ArrayList<ListViewItem> itemList = new ArrayList<ListViewItem>();

    public ListViewAdapter(){

    }
    @Override
    public int getCount() {
        return itemList.size();
    }
    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int pos = position;
        final Context c = parent.getContext();

        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.listitem, parent, false);
        }
        ImageView post = (ImageView)convertView.findViewById(R.id.imageView);
        TextView title = (TextView)convertView.findViewById(R.id.textView);
        TextView descript = (TextView)convertView.findViewById(R.id.textView2);
        TextView movieman = (TextView)convertView.findViewById(R.id.textView3);

        ListViewItem listitem = itemList.get(pos);
        post.setImageDrawable(listitem.getPost());
        title.setText(listitem.getTitle());
        descript.setText(listitem.getDescript());
        movieman.setText(listitem.getMovieman());
        return convertView;
    }
    public void additem(Drawable post, String title, String descript, String movieman){
        ListViewItem item = new ListViewItem();
        item.setPost(post);
        item.setTitle(title);
        item.setDescript(descript);
        item.setMovieman(movieman);

        itemList.add(item);
    }
}

* ListViewItem.java
package com.example.moviepost;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class ListViewAdapter extends BaseAdapter {
    ArrayList<ListViewItem> itemList = new ArrayList<ListViewItem>();

    public ListViewAdapter(){

    }
    @Override
    public int getCount() {
        return itemList.size();
    }
    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int pos = position;
        final Context c = parent.getContext();

        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.listitem, parent, false);
        }
        ImageView post = (ImageView)convertView.findViewById(R.id.imageView);
        TextView title = (TextView)convertView.findViewById(R.id.textView);
        TextView descript = (TextView)convertView.findViewById(R.id.textView2);
        TextView movieman = (TextView)convertView.findViewById(R.id.textView3);

        ListViewItem listitem = itemList.get(pos);
        post.setImageDrawable(listitem.getPost());
        title.setText(listitem.getTitle());
        descript.setText(listitem.getDescript());
        movieman.setText(listitem.getMovieman());
        return convertView;
    }
    public void additem(Drawable post, String title, String descript, String movieman){
        ListViewItem item = new ListViewItem();
        item.setPost(post);
        item.setTitle(title);
        item.setDescript(descript);
        item.setMovieman(movieman);

        itemList.add(item);
    }
}

* activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/lvMovie"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

* listitem.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="127dp"
        android:layout_height="111dp"
        android:layout_weight="1"
        app:srcCompat="@drawable/movie01" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:textSize="26dp"
            android:textColor="#000000"
            android:gravity="center_vertical"
            android:layout_gravity="center_horizontal"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:text="TextView" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
</LinearLayout>

감사합니다 :D