Improve StringListAdapter

This commit is contained in:
topjohnwu 2018-12-31 02:46:50 +08:00
parent 76491cbb31
commit 07d1a20f3d

View File

@ -21,6 +21,7 @@ public abstract class StringListAdapter<VH extends StringListAdapter.ViewHolder>
private boolean dynamic; private boolean dynamic;
private int screenWidth; private int screenWidth;
private int txtWidth = -1; private int txtWidth = -1;
private int padding;
protected List<String> mList; protected List<String> mList;
@ -53,16 +54,17 @@ public abstract class StringListAdapter<VH extends StringListAdapter.ViewHolder>
protected void onUpdateTextWidth(VH vh) { protected void onUpdateTextWidth(VH vh) {
if (txtWidth < 0) { if (txtWidth < 0) {
txtWidth = screenWidth; txtWidth = screenWidth - padding;
} else { } else {
vh.txt.measure(0, 0); vh.txt.measure(0, 0);
int width = vh.txt.getMeasuredWidth(); int width = vh.txt.getMeasuredWidth();
if (width > txtWidth) { if (width > txtWidth) {
txtWidth = width; txtWidth = width;
vh.txt.getLayoutParams().width = txtWidth; vh.txt.getLayoutParams().width = txtWidth;
rv.requestLayout();
} }
} }
if (rv.getWidth() != txtWidth + padding)
rv.requestLayout();
} }
@Override @Override
@ -71,6 +73,7 @@ public abstract class StringListAdapter<VH extends StringListAdapter.ViewHolder>
((Activity) rv.getContext()).getWindowManager() ((Activity) rv.getContext()).getWindowManager()
.getDefaultDisplay().getMetrics(displayMetrics); .getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels; screenWidth = displayMetrics.widthPixels;
padding = rv.getPaddingStart() + rv.getPaddingEnd();
this.rv = rv; this.rv = rv;
} }