Created
July 14, 2021 07:44
-
-
Save puke3615/46bf59b4826af685dcb2c1fa230098e9 to your computer and use it in GitHub Desktop.
LimitSizeRecyclerView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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