Use more general solution

This commit is contained in:
topjohnwu 2018-12-31 01:50:41 +08:00
parent bf7d6ddcb2
commit 76491cbb31
3 changed files with 150 additions and 76 deletions

View File

@ -1,75 +0,0 @@
package com.topjohnwu.magisk.adapters;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.topjohnwu.magisk.R;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
public class MagiskLogAdapter extends RecyclerView.Adapter<MagiskLogAdapter.ViewHolder> {
private List<String> mList;
private int txtWidth = -1;
public MagiskLogAdapter(List<String> list) {
mList = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_magisk_log, parent, false);
ViewHolder vh = new ViewHolder(v);
if (txtWidth < 0) {
int max = 0;
String maxStr = "";
for (String s : mList) {
int len = s.length();
if (len > max) {
max = len;
maxStr = s;
}
}
vh.txt.setText(maxStr);
vh.txt.measure(0, 0);
txtWidth = vh.txt.getMeasuredWidth();
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) parent.getContext()).getWindowManager()
.getDefaultDisplay().getMetrics(displayMetrics);
if (txtWidth < displayMetrics.widthPixels)
txtWidth = displayMetrics.widthPixels;
}
vh.txt.getLayoutParams().width = txtWidth;
return vh;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.txt.setText(mList.get(position));
}
@Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.txt) TextView txt;
public ViewHolder(@NonNull View v) {
super(v);
new MagiskLogAdapter$ViewHolder_ViewBinding(this, itemView);
}
}
}

View File

@ -0,0 +1,100 @@
package com.topjohnwu.magisk.adapters;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public abstract class StringListAdapter<VH extends StringListAdapter.ViewHolder>
extends RecyclerView.Adapter<VH> {
private RecyclerView rv;
private boolean dynamic;
private int screenWidth;
private int txtWidth = -1;
protected List<String> mList;
public StringListAdapter(List<String> list) {
this(list, false);
}
public StringListAdapter(List<String> list, boolean isDynamic) {
mList = list;
dynamic = isDynamic;
}
@NonNull
@Override
public final VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(itemLayoutRes(), parent, false);
VH vh = createViewHolder(v);
if (txtWidth < 0)
onUpdateTextWidth(vh);
return vh;
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
holder.txt.setText(mList.get(position));
holder.txt.getLayoutParams().width = txtWidth;
if (dynamic)
onUpdateTextWidth(holder);
}
protected void onUpdateTextWidth(VH vh) {
if (txtWidth < 0) {
txtWidth = screenWidth;
} else {
vh.txt.measure(0, 0);
int width = vh.txt.getMeasuredWidth();
if (width > txtWidth) {
txtWidth = width;
vh.txt.getLayoutParams().width = txtWidth;
rv.requestLayout();
}
}
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView rv) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) rv.getContext()).getWindowManager()
.getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels;
this.rv = rv;
}
@Override
public final int getItemCount() {
return mList.size();
}
@LayoutRes
protected abstract int itemLayoutRes();
@NonNull
public abstract VH createViewHolder(@NonNull View v);
public static abstract class ViewHolder extends RecyclerView.ViewHolder {
public TextView txt;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txt = itemView.findViewById(textViewResId());
}
@IdRes
protected abstract int textViewResId();
}
}

View File

@ -13,7 +13,7 @@ import com.google.android.material.snackbar.Snackbar;
import com.topjohnwu.core.Const;
import com.topjohnwu.core.utils.Utils;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.adapters.MagiskLogAdapter;
import com.topjohnwu.magisk.adapters.StringListAdapter;
import com.topjohnwu.magisk.components.BaseFragment;
import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.superuser.Shell;
@ -21,7 +21,9 @@ import com.topjohnwu.superuser.Shell;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
@ -101,4 +103,51 @@ public class MagiskLogFragment extends BaseFragment {
Shell.su("echo -n > " + Const.MAGISK_LOG).submit();
SnackbarMaker.make(rv, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
}
private static class MagiskLogAdapter extends StringListAdapter<MagiskLogAdapter.ViewHolder> {
MagiskLogAdapter(List<String> list) {
super(list);
}
@Override
protected int itemLayoutRes() {
return R.layout.list_item_magisk_log;
}
@NonNull
@Override
public ViewHolder createViewHolder(@NonNull View v) {
return new ViewHolder(v);
}
@Override
protected void onUpdateTextWidth(ViewHolder holder) {
super.onUpdateTextWidth(holder);
// Find the longest string and update accordingly
int max = 0;
String maxStr = "";
for (String s : mList) {
int len = s.length();
if (len > max) {
max = len;
maxStr = s;
}
}
holder.txt.setText(maxStr);
super.onUpdateTextWidth(holder);
}
public class ViewHolder extends StringListAdapter.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
@Override
protected int textViewResId() {
return R.id.txt;
}
}
}
}