Browse Source

switch to recyclerview

pull/45/head
Thodoris1999 6 years ago
parent
commit
62a3e8d831
  1. 18
      app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java
  2. 51
      app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboardAdapter.java
  3. 53
      app/src/main/java/gr/thmmy/mthmmy/utils/ImageKeyboardAdapter.java
  4. 12
      app/src/main/res/layout/emoji_keyboard.xml
  5. 6
      app/src/main/res/layout/emoji_keyboard_grid_cell.xml

18
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);
}

51
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<EmojiKeyboardAdapter.EmojiViewHolder> {
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;
}
}
}

53
app/src/main/java/gr/thmmy/mthmmy/utils/ImageKeyboardAdapter.java

@ -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;
}
}

12
app/src/main/res/layout/emoji_keyboard.xml

@ -19,13 +19,9 @@
android:padding="2dp"
app:srcCompat="@drawable/ic_backspace_black_24dp" />
<GridView
android:id="@+id/emoji_gridview"
<android.support.v7.widget.RecyclerView
android:id="@+id/emoji_recyclerview"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />
android:layout_height="200dp"/>
</merge>

6
app/src/main/res/layout/emoji_keyboard_grid_cell.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="8dp"/>
Loading…
Cancel
Save