Android

Android::RecyclerView, Lottie, Animation μ‚¬μš©ν•΄μ„œ λ©”λͺ¨μž₯ μ•± μˆ˜μ •ν•˜κΈ°

λ„μΊλ¦¬πŸ± 2020. 7. 30. 15:17
λ°˜μ‘ν˜•

● 1μ°¨ μ™„μ„± λ™μ˜μƒ

 

 


● μ½”λ“œ

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"
    android:background="@color/colorPrimary"
    tools:context=".MainActivity">
    <ImageButton
        android:id="@+id/main_btn_back"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="12dp"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:srcCompat="@drawable/ic_arrow_back_black_24dp"
        tools:ignore="ContentDescription,RtlHardcoded,VectorDrawableCompat" />



    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/main_lv_memo_list"
        android:layout_width="match_parent"
        android:layout_height="590dp"
        app:layout_constraintTop_toBottomOf="@+id/main_btn_back"
        android:layout_marginTop="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:scrollbarFadeDuration="0"
        android:scrollbarSize="5dp"
        android:scrollbarThumbVertical="@color/colorPrimaryDark"
        android:scrollbars="vertical"
        />
    <TextView
        android:id="@+id/main_tv_count"
        android:text="15"
        android:textColor="@color/colorText"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="170dp"
        app:layout_constraintTop_toBottomOf="@id/main_lv_memo_list"
        tools:ignore="HardcodedText,RtlHardcoded"/>

    <TextView
        android:id="@+id/main_tv_explain"
        android:text="개의 λ©”λͺ¨"
        android:textColor="@color/colorText"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/main_tv_count"
        app:layout_constraintTop_toBottomOf="@id/main_lv_memo_list"
        android:layout_marginLeft="5dp"
        tools:ignore="HardcodedText,RtlHardcoded"/>

    <ImageButton
        android:id="@+id/main_btn_search"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="12dp"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/main_btn_add_text"
        app:srcCompat="@drawable/ic_search_black_24dp"
        tools:ignore="ContentDescription,RtlHardcoded,VectorDrawableCompat" />

    <ImageButton
        android:id="@+id/main_btn_add_text"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="12dp"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:srcCompat="@drawable/ic_mode_edit_black_24dp"
        tools:ignore="ContentDescription,RtlHardcoded,VectorDrawableCompat" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

 

CustomAdapter

package com.example.rvmemoapplication;

import android.content.Context;
import android.content.Intent;
import android.text.Layout;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

// CustomAdapter: ArrayList 에 μžˆλŠ” Memo 클래슀 νƒ€μž…μ˜ 데이터λ₯Ό RecyclerView 에 λ³΄μ—¬μ£ΌλŠ” 처리λ₯Ό ν•œλ‹€.
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder>
{
//    RecyclerView.Adapter λ₯Ό 상속받아 μƒˆλ‘œμš΄ μ–΄λŒ‘ν„°λ₯Ό λ§Œλ“€ λ•Œ μ˜€λ²„λΌμ΄λ“œ ν•΄μ•Όν•˜λŠ” ν•¨μˆ˜
//    : onCreateViewHolder : viewType ν˜•νƒœμ˜ μ•„μ΄ν…œ λ·°λ₯Ό μœ„ν•œ λ·° 홀더 객체 생성
//    : onBindViewHolder   : position 에 ν•΄λ‹Ήν•˜λŠ” 데이터λ₯Ό λ·°ν™€λ”μ˜ μ•„μ΄ν…œλ·°μ— ν‘œμ‹œ
//    : getItemCount       : 전체 μ•„μ΄ν…œ 갯수 리턴

    private ArrayList<Memo> mList;

    //(1) μ»€μŠ€ν…€ λ¦¬μŠ€λ„ˆ μΈν„°νŽ˜μ΄μŠ€ μ •μ˜ - onItemClick, onItemLongClick
    public interface OnItemClickListener
    {
        void onItemClick(View v, int pos);
    }
    public interface OnItemLongClickListener
    {
        void onItemLongClick(View v, int pos);
    }

    //(2) λ¦¬μŠ€λ„ˆ 객체λ₯Ό μ „λ‹¬ν•˜λŠ” λ©”μ„œλ“œμ™€ μ „λ‹¬λœ 객체λ₯Ό μ €μž₯ν•  λ³€μˆ˜(mListener) μΆ”κ°€
    private OnItemClickListener mListener =null;
    private OnItemLongClickListener mLongListener = null;

    public void setOnItemClickListener(OnItemClickListener listener)
    {
        this.mListener = listener;
    }
    public void setOnItemLongClickListener(OnItemLongClickListener listener)
    {
        this.mLongListener = listener;
    }

    // μ•„μ΄ν…œ λ·°λ₯Ό μ €μž₯ν•˜λŠ” 뷰홀더 클래슀
    public class CustomViewHolder extends RecyclerView.ViewHolder
    {
        TextView title;
        TextView contents;

        public CustomViewHolder(View itemview) {
            super(itemview);
            // λ·° 객체에 λŒ€ν•œ μ°Έμ‘°
            this.title = (TextView)itemview.findViewById(R.id.memo_tv_title);
            this.contents = (TextView)itemview.findViewById(R.id.memo_tv_contents);
//            this.like = (ImageButton)view.findViewById(R.id.memo_btn_heart);

            // (3)뷰홀더가 λ§Œλ“€μ–΄μ§€λŠ” μ‹œμ μ— 클릭 이벀트λ₯Ό μ²˜λ¦¬ν•΄μ•Ό ν•œλ‹€.
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //λ¦¬μ‚¬μ΄ν΄λŸ¬λ·°μ˜ λ·°ν™€λ”μ—λŠ” μžμ‹ μ˜ μœ„μΉ˜λ₯Ό 확인할 수 μžˆλŠ” μ•„λž˜ λ©”μ„œλ“œ 제곡
                    int pos = getAdapterPosition();
                    if(pos!= RecyclerView.NO_POSITION)
                    {
                       mListener.onItemClick(v,pos);
                    }
                }
            });

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = getAdapterPosition();
                    if(pos != RecyclerView.NO_POSITION)
                    {
                        mLongListener.onItemLongClick(v,pos);
                    }
                    return true;
                }
            });
        }
    }
    // μƒμ„±μžμ—μ„œ 데이터 리슀트 객체λ₯Ό μ „λ‹¬λ°›λŠ”λ‹€.
    public CustomAdapter(ArrayList<Memo> list)
    {
        this.mList = list;
    }



    // μ•„μ΄ν…œ λ·°λ₯Ό μœ„ν•œ 뷰홀더 객체 μƒμ„±ν•˜μ—¬ λ¦¬ν„΄ν•œλ‹€.
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType)
    {
        Context context = viewGroup.getContext();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;

        View view = inflater.inflate(R.layout.item_memo, viewGroup, false);

//        View view = LayoutInflater.from(viewGroup.getContext())
//                .inflate(R.layout.item_memo,viewGroup, false);
        CustomViewHolder viewHolder = new CustomViewHolder(view);
        return viewHolder;
    }


    // position 에 ν•΄λ‹Ήν•˜λŠ” 데이터λ₯Ό λ·°ν™€λ”μ˜ μ•„μ΄ν…œ 뷰에 ν‘œμ‹œ
    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder viewHolder, int position)
    {
        viewHolder.title.setText(mList.get(position).getTitle());
        viewHolder.contents.setText(mList.get(position).getContents());
    }

    // 전체 데이터 갯수 리턴
    @Override
    public int getItemCount() {
        // mList λΉ„μ–΄ 있으면 0리턴, μ•„λ‹ˆλ©΄ mList μ‚¬μ΄μ¦ˆ 리턴
        // return (null != mList ? mList.size():0);
        return mList.size();
    }


}

 

 

MainActivity

package com.example.rvmemoapplication;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<Memo> mArrayList;
    private CustomAdapter mAdapter;


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

    @Override
    public void onBackPressed() {
        // super.onBackPressed();
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.setMessage("Do you want to quit this app?");
        builder.setTitle("Exit Notification");
        builder.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mArrayList =new ArrayList<>();

        // [Done] λ¦¬μ‚¬μ΄ν΄λŸ¬λ·°μ— μ—°κ²°
        final RecyclerView mRecyclerView = (RecyclerView)findViewById(R.id.main_lv_memo_list);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);

        mAdapter = new CustomAdapter(mArrayList);
        mRecyclerView.setAdapter(mAdapter);

        // [Done] DBHelper 객체 생성
        String dbName = "Memo0.db";
        int dbVersion = 1;
        final DBHelper dbHelper;

        // [Done] db μ—μ„œ κ°’ κ°€μ Έμ™€μ„œ Memo ν΄λž˜μŠ€μ— λ‹΄μ•„ mArrayList 에 λ‹΄κΈ°
        dbHelper = new DBHelper(this,dbName,null,dbVersion);
        final SQLiteDatabase db = dbHelper.getReadableDatabase();
        String sql = "SELECT * FROM Memo0;";
        Cursor cursor = db.rawQuery(sql,null);
        final TextView memoCount = findViewById(R.id.main_tv_count);

        // [Done] 데이터 바뀐거 감지 ν•  수 μžˆκ²Œλ”
        mAdapter.notifyDataSetChanged();
        memoCount.setText(String.valueOf(cursor.getCount()));
        try{
            if(cursor.getCount() >0)
            {
                while(cursor.moveToNext())
                {
                    // DB에 μžˆλŠ” κ°’λ“€μ˜ String 값듀을 Memo 클래슀 μƒμ„±μžμ— λ‹΄κΈ°.
                    Memo m = new Memo(cursor.getString(1),cursor.getString(2));
                    mArrayList.add(m);
                }
            }
        }finally {
            cursor.close();
        }

        final ImageButton backBtn = findViewById(R.id.main_btn_back);
        final ImageButton searchBtn = findViewById(R.id.main_btn_search);
        final ImageButton newBtn = findViewById(R.id.main_btn_add_text);

        // [Done] λ’€λ‘œκ°€κΈ° λ²„νŠΌ
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation anim = AnimationUtils.loadAnimation(
                        getApplicationContext(),
                        R.anim.scale_anim);
                backBtn.startAnimation(anim);

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.setMessage("Do you want to quit this app?");
                builder.setTitle("Exit Notification");
                builder.show();
            }
        });

        String got_title_from_custom_Dialog;

        // 검색 λ²„νŠΌ
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation anim = AnimationUtils.loadAnimation(
                        getApplicationContext(),
                        R.anim.scale_anim);
                searchBtn.startAnimation(anim);

                Handler delayHandler = new Handler();
                delayHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // TODO

//                        customDialog = new CustomDialog(this);
//                        customDialog.show();
//
//                        private View.OnClickListener searchListener = new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//
//                            }
//                        }

                    }
                }, 2000);

            }
        });

        // [Done] μƒˆλ‘œμš΄ λ©”λͺ¨ μΆ”κ°€ λ²„νŠΌ
        newBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                float deg = newBtn.getRotation() + 80F;
//                newBtn.animate().rotation(deg).setInterpolator(new AccelerateDecelerateInterpolator());
                Animation anim = AnimationUtils.loadAnimation(
                        getApplicationContext(),
                        R.anim.rotate_anim);
                newBtn.startAnimation(anim);

                Handler delayHandler = new Handler();
                delayHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // TODO
                        Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                        startActivity(intent);
                    }
                }, 2000);

            }
        });

        //  [Done] 리사이클 λ·° λ‚΄μ˜ μ•„μ΄ν…œ ν΄λ¦­μ‹œ → edit
        mAdapter.setOnItemClickListener(new CustomAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {
                Intent intent = new Intent(MainActivity.this, EditActivity.class);
                Cursor cursor = (Cursor) dbHelper.getReadableDatabase().query("Memo0",null,null,null,null,null,null);
                cursor.moveToPosition(pos);

                String t_title = cursor.getString(1);
                String c_contents = cursor.getString(2);

                intent.putExtra("title",t_title);
                intent.putExtra("contents",c_contents);
                startActivity(intent);

            }
        });

        //  [Done] 리사이클 λ·° λ‚΄μ˜ μ•„μ΄ν…œ λ‘±ν΄λ¦­μ‹œ → μ‚­μ œ μ—¬λΆ€
        mAdapter.setOnItemLongClickListener(new CustomAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View v, final int pos) {
                final Cursor cursor = (Cursor) dbHelper.getReadableDatabase().query("Memo0",null,null,null,null,null,null);
                cursor.moveToPosition(pos);
                final String t_title = cursor.getString(1);
                // String c_contents = cursor.getString(2);
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // λ©”λͺ¨ μ‚­μ œ μž‘μ—…
                        String sql0 ="DELETE FROM Memo0 WHERE title = '"+ t_title +"';";
                        db.execSQL(sql0);
                        mArrayList.remove(pos);
                        mAdapter.notifyDataSetChanged();
                        memoCount.setText(String.valueOf(mArrayList.size()));
                    }
                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.setMessage("Do you want to delete this memo?");
                builder.setTitle("Delete Notification");
                builder.show();
            }
        });

    }
}

 

● μ°Έκ³ 

Lottieλ₯Ό μ΄μš©ν•΄ ν™”λ €ν•œ μ• λ‹ˆλ©”μ΄μ…˜ λ„£κΈ°

blog.codejun.space/67

lottiefiles.com/popular?page=2

airbnb.io/lottie/#/android

postDelayedλ©”μ†Œλ“œλ₯Ό μ΄μš©ν•œ delay μ²˜λ¦¬

blog.devez.net/362

μ•ˆλ“œλ‘œμ΄λ“œ λ¦¬μ‚¬μ΄ν΄λŸ¬λ·° κΈ°λ³Έ μ‚¬μš©λ²•. (Android RecyclerView)

recipes4dev.tistory.com/154

RecyclerView λ°μ΄ν„° μΆ”κ°€/ νŽΈμ§‘ μ‚­μ œ

webnautes.tistory.com/1222

RecyclerView μ•„μ΄ν…œ ν΄λ¦­ μ΄λ²€νŠΈ

lesslate.github.io/android/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-%EB%A6%AC%EC%82%AC%EC%9D%B4%ED%81%B4%EB%9F%AC%EB%B7%B0-%ED%81%B4%EB%A6%AD/

webnautes.tistory.com/1300

RecyclerView μ˜ˆμ œ

webnautes.tistory.com/1214

Recyclerview

developer.android.com/jetpack/androidx/releases/recyclerview

ConstraintLayout μ²΄μΈ μ„€μ •ν•˜κΈ°

black-jin0427.tistory.com/40

λ°˜μ‘ν˜•