From 5bb943f84582f95c423507ddfb7a40169618b27c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 6 Jan 2017 15:33:31 +0800 Subject: [PATCH] Fix repo expand card issue --- .../magisk/adapters/ReposAdapter.java | 128 ++++++++++-------- 1 file changed, 69 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java b/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java index 52c542e24..9cb23771e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/adapters/ReposAdapter.java @@ -24,6 +24,7 @@ import com.topjohnwu.magisk.receivers.RepoDlReceiver; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.WebWindow; +import java.util.HashSet; import java.util.List; import butterknife.BindView; @@ -32,9 +33,11 @@ import butterknife.ButterKnife; public class ReposAdapter extends RecyclerView.Adapter { private List mUpdateRepos, mInstalledRepos, mOthersRepos; - private View mView; + private HashSet expandList = new HashSet<>(); private Context mContext; + private int expandHeight = 0; + public ReposAdapter(List update, List installed, List others) { mUpdateRepos = update; mInstalledRepos = installed; @@ -43,11 +46,11 @@ public class ReposAdapter extends RecyclerView.Adapter @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false); - ButterKnife.bind(this, mView); + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false); + ButterKnife.bind(this, v); mContext = parent.getContext(); - return new ViewHolder(mView); + return new ViewHolder(v); } @Override @@ -78,36 +81,46 @@ public class ReposAdapter extends RecyclerView.Adapter holder.description.setText(description); } - View.OnClickListener listener = view -> { - if (view.getId() == holder.updateImage.getId()) { - String filename = repo.getName() + "-" + repo.getVersion() + ".zip"; - MainActivity.alertBuilder - .setTitle(mContext.getString(R.string.repo_install_title, repo.getName())) - .setMessage(mContext.getString(R.string.repo_install_msg, filename)) - .setCancelable(true) - .setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.dlAndReceive( - mContext, - new RepoDlReceiver(), - repo.getZipUrl(), - Utils.getLegalFilename(filename))) - .setNegativeButton(R.string.no_thanks, null) - .show(); + holder.setExpanded(expandList.contains(repo)); + + holder.itemView.setOnClickListener(view -> { + if (holder.mExpanded) { + holder.collapse(); + expandList.remove(repo); + } else { + holder.expand(); + expandList.add(repo); } - if ((view.getId() == holder.changeLog.getId()) && (!repo.getLogUrl().equals(""))) { + }); + holder.changeLog.setOnClickListener(view -> { + if (!repo.getLogUrl().isEmpty()) { new WebWindow(mContext.getString(R.string.changelog), repo.getLogUrl(), mContext); } - if ((view.getId() == holder.authorLink.getId()) && (!repo.getSupportUrl().equals(""))) { + }); + holder.updateImage.setOnClickListener(view -> { + String filename = repo.getName() + "-" + repo.getVersion() + ".zip"; + MainActivity.alertBuilder + .setTitle(mContext.getString(R.string.repo_install_title, repo.getName())) + .setMessage(mContext.getString(R.string.repo_install_msg, filename)) + .setCancelable(true) + .setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.dlAndReceive( + mContext, + new RepoDlReceiver(), + repo.getZipUrl(), + Utils.getLegalFilename(filename))) + .setNegativeButton(R.string.no_thanks, null) + .show(); + }); + holder.authorLink.setOnClickListener(view -> { + if (!repo.getDonateUrl().isEmpty()) { mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(repo.getDonateUrl()))); } - if ((view.getId() == holder.supportLink.getId()) && (!repo.getSupportUrl().equals(""))) { + }); + holder.supportLink.setOnClickListener(view -> { + if (!repo.getSupportUrl().isEmpty()) { mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(repo.getSupportUrl()))); } - }; - - holder.changeLog.setOnClickListener(listener); - holder.updateImage.setOnClickListener(listener); - holder.authorLink.setOnClickListener(listener); - holder.supportLink.setOnClickListener(listener); + }); } @Override @@ -130,9 +143,7 @@ public class ReposAdapter extends RecyclerView.Adapter private ValueAnimator mAnimator; private ObjectAnimator animY2; - private ViewHolder holder; - - private boolean expanded = false; + private boolean mExpanded = false; public ViewHolder(View itemView) { super(itemView); @@ -140,51 +151,51 @@ public class ReposAdapter extends RecyclerView.Adapter ButterKnife.bind(this, itemView); DisplayMetrics dimension = new DisplayMetrics(); windowmanager.getDefaultDisplay().getMetrics(dimension); - holder = this; - this.expandLayout.getViewTreeObserver().addOnPreDrawListener( + expandLayout.getViewTreeObserver().addOnPreDrawListener( new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { - final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); - final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); - holder.expandLayout.getViewTreeObserver().removeOnPreDrawListener(this); - holder.expandLayout.setVisibility(View.GONE); - holder.expandLayout.measure(widthSpec, heightSpec); - final int holderHeight = holder.expandLayout.getMeasuredHeight(); - mAnimator = slideAnimator(0, holderHeight); - animY2 = ObjectAnimator.ofFloat(holder.updateImage, "translationY", holderHeight / 2); + 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); + mAnimator = slideAnimator(0, expandHeight); + animY2 = ObjectAnimator.ofFloat(updateImage, "translationY", expandHeight / 2); return true; } }); - - mView.setOnClickListener(view -> { - if (expanded) { - collapse(holder.expandLayout); - } else { - expand(holder.expandLayout); - } - expanded = !expanded; - }); - } - private void expand(View view) { - view.setVisibility(View.VISIBLE); + 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); + } + + private void expand() { + expandLayout.setVisibility(View.VISIBLE); mAnimator.start(); animY2.start(); - + mExpanded = true; } - private void collapse(View view) { - int finalHeight = view.getHeight(); + 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) { - view.setVisibility(View.GONE); + expandLayout.setVisibility(View.GONE); } @Override @@ -198,7 +209,7 @@ public class ReposAdapter extends RecyclerView.Adapter }); mAnimator.start(); animY2.reverse(); - + mExpanded = false; } private ValueAnimator slideAnimator(int start, int end) { @@ -207,8 +218,7 @@ public class ReposAdapter extends RecyclerView.Adapter animator.addUpdateListener(valueAnimator -> { int value = (Integer) valueAnimator.getAnimatedValue(); - ViewGroup.LayoutParams layoutParams = expandLayout - .getLayoutParams(); + ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams(); layoutParams.height = value; expandLayout.setLayoutParams(layoutParams); });