From 62a3e8d8318fc39504f95615a7db3bb2155305ca Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Sat, 18 Aug 2018 14:15:10 +0300 Subject: [PATCH] switch to recyclerview --- .../gr/thmmy/mthmmy/utils/EmojiKeyboard.java | 18 +++---- .../mthmmy/utils/EmojiKeyboardAdapter.java | 51 ++++++++++++++++++ .../mthmmy/utils/ImageKeyboardAdapter.java | 53 ------------------- app/src/main/res/layout/emoji_keyboard.xml | 12 ++--- .../res/layout/emoji_keyboard_grid_cell.xml | 6 +++ 5 files changed, 69 insertions(+), 71 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboardAdapter.java delete mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/ImageKeyboardAdapter.java create mode 100644 app/src/main/res/layout/emoji_keyboard_grid_cell.xml diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java b/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java index 06dff3d4..5a496be7 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java @@ -3,13 +3,14 @@ package gr.thmmy.mthmmy.utils; import android.content.Context; import android.os.Handler; import android.support.v7.widget.AppCompatImageButton; +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.util.AttributeSet; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.inputmethod.InputConnection; -import android.widget.GridView; import android.widget.LinearLayout; import gr.thmmy.mthmmy.R; @@ -151,13 +152,10 @@ public class EmojiKeyboard extends LinearLayout { emojis.append(R.drawable.emoji_extremely_shocked, "^ex_shocked^"); emojis.append(R.drawable.emoji_smurf, "^smurf^"); - GridView emojiGridView = findViewById(R.id.emoji_gridview); - emojiGridView.setAdapter(new ImageKeyboardAdapter(context, getEmojiArray())); - emojiGridView.setOnItemClickListener((parent, view, position, id) -> { - if (inputConnection == null) return; - String value = emojis.valueAt(position); - inputConnection.commitText(value, 1); - }); + RecyclerView emojiRecyclerview = findViewById(R.id.emoji_recyclerview); + emojiRecyclerview.setHasFixedSize(true); + emojiRecyclerview.setLayoutManager(new GridLayoutManager(context, 6)); + emojiRecyclerview.setAdapter(new EmojiKeyboardAdapter(getEmojiArray())); AppCompatImageButton backspaceButton = findViewById(R.id.backspace_button); // backspace behavior final Handler handler = new Handler(); @@ -190,8 +188,8 @@ public class EmojiKeyboard extends LinearLayout { this.inputConnection = inputConnection; } - public Integer[] getEmojiArray() { - Integer[] emojiArray = new Integer[emojis.size()]; + public int[] getEmojiArray() { + int[] emojiArray = new int[emojis.size()]; for (int i = 0; i < emojiArray.length; i++) { emojiArray[i] = emojis.keyAt(i); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboardAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboardAdapter.java new file mode 100644 index 00000000..a29f1030 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboardAdapter.java @@ -0,0 +1,51 @@ +package gr.thmmy.mthmmy.utils; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; +import android.support.v7.widget.AppCompatImageView; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.GridView; +import android.widget.ImageView; + +import java.util.Arrays; + +import gr.thmmy.mthmmy.R; + +public class EmojiKeyboardAdapter extends RecyclerView.Adapter { + private int[] emojiIds; + + public EmojiKeyboardAdapter(int[] emojiIds) { + this.emojiIds = emojiIds; + } + + @NonNull + @Override + public EmojiViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + AppCompatImageView imageView = (AppCompatImageView) LayoutInflater.from(parent.getContext()) + .inflate(R.layout.emoji_keyboard_grid_cell, parent, false); + return new EmojiViewHolder(imageView); + } + + @Override + public void onBindViewHolder(@NonNull EmojiViewHolder holder, int position) { + holder.imageView.setImageResource(emojiIds[position]); + } + + @Override + public int getItemCount() { + return emojiIds.length; + } + + public static class EmojiViewHolder extends RecyclerView.ViewHolder { + AppCompatImageView imageView; + public EmojiViewHolder(AppCompatImageView imageView) { + super(imageView); + this.imageView = imageView; + } + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ImageKeyboardAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ImageKeyboardAdapter.java deleted file mode 100644 index b19b6204..00000000 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/ImageKeyboardAdapter.java +++ /dev/null @@ -1,53 +0,0 @@ -package gr.thmmy.mthmmy.utils; - -import android.content.Context; -import android.graphics.drawable.Drawable; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; -import android.widget.GridView; -import android.widget.ImageView; - -import java.util.Arrays; - -public class ImageKeyboardAdapter extends BaseAdapter { - - private Context context; - private Integer[] emojiIds; - - public ImageKeyboardAdapter(Context context, Integer[] emojiIds) { - this.context = context; - this.emojiIds = emojiIds; - } - - @Override - public int getCount() { - return emojiIds.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 emoji; - if (convertView == null) { - emoji = new ImageView(context); - emoji.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - emoji.setScaleType(ImageView.ScaleType.CENTER_CROP); - - emoji.setPadding(8, 8, 8, 8); - } else { - emoji = (ImageView) convertView; - } - emoji.setImageResource(emojiIds[position]); - return emoji; - } -} diff --git a/app/src/main/res/layout/emoji_keyboard.xml b/app/src/main/res/layout/emoji_keyboard.xml index 156bfa1f..f159a051 100644 --- a/app/src/main/res/layout/emoji_keyboard.xml +++ b/app/src/main/res/layout/emoji_keyboard.xml @@ -19,13 +19,9 @@ android:padding="2dp" app:srcCompat="@drawable/ic_backspace_black_24dp" /> - + android:layout_height="200dp"/> \ No newline at end of file diff --git a/app/src/main/res/layout/emoji_keyboard_grid_cell.xml b/app/src/main/res/layout/emoji_keyboard_grid_cell.xml new file mode 100644 index 00000000..025802b9 --- /dev/null +++ b/app/src/main/res/layout/emoji_keyboard_grid_cell.xml @@ -0,0 +1,6 @@ + + \ No newline at end of file