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

View File

@ -133,7 +133,9 @@ public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
}
holder.title.setText(webViewTitle);
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 -> {
selectedPosition = holder.getAdapterPosition();
listener.onSelectTab(v, selectedPosition);
@ -159,9 +161,9 @@ public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
}
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 {

View File

@ -1,39 +1,49 @@
package org.kiwix.kiwixmobile.utils;
import android.graphics.Bitmap;
import android.util.Log;
import android.media.ThumbnailUtils;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class ImageUtils {
public static Bitmap getBitmapFromView(View view) {
view.clearFocus();
view.setPressed(false);
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
/*
* 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();
@Nullable
public static Bitmap getBitmapFromView(final @NonNull View viewToDrawFrom, int width,
int height) {
boolean wasDrawingCacheEnabled = viewToDrawFrom.isDrawingCacheEnabled();
if (!wasDrawingCacheEnabled) {
viewToDrawFrom.setDrawingCacheEnabled(true);
}
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) {
Log.e("ImageUtils", "Failed getViewBitmap(" + view + ")");
return null;
if (width <= 0 || height <= 0) {
if (viewToDrawFrom.getWidth() <= 0 || viewToDrawFrom.getHeight() <= 0) {
viewToDrawFrom.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec
.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;
}