본문 바로가기
  • We are looking for banner advertisers Please refer to the email below
안드로이드 스튜디오

안드로이드 스튜디오 리싸이클러뷰(RecyclerView) 예제소스

by 크론크롱 2021. 4. 20.
반응형

리싸이클러 예제

 

MainActivity.java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity<clip> extends AppCompatActivity {

    private ArrayList<MainData> arrayList;
    private MainAdapter mainAdapter;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;


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


        recyclerView = (RecyclerView)findViewById(R.id.RecyclerView_contents);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        arrayList = new ArrayList<>();
        mainAdapter = new MainAdapter(arrayList);
        recyclerView.setAdapter(mainAdapter);

        Button Button_add = (Button)findViewById(R.id.Button_add);
        Button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainData mainData = new MainData(R.mipmap.ic_launcher,"Tile","Contents");
                arrayList.add(mainData);
                mainAdapter.notifyDataSetChanged();
            }
        });

    }
}

 

MainData.java

public class MainData {

    private String TextView_title;
    private String InputEditText_contents;

    public MainData(int ic_launcher, String TextView_title, String InputEditText_contents) {
        this.TextView_title = TextView_title;
        this.InputEditText_contents = InputEditText_contents;
    }

    public String getTextView_title() {
        return TextView_title;
    }

    public void setTv_name(String tv_name) {
        this.TextView_title = TextView_title;
    }

    public String getTv_content() {
        return InputEditText_contents;
    }

    public void setTv_content(String tv_content) {
        this.InputEditText_contents = InputEditText_contents;
    }
}

 

MainAdapter.java

package samjin.net.sjclipboard;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

import java.util.ArrayList;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.CustomViewHolder> {

    private ArrayList<MainData> arrayList;

    public MainAdapter(ArrayList<MainData> arrayList) {
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public MainAdapter.CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
        CustomViewHolder holder = new CustomViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MainAdapter.CustomViewHolder holder, int position) {
        holder.TextView_title.setText(arrayList.get(position).getTextView_title());
        holder.InputEditText_contents.setText(arrayList.get(position).getTv_content());

        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String curName = holder.TextView_title.getText().toString();
                Toast.makeText(v.getContext(),curName,Toast.LENGTH_SHORT).show();


        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v){
                remove(holder.getAdapterPosition());
                return true;
            }
        });

            }
        });

    }



    @Override
    public int getItemCount() {
        return (null !=arrayList?arrayList.size():0);
    }

    public void remove(int position){
        try {
            arrayList.remove(position);
            notifyItemRemoved(position);
        } catch (IndexOutOfBoundsException ex) {
            ex.printStackTrace();
        }
    }


    public class CustomViewHolder extends RecyclerView.ViewHolder {

        protected TextView TextView_title;
        protected TextView InputEditText_contents;

        public CustomViewHolder(@NonNull View itemView) {
            super(itemView);
            this.TextView_title = (TextView) itemView.findViewById(R.id.TextView_title);
            this.InputEditText_contents = (TextView) itemView.findViewById(R.id.InputEditText_contents);
        }
    }
}

 

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"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:id="@+id/RelativeLayout_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RecyclerView_contents"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbarFadeDuration="0"
        android:scrollbarSize="5dp"
        android:scrollbarThumbVertical="@android:color/darker_gray"
        android:scrollbars="vertical"
        android:layout_weight="1"
        />

    <Button
        android:id="@+id/Button_add"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="8"
        android:text="추가"
        />

</LinearLayout>

 

 

item_list.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="wrap_content"
    >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:orientation="vertical"
            >

            <TextView
                android:id="@+id/TextView_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="클립보드만들기"
                />

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/InputEditText_contents"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="리싸이클러 뷰"
                />
        </LinearLayout>


</LinearLayout>

 

실행후, 추가버튼 클릭시 위와같이 EditText 가 출력된다.

반응형

댓글