From dd3b716d85bed506e47fbce21b632154efbabda4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 20 Jun 2017 17:55:33 +0800 Subject: [PATCH] Extract expandable viewholder --- .../magisk/adapters/PolicyAdapter.java | 81 ++--------------- .../magisk/adapters/SuLogAdapter.java | 91 +++++-------------- .../components/ExpandableViewHolder.java | 78 ++++++++++++++++ 3 files changed, 107 insertions(+), 143 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/components/ExpandableViewHolder.java diff --git a/app/src/main/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java b/app/src/main/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java index b9bc401cd..5b4577fd0 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java @@ -16,6 +16,7 @@ import android.widget.TextView; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.components.AlertDialogBuilder; +import com.topjohnwu.magisk.components.ExpandableViewHolder; import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.database.SuDatabaseHelper; import com.topjohnwu.magisk.superuser.Policy; @@ -122,11 +123,10 @@ public class PolicyAdapter extends RecyclerView.Adapter { - int value = (Integer) valueAnimator.getAnimatedValue(); - ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams(); - layoutParams.height = value; - expandLayout.setLayoutParams(layoutParams); - }); - return animator; - } - } } diff --git a/app/src/main/java/com/topjohnwu/magisk/adapters/SuLogAdapter.java b/app/src/main/java/com/topjohnwu/magisk/adapters/SuLogAdapter.java index 90dfcf394..01b2e46d1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/SuLogAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/SuLogAdapter.java @@ -1,17 +1,13 @@ package com.topjohnwu.magisk.adapters; -import android.animation.Animator; -import android.animation.ValueAnimator; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.ViewTreeObserver; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.TextView; import com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter; @@ -19,6 +15,7 @@ import com.thoughtbot.expandablerecyclerview.models.ExpandableGroup; import com.thoughtbot.expandablerecyclerview.viewholders.ChildViewHolder; import com.thoughtbot.expandablerecyclerview.viewholders.GroupViewHolder; import com.topjohnwu.magisk.R; +import com.topjohnwu.magisk.components.ExpandableViewHolder; import com.topjohnwu.magisk.superuser.SuLogEntry; import java.util.ArrayList; @@ -90,7 +87,7 @@ public class SuLogAdapter { SuLogEntry logEntry = (SuLogEntry) group.getItems().get(childIndex); holder.setExpanded(expandList.contains(logEntry)); holder.itemView.setOnClickListener(view -> { - if (holder.mExpanded) { + if (holder.getExpanded()) { holder.collapse(); expandList.remove(logEntry); } else { @@ -150,92 +147,50 @@ public class SuLogAdapter { } } + // Wrapper class static class LogViewHolder extends ChildViewHolder { + private InternalViewHolder expandableViewHolder; + @BindView(R.id.app_name) TextView appName; @BindView(R.id.action) TextView action; @BindView(R.id.time) TextView time; @BindView(R.id.fromPid) TextView fromPid; @BindView(R.id.toUid) TextView toUid; @BindView(R.id.command) TextView command; - @BindView(R.id.expand_layout) LinearLayout expandLayout; - private ValueAnimator mAnimator; - private boolean mExpanded = false; - private static int expandHeight = 0; - - public LogViewHolder(View itemView) { + LogViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); - expandLayout.getViewTreeObserver().addOnPreDrawListener( - new ViewTreeObserver.OnPreDrawListener() { + expandableViewHolder = new InternalViewHolder(itemView); + } - @Override - public boolean onPreDraw() { - if (expandHeight == 0) { - final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); - final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); - expandLayout.measure(widthSpec, heightSpec); - expandHeight = expandLayout.getMeasuredHeight(); - } + private class InternalViewHolder extends ExpandableViewHolder { - expandLayout.getViewTreeObserver().removeOnPreDrawListener(this); - expandLayout.setVisibility(View.GONE); - mAnimator = slideAnimator(0, expandHeight); - return true; - } + InternalViewHolder(View itemView) { + super(itemView); + } - }); + @Override + public void setExpandLayout(View itemView) { + expandLayout = itemView.findViewById(R.id.expand_layout); + } + } + + private boolean getExpanded() { + return expandableViewHolder.mExpanded; } private void setExpanded(boolean expanded) { - mExpanded = expanded; - ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams(); - layoutParams.height = expanded ? expandHeight : 0; - expandLayout.setLayoutParams(layoutParams); - expandLayout.setVisibility(expanded ? View.VISIBLE : View.GONE); + expandableViewHolder.setExpanded(expanded); } private void expand() { - expandLayout.setVisibility(View.VISIBLE); - mAnimator.start(); - mExpanded = true; + expandableViewHolder.expand(); } private void collapse() { - if (!mExpanded) return; - int finalHeight = expandLayout.getHeight(); - ValueAnimator mAnimator = slideAnimator(finalHeight, 0); - mAnimator.addListener(new Animator.AnimatorListener() { - @Override - public void onAnimationEnd(Animator animator) { - expandLayout.setVisibility(View.GONE); - } - - @Override - public void onAnimationStart(Animator animator) {} - - @Override - public void onAnimationCancel(Animator animator) {} - - @Override - public void onAnimationRepeat(Animator animator) {} - }); - mAnimator.start(); - mExpanded = false; - } - - private ValueAnimator slideAnimator(int start, int end) { - - ValueAnimator animator = ValueAnimator.ofInt(start, end); - - animator.addUpdateListener(valueAnimator -> { - int value = (Integer) valueAnimator.getAnimatedValue(); - ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams(); - layoutParams.height = value; - expandLayout.setLayoutParams(layoutParams); - }); - return animator; + expandableViewHolder.collapse(); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/components/ExpandableViewHolder.java b/app/src/main/java/com/topjohnwu/magisk/components/ExpandableViewHolder.java new file mode 100644 index 000000000..5b7fee1cd --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/components/ExpandableViewHolder.java @@ -0,0 +1,78 @@ +package com.topjohnwu.magisk.components; + +import android.animation.ValueAnimator; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewTreeObserver; +import android.widget.LinearLayout; + +public abstract class ExpandableViewHolder extends RecyclerView.ViewHolder { + + protected ViewGroup expandLayout; + private ValueAnimator expandAnimator, collapseAnimator; + private static int expandHeight = 0; + + public boolean mExpanded = false; + + public ExpandableViewHolder(View itemView) { + super(itemView); + setExpandLayout(itemView); + expandLayout.getViewTreeObserver().addOnPreDrawListener( + new ViewTreeObserver.OnPreDrawListener() { + + @Override + public boolean onPreDraw() { + if (expandHeight == 0) { + final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); + final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); + expandLayout.measure(widthSpec, heightSpec); + expandHeight = expandLayout.getMeasuredHeight(); + } + + expandLayout.getViewTreeObserver().removeOnPreDrawListener(this); + expandLayout.setVisibility(View.GONE); + expandAnimator = slideAnimator(0, expandHeight); + collapseAnimator = slideAnimator(expandHeight, 0); + return true; + } + + }); + } + + public void setExpanded(boolean expanded) { + mExpanded = expanded; + ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams(); + layoutParams.height = expanded ? expandHeight : 0; + expandLayout.setLayoutParams(layoutParams); + expandLayout.setVisibility(expanded ? View.VISIBLE : View.GONE); + } + + public void expand() { + if (mExpanded) return; + expandLayout.setVisibility(View.VISIBLE); + expandAnimator.start(); + mExpanded = true; + } + + public void collapse() { + if (!mExpanded) return; + collapseAnimator.start(); + mExpanded = false; + } + + public abstract void setExpandLayout(View itemView); + + private ValueAnimator slideAnimator(int start, int end) { + + ValueAnimator animator = ValueAnimator.ofInt(start, end); + + animator.addUpdateListener(valueAnimator -> { + int value = (Integer) valueAnimator.getAnimatedValue(); + ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams(); + layoutParams.height = value; + expandLayout.setLayoutParams(layoutParams); + }); + return animator; + } +}