From 98b9a8fdf1cb11f7107bf08e0a5e55145e29dd26 Mon Sep 17 00:00:00 2001 From: dslul Date: Thu, 10 Sep 2020 19:04:43 +0200 Subject: [PATCH] emojis vertical swipe --- app/build.gradle | 1 + .../keyboard/emoji/EmojiLayoutParams.java | 3 +- .../keyboard/emoji/EmojiPalettesAdapter.java | 118 ++++++++++++++---- .../keyboard/emoji/EmojiPalettesView.java | 89 ++++++------- .../main/res/layout/emoji_keyboard_page.xml | 4 +- .../main/res/layout/emoji_palettes_view.xml | 5 +- 6 files changed, 148 insertions(+), 72 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0e7e54de4..be0ae6d1b 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,6 +41,7 @@ dependencies { implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.core:core-ktx:1.3.1' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'androidx.viewpager2:viewpager2:1.0.0' } repositories { mavenCentral() diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiLayoutParams.java b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiLayoutParams.java index f9af349dd..431aa5ed1 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiLayoutParams.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiLayoutParams.java @@ -24,6 +24,7 @@ import org.dslul.openboard.inputmethod.latin.utils.ResourceUtils; import androidx.viewpager.widget.ViewPager; +import androidx.viewpager2.widget.ViewPager2; final class EmojiLayoutParams { private static final int DEFAULT_KEYBOARD_ROWS = 4; @@ -61,7 +62,7 @@ public EmojiLayoutParams(final Resources res) { mEmojiKeyboardHeight = mEmojiPagerHeight - mEmojiPagerBottomMargin - 1; } - public void setPagerProperties(final ViewPager vp) { + public void setPagerProperties(final ViewPager2 vp) { final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) vp.getLayoutParams(); lp.height = mEmojiKeyboardHeight; lp.bottomMargin = mEmojiPagerBottomMargin; diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesAdapter.java b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesAdapter.java index af8835684..09cdc9674 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesAdapter.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesAdapter.java @@ -27,9 +27,10 @@ import org.dslul.openboard.inputmethod.keyboard.KeyboardView; import org.dslul.openboard.inputmethod.latin.R; -import androidx.viewpager.widget.PagerAdapter; +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; -final class EmojiPalettesAdapter extends PagerAdapter { +final class EmojiPalettesAdapter extends RecyclerView.Adapter{ private static final String TAG = EmojiPalettesAdapter.class.getSimpleName(); private static final boolean DEBUG_PAGER = false; @@ -83,25 +84,7 @@ public void releaseCurrentKey(final boolean withKeyRegistering) { currentKeyboardView.releaseCurrentKey(withKeyRegistering); } - @Override - public int getCount() { - return mEmojiCategory.getTotalPageCountOfAllCategories(); - } - - @Override - public void setPrimaryItem(final ViewGroup container, final int position, - final Object object) { - if (mActivePosition == position) { - return; - } - final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(mActivePosition); - if (oldKeyboardView != null) { - oldKeyboardView.releaseCurrentKey(false /* withKeyRegistering */); - oldKeyboardView.deallocateMemory(); - } - mActivePosition = position; - } - +/* @Override public Object instantiateItem(final ViewGroup container, final int position) { if (DEBUG_PAGER) { @@ -116,8 +99,8 @@ public Object instantiateItem(final ViewGroup container, final int position) { final Keyboard keyboard = mEmojiCategory.getKeyboardFromPagePosition(position); final LayoutInflater inflater = LayoutInflater.from(container.getContext()); - final EmojiPageKeyboardView keyboardView = (EmojiPageKeyboardView)inflater.inflate( - R.layout.emoji_keyboard_page, container, false /* attachToRoot */); + final EmojiPageKeyboardView keyboardView = (EmojiPageKeyboardView) inflater.inflate( + R.layout.emoji_keyboard_page, container, false); keyboardView.setKeyboard(keyboard); keyboardView.setOnKeyEventListener(mListener); container.addView(keyboardView); @@ -125,6 +108,20 @@ public Object instantiateItem(final ViewGroup container, final int position) { return keyboardView; } + @Override + public void setPrimaryItem(final ViewGroup container, final int position, + final Object object) { + if (mActivePosition == position) { + return; + } + final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(mActivePosition); + if (oldKeyboardView != null) { + oldKeyboardView.releaseCurrentKey(false); + oldKeyboardView.deallocateMemory(); + } + mActivePosition = position; + } + @Override public boolean isViewFromObject(final View view, final Object object) { return view == object; @@ -147,4 +144,79 @@ public void destroyItem(final ViewGroup container, final int position, Log.w(TAG, "Warning!!! Emoji palette may be leaking. " + object); } } +*/ + + @NonNull + @Override + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + /*if (DEBUG_PAGER) { + Log.d(TAG, "instantiate item: " + viewType); + } + final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(viewType); + if (oldKeyboardView != null) { + oldKeyboardView.deallocateMemory(); + // This may be redundant but wanted to be safer.. + mActiveKeyboardViews.remove(viewType); + } + final Keyboard keyboard = + mEmojiCategory.getKeyboardFromPagePosition(parent.getVerticalScrollbarPosition());*/ + final LayoutInflater inflater = LayoutInflater.from(parent.getContext()); + final EmojiPageKeyboardView keyboardView = (EmojiPageKeyboardView)inflater.inflate( + R.layout.emoji_keyboard_page, parent, false); + /*keyboardView.setKeyboard(keyboard); + keyboardView.setOnKeyEventListener(mListener); + parent.addView(keyboardView); + mActiveKeyboardViews.put(parent.getVerticalScrollbarPosition(), keyboardView);*/ + return new ViewHolder(keyboardView); + } + + @Override + public void onBindViewHolder(@NonNull EmojiPalettesAdapter.ViewHolder holder, int position) { + if (DEBUG_PAGER) { + Log.d(TAG, "instantiate item: " + position); + } + final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(position); + if (oldKeyboardView != null) { + oldKeyboardView.deallocateMemory(); + // This may be redundant but wanted to be safer.. + mActiveKeyboardViews.remove(position); + } + final Keyboard keyboard = + mEmojiCategory.getKeyboardFromPagePosition(position); + holder.getKeyboardView().setKeyboard(keyboard); + holder.getKeyboardView().setOnKeyEventListener(mListener); + //parent.addView(keyboardView); + mActiveKeyboardViews.put(position, holder.getKeyboardView()); + + /*if (mActivePosition == position) { + return; + } + final EmojiPageKeyboardView oldKeyboardView = mActiveKeyboardViews.get(mActivePosition); + if (oldKeyboardView != null) { + oldKeyboardView.releaseCurrentKey(false); + oldKeyboardView.deallocateMemory(); + } + mActivePosition = position;*/ + } + + @Override + public int getItemCount() { + return mEmojiCategory.getTotalPageCountOfAllCategories(); + } + + + static class ViewHolder extends RecyclerView.ViewHolder { + private EmojiPageKeyboardView customView; + + public ViewHolder(View v) { + super(v); + customView = (EmojiPageKeyboardView) v; + } + + public EmojiPageKeyboardView getKeyboardView() { + return customView; + } + + } } + diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesView.java b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesView.java index 581d17992..4bdb5f0d2 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesView.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesView.java @@ -49,6 +49,7 @@ import org.dslul.openboard.inputmethod.latin.utils.ResourceUtils; import androidx.viewpager.widget.ViewPager; +import androidx.viewpager2.widget.ViewPager2; import static org.dslul.openboard.inputmethod.latin.common.Constants.NOT_A_COORDINATE; @@ -63,9 +64,9 @@ * * Because of the above reasons, this class doesn't extend {@link KeyboardView}. */ -public final class EmojiPalettesView extends LinearLayout implements OnTabChangeListener, - ViewPager.OnPageChangeListener, View.OnClickListener, View.OnTouchListener, - EmojiPageKeyboardView.OnKeyEventListener { +public final class EmojiPalettesView extends LinearLayout + implements OnTabChangeListener, View.OnClickListener, View.OnTouchListener, + EmojiPageKeyboardView.OnKeyEventListener{ private final int mFunctionalKeyBackgroundId; private final int mSpacebarBackgroundId; private final boolean mCategoryIndicatorEnabled; @@ -83,7 +84,7 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange // TODO: Remove this workaround. private View mSpacebarIcon; private TabHost mTabHost; - private ViewPager mEmojiPager; + private ViewPager2 mEmojiPager; private int mCurrentPagerPosition = 0; private EmojiCategoryPageIndicatorView mEmojiCategoryPageIndicatorView; @@ -183,8 +184,46 @@ protected void onFinishInflate() { mEmojiPager = findViewById(R.id.emoji_keyboard_pager); mEmojiPager.setAdapter(mEmojiPalettesAdapter); - mEmojiPager.setOnPageChangeListener(this); - mEmojiPager.setOffscreenPageLimit(0); + + mEmojiPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {mEmojiPalettesAdapter.onPageScrolled(); + final Pair newPos = + mEmojiCategory.getCategoryIdAndPageIdFromPagePosition(position); + final int newCategoryId = newPos.first; + final int newCategorySize = mEmojiCategory.getCategoryPageSize(newCategoryId); + final int currentCategoryId = mEmojiCategory.getCurrentCategoryId(); + final int currentCategoryPageId = mEmojiCategory.getCurrentCategoryPageId(); + final int currentCategorySize = mEmojiCategory.getCurrentCategoryPageSize(); + if (newCategoryId == currentCategoryId) { + mEmojiCategoryPageIndicatorView.setCategoryPageId( + newCategorySize, newPos.second, positionOffset); + } else if (newCategoryId > currentCategoryId) { + mEmojiCategoryPageIndicatorView.setCategoryPageId( + currentCategorySize, currentCategoryPageId, positionOffset); + } else if (newCategoryId < currentCategoryId) { + mEmojiCategoryPageIndicatorView.setCategoryPageId( + currentCategorySize, currentCategoryPageId, positionOffset - 1); + } + } + + @Override + public void onPageSelected(int position) { + final Pair newPos = + mEmojiCategory.getCategoryIdAndPageIdFromPagePosition(position); + setCurrentCategoryId(newPos.first /* categoryId */, false /* force */); + mEmojiCategory.setCurrentCategoryPageId(newPos.second /* categoryPageId */); + updateEmojiCategoryPageIdView(); + mCurrentPagerPosition = position; + } + + @Override + public void onPageScrollStateChanged(int state) { + // Ignore this message. Only want the actual page selected. + } + }); + + mEmojiPager.setOffscreenPageLimit(ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT); mEmojiPager.setPersistentDrawingCache(PERSISTENT_NO_CACHE); mEmojiLayoutParams.setPagerProperties(mEmojiPager); @@ -243,44 +282,6 @@ public void onTabChanged(final String tabId) { updateEmojiCategoryPageIdView(); } - @Override - public void onPageSelected(final int position) { - final Pair newPos = - mEmojiCategory.getCategoryIdAndPageIdFromPagePosition(position); - setCurrentCategoryId(newPos.first /* categoryId */, false /* force */); - mEmojiCategory.setCurrentCategoryPageId(newPos.second /* categoryPageId */); - updateEmojiCategoryPageIdView(); - mCurrentPagerPosition = position; - } - - @Override - public void onPageScrollStateChanged(final int state) { - // Ignore this message. Only want the actual page selected. - } - - @Override - public void onPageScrolled(final int position, final float positionOffset, - final int positionOffsetPixels) { - mEmojiPalettesAdapter.onPageScrolled(); - final Pair newPos = - mEmojiCategory.getCategoryIdAndPageIdFromPagePosition(position); - final int newCategoryId = newPos.first; - final int newCategorySize = mEmojiCategory.getCategoryPageSize(newCategoryId); - final int currentCategoryId = mEmojiCategory.getCurrentCategoryId(); - final int currentCategoryPageId = mEmojiCategory.getCurrentCategoryPageId(); - final int currentCategorySize = mEmojiCategory.getCurrentCategoryPageSize(); - if (newCategoryId == currentCategoryId) { - mEmojiCategoryPageIndicatorView.setCategoryPageId( - newCategorySize, newPos.second, positionOffset); - } else if (newCategoryId > currentCategoryId) { - mEmojiCategoryPageIndicatorView.setCategoryPageId( - currentCategorySize, currentCategoryPageId, positionOffset); - } else if (newCategoryId < currentCategoryId) { - mEmojiCategoryPageIndicatorView.setCategoryPageId( - currentCategorySize, currentCategoryPageId, positionOffset - 1); - } - } - /** * Called from {@link EmojiPageKeyboardView} through {@link android.view.View.OnTouchListener} * interface to handle touch events from View-based elements such as the space bar. diff --git a/app/src/main/res/layout/emoji_keyboard_page.xml b/app/src/main/res/layout/emoji_keyboard_page.xml index db97b76ba..a2918907a 100644 --- a/app/src/main/res/layout/emoji_keyboard_page.xml +++ b/app/src/main/res/layout/emoji_keyboard_page.xml @@ -22,5 +22,5 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/emoji_keyboard_page" android:layoutDirection="ltr" - android:layout_width="wrap_content" - android:layout_height="wrap_content" /> + android:layout_width="match_parent" + android:layout_height="match_parent" /> diff --git a/app/src/main/res/layout/emoji_palettes_view.xml b/app/src/main/res/layout/emoji_palettes_view.xml index 4afba898c..bb66d15fa 100644 --- a/app/src/main/res/layout/emoji_palettes_view.xml +++ b/app/src/main/res/layout/emoji_palettes_view.xml @@ -59,10 +59,11 @@ - + android:layout_height="wrap_content" + android:orientation="vertical" />