본문 바로가기
안드로이드 자바

[JAVA][Android] 키보드에 글자가 가려지는 경우 setStackFromEnd 사용 // RecyclerView 스크롤 키보드

by teamnova 2024. 8. 15.
728x90

리사이클러뷰를 만들어서 아이템뷰나 텍스트를 출력해서 목록을 만듭니다.

그런데 화면에 뷰가 가득차고 추가로 데이터를 입력하기 위해서 키보드 자판이 나오게 합니다.

이 경우 자판에 텍스트가 가려져 불편할때가 있습니다. 

 

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">

    <EditText
        android:id="@+id/inText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Enter text here"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/send_btn"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/send_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/inText"
        app:layout_constraintTop_toBottomOf="@+id/show_title"
        tools:layout_editor_absoluteX="119dp" />

    <TextView
        android:id="@+id/show_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="채팅방"
        android:gravity="center"
        android:textSize="44sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

item_text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text_view"
    android:padding="8dp"
    android:textSize="30sp" />

 

MainActivity

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

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Button send_btn;
    EditText input_Text;
    RecyclerView recyclerView;
    TextAdapter textAdapter;
    ArrayList<String> arrayList;
    TextView textView;
    LinearLayoutManager layoutManager;

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

        textView = findViewById(R.id.show_title);
        send_btn = findViewById(R.id.send_btn);
        input_Text = findViewById(R.id.inText);
        recyclerView = findViewById(R.id.recyclerView);
        textAdapter = new TextAdapter();
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(textAdapter);

        arrayList = new ArrayList<>();
        for (int i = 1; i < 31; i++) {
            arrayList.add("Message : " + i);
            textAdapter.addItem(arrayList);
            textAdapter.notifyItemInserted(arrayList.size() - 1);
        }
        input_Text.setOnClickListener(v -> {
            // 마지막으로 보이는 아이템의 포지션 얻기
            int lastVisiblePosition = layoutManager.findLastVisibleItemPosition();
            System.out.println("현재 뷰에서 보이는 마지막 포지션: " + lastVisiblePosition);
        });
        send_btn.setOnClickListener(v -> {
            if (input_Text != null && !input_Text.getText().toString().isEmpty()) {
                String addText = input_Text.getText().toString();
                arrayList.add(addText);
                textAdapter.addItem(arrayList);
                textAdapter.notifyItemInserted(arrayList.size() - 1);
                recyclerView.smoothScrollToPosition(arrayList.size() - 1);
                input_Text.setText("");
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("글자를 입력해주세요!");
                builder.setMessage("글자를 입력하고 전송버튼을 눌러주세요!");
                builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.show();
            }
        });
    }
}

 

TextAdapter

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

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

import java.util.ArrayList;

public class TextAdapter extends RecyclerView.Adapter<TextAdapter.TextViewHolder> {
    ArrayList<String> textList = new ArrayList<>();


    @NonNull
    @Override
    public TextViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false);
        return new TextViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TextViewHolder holder, int position) {
        String text = textList.get(position);
        holder.textView.setText(text);
    }

    @Override
    public int getItemCount() {
        return textList.size();
    }

    public void addItem(ArrayList<String> arrayList) {
        this.textList = arrayList;
    }

    public static class TextViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public TextViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_view); // Correct ID reference
        }
    }
}

 

 

시연영상 (setStackFromEnd(true)) 경우

 

시연영상 (setStackFromEnd(false)) 경우