Fix #948 Force redraw of RecyclerView children so that tabs are properly oriented on screen rotation.

This commit is contained in:
Abdul Wadood 2019-08-15 23:38:09 +02:00
parent 189e1b4b9f
commit ddb96c064c
3 changed files with 47 additions and 34 deletions

View File

@ -66,7 +66,6 @@ import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatButton; import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
@ -466,7 +465,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
private void setTabListener() { private void setTabListener() {
tabsAdapter.setTabClickListener(new TabsAdapter.TabClickListener() { tabsAdapter.setTabClickListener(new TabsAdapter.TabClickListener() {
@Override @Override
public void onSelectTab(View view, int position) { public void onSelectTab(@NonNull View view, int position) {
hideTabSwitcher(); hideTabSwitcher();
selectTab(position); selectTab(position);
@ -475,7 +474,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
} }
@Override @Override
public void onCloseTab(View view, int position) { public void onCloseTab(@NonNull View view, int position) {
closeTab(position); closeTab(position);
} }
}); });
@ -1581,6 +1580,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
toggleActionItemsConfig(); toggleActionItemsConfig();
// Forcing redraw of RecyclerView children so that the tabs are properly oriented on rotation
tabRecyclerView.setAdapter(tabsAdapter);
} }
private void toggleActionItemsConfig() { private void toggleActionItemsConfig() {

View File

@ -133,7 +133,9 @@ public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
} }
holder.title.setText(webViewTitle); holder.title.setText(webViewTitle);
holder.close.setOnClickListener(v -> listener.onCloseTab(v, holder.getAdapterPosition())); holder.close.setOnClickListener(v -> listener.onCloseTab(v, holder.getAdapterPosition()));
holder.content.setImageBitmap(getBitmapFromView(webView)); holder.content.setImageBitmap(
getBitmapFromView(webView, getWindowWidth(activity), getWindowHeight(activity))
);
holder.content.setOnClickListener(v -> { holder.content.setOnClickListener(v -> {
selectedPosition = holder.getAdapterPosition(); selectedPosition = holder.getAdapterPosition();
listener.onSelectTab(v, selectedPosition); listener.onSelectTab(v, selectedPosition);
@ -159,9 +161,9 @@ public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
} }
public interface TabClickListener { public interface TabClickListener {
void onSelectTab(View view, int position); void onSelectTab(@NonNull View view, int position);
void onCloseTab(View view, int position); void onCloseTab(@NonNull View view, int position);
} }
static class ViewHolder extends RecyclerView.ViewHolder { static class ViewHolder extends RecyclerView.ViewHolder {

View File

@ -1,39 +1,49 @@
package org.kiwix.kiwixmobile.utils; package org.kiwix.kiwixmobile.utils;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.util.Log; import android.media.ThumbnailUtils;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class ImageUtils { public class ImageUtils {
public static Bitmap getBitmapFromView(View view) { @Nullable
view.clearFocus(); public static Bitmap getBitmapFromView(final @NonNull View viewToDrawFrom, int width,
view.setPressed(false); int height) {
boolean wasDrawingCacheEnabled = viewToDrawFrom.isDrawingCacheEnabled();
boolean willNotCache = view.willNotCacheDrawing(); if (!wasDrawingCacheEnabled) {
view.setWillNotCacheDrawing(false); viewToDrawFrom.setDrawingCacheEnabled(true);
/*
* Reset the drawing cache background color to fully transparent
* for the duration of this operation
*/
int color = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheBackgroundColor(0);
if (color != 0) {
view.destroyDrawingCache();
} }
view.buildDrawingCache(); if (width <= 0 || height <= 0) {
Bitmap cacheBitmap = view.getDrawingCache(); if (viewToDrawFrom.getWidth() <= 0 || viewToDrawFrom.getHeight() <= 0) {
if (cacheBitmap == null) { viewToDrawFrom.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
Log.e("ImageUtils", "Failed getViewBitmap(" + view + ")"); View.MeasureSpec
return null; .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
width = viewToDrawFrom.getMeasuredWidth();
height = viewToDrawFrom.getMeasuredHeight();
}
if (width <= 0 || height <= 0) {
final Bitmap bmp = viewToDrawFrom.getDrawingCache();
final Bitmap bitmap = bmp == null ? null : Bitmap.createBitmap(bmp);
if (!wasDrawingCacheEnabled) {
viewToDrawFrom.setDrawingCacheEnabled(false);
}
return bitmap;
}
viewToDrawFrom.layout(0, 0, width, height);
} else {
viewToDrawFrom.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec
.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
viewToDrawFrom.layout(0, 0, viewToDrawFrom.getMeasuredWidth(),
viewToDrawFrom.getMeasuredHeight());
}
final Bitmap drawingCache = viewToDrawFrom.getDrawingCache();
final Bitmap bmp = ThumbnailUtils.extractThumbnail(drawingCache, width, height);
final Bitmap bitmap = bmp == null || bmp != drawingCache ? bmp : Bitmap.createBitmap(bmp);
if (!wasDrawingCacheEnabled) {
viewToDrawFrom.setDrawingCacheEnabled(false);
} }
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
return bitmap; return bitmap;
} }