Fix repo expand card issue

This commit is contained in:
topjohnwu 2017-01-06 15:33:31 +08:00
parent a3109953d0
commit 5bb943f845

View File

@ -24,6 +24,7 @@ import com.topjohnwu.magisk.receivers.RepoDlReceiver;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebWindow; import com.topjohnwu.magisk.utils.WebWindow;
import java.util.HashSet;
import java.util.List; import java.util.List;
import butterknife.BindView; import butterknife.BindView;
@ -32,9 +33,11 @@ import butterknife.ButterKnife;
public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder> { public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder> {
private List<Repo> mUpdateRepos, mInstalledRepos, mOthersRepos; private List<Repo> mUpdateRepos, mInstalledRepos, mOthersRepos;
private View mView; private HashSet<Repo> expandList = new HashSet<>();
private Context mContext; private Context mContext;
private int expandHeight = 0;
public ReposAdapter(List<Repo> update, List<Repo> installed, List<Repo> others) { public ReposAdapter(List<Repo> update, List<Repo> installed, List<Repo> others) {
mUpdateRepos = update; mUpdateRepos = update;
mInstalledRepos = installed; mInstalledRepos = installed;
@ -43,11 +46,11 @@ public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder>
@Override @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false); View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false);
ButterKnife.bind(this, mView); ButterKnife.bind(this, v);
mContext = parent.getContext(); mContext = parent.getContext();
return new ViewHolder(mView); return new ViewHolder(v);
} }
@Override @Override
@ -78,36 +81,46 @@ public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder>
holder.description.setText(description); holder.description.setText(description);
} }
View.OnClickListener listener = view -> { holder.setExpanded(expandList.contains(repo));
if (view.getId() == holder.updateImage.getId()) {
String filename = repo.getName() + "-" + repo.getVersion() + ".zip"; holder.itemView.setOnClickListener(view -> {
MainActivity.alertBuilder if (holder.mExpanded) {
.setTitle(mContext.getString(R.string.repo_install_title, repo.getName())) holder.collapse();
.setMessage(mContext.getString(R.string.repo_install_msg, filename)) expandList.remove(repo);
.setCancelable(true) } else {
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.dlAndReceive( holder.expand();
mContext, expandList.add(repo);
new RepoDlReceiver(),
repo.getZipUrl(),
Utils.getLegalFilename(filename)))
.setNegativeButton(R.string.no_thanks, null)
.show();
} }
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); 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()))); 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()))); 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 @Override
@ -130,9 +143,7 @@ public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder>
private ValueAnimator mAnimator; private ValueAnimator mAnimator;
private ObjectAnimator animY2; private ObjectAnimator animY2;
private ViewHolder holder; private boolean mExpanded = false;
private boolean expanded = false;
public ViewHolder(View itemView) { public ViewHolder(View itemView) {
super(itemView); super(itemView);
@ -140,51 +151,51 @@ public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder>
ButterKnife.bind(this, itemView); ButterKnife.bind(this, itemView);
DisplayMetrics dimension = new DisplayMetrics(); DisplayMetrics dimension = new DisplayMetrics();
windowmanager.getDefaultDisplay().getMetrics(dimension); windowmanager.getDefaultDisplay().getMetrics(dimension);
holder = this; expandLayout.getViewTreeObserver().addOnPreDrawListener(
this.expandLayout.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() { new ViewTreeObserver.OnPreDrawListener() {
@Override @Override
public boolean onPreDraw() { public boolean onPreDraw() {
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); if (expandHeight == 0) {
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
holder.expandLayout.getViewTreeObserver().removeOnPreDrawListener(this); final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
holder.expandLayout.setVisibility(View.GONE); expandLayout.measure(widthSpec, heightSpec);
holder.expandLayout.measure(widthSpec, heightSpec); expandHeight = expandLayout.getMeasuredHeight();
final int holderHeight = holder.expandLayout.getMeasuredHeight(); }
mAnimator = slideAnimator(0, holderHeight);
animY2 = ObjectAnimator.ofFloat(holder.updateImage, "translationY", holderHeight / 2);
expandLayout.getViewTreeObserver().removeOnPreDrawListener(this);
expandLayout.setVisibility(View.GONE);
mAnimator = slideAnimator(0, expandHeight);
animY2 = ObjectAnimator.ofFloat(updateImage, "translationY", expandHeight / 2);
return true; return true;
} }
}); });
mView.setOnClickListener(view -> {
if (expanded) {
collapse(holder.expandLayout);
} else {
expand(holder.expandLayout);
}
expanded = !expanded;
});
} }
private void expand(View view) { private void setExpanded(boolean expanded) {
view.setVisibility(View.VISIBLE); 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(); mAnimator.start();
animY2.start(); animY2.start();
mExpanded = true;
} }
private void collapse(View view) { private void collapse() {
int finalHeight = view.getHeight(); if (!mExpanded) return;
int finalHeight = expandLayout.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0); ValueAnimator mAnimator = slideAnimator(finalHeight, 0);
mAnimator.addListener(new Animator.AnimatorListener() { mAnimator.addListener(new Animator.AnimatorListener() {
@Override @Override
public void onAnimationEnd(Animator animator) { public void onAnimationEnd(Animator animator) {
view.setVisibility(View.GONE); expandLayout.setVisibility(View.GONE);
} }
@Override @Override
@ -198,7 +209,7 @@ public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder>
}); });
mAnimator.start(); mAnimator.start();
animY2.reverse(); animY2.reverse();
mExpanded = false;
} }
private ValueAnimator slideAnimator(int start, int end) { private ValueAnimator slideAnimator(int start, int end) {
@ -207,8 +218,7 @@ public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder>
animator.addUpdateListener(valueAnimator -> { animator.addUpdateListener(valueAnimator -> {
int value = (Integer) valueAnimator.getAnimatedValue(); int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = expandLayout ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams();
.getLayoutParams();
layoutParams.height = value; layoutParams.height = value;
expandLayout.setLayoutParams(layoutParams); expandLayout.setLayoutParams(layoutParams);
}); });