Skip to content

Instantly share code, notes, and snippets.

@puke3615
Created July 14, 2021 07:44
Show Gist options
  • Save puke3615/46bf59b4826af685dcb2c1fa230098e9 to your computer and use it in GitHub Desktop.
Save puke3615/46bf59b4826af685dcb2c1fa230098e9 to your computer and use it in GitHub Desktop.
LimitSizeRecyclerView
import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
/**
* @author puke
* @version 2021/5/13
*/
public class LimitSizeRecyclerView extends RecyclerView {
private int maxWidth;
private int maxHeight;
public LimitSizeRecyclerView(@NonNull Context context) {
super(context);
}
public LimitSizeRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public LimitSizeRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setMaxWidth(int maxWidth) {
this.maxWidth = maxWidth;
}
public void setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
if (maxWidth > 0) {
int width = MeasureSpec.getSize(widthSpec);
if (width > maxWidth) {
widthSpec = MeasureSpec.makeMeasureSpec(
maxWidth, MeasureSpec.getMode(widthSpec));
}
}
if (maxHeight > 0) {
int height = MeasureSpec.getSize(heightSpec);
if (height > maxHeight) {
heightSpec = MeasureSpec.makeMeasureSpec(
maxHeight, MeasureSpec.getMode(heightSpec));
}
}
super.onMeasure(widthSpec, heightSpec);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment