mirror of https://github.com/ThmmyNoLife/mTHMMY
Thodoris1999
6 years ago
7 changed files with 126 additions and 22 deletions
@ -0,0 +1,55 @@ |
|||
package gr.thmmy.mthmmy.utils; |
|||
|
|||
import android.content.Context; |
|||
import android.content.res.TypedArray; |
|||
import android.util.AttributeSet; |
|||
import android.widget.GridLayout; |
|||
import gr.thmmy.mthmmy.R; |
|||
|
|||
public class AutoFitGridLayout extends GridLayout { |
|||
private int columnWidth; |
|||
private int defaultColumnCount; |
|||
|
|||
public AutoFitGridLayout(Context context) { |
|||
super(context); |
|||
init(context, null, 0); |
|||
} |
|||
|
|||
public AutoFitGridLayout(Context context, AttributeSet attrs) { |
|||
super(context, attrs); |
|||
init(context, attrs, 0); |
|||
} |
|||
|
|||
public AutoFitGridLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
|||
super(context, attrs, defStyleAttr); |
|||
init(context, attrs, defStyleAttr); |
|||
} |
|||
|
|||
public void init(Context context, AttributeSet attrs, int defStyleAttr) { |
|||
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoFitGridLayout, 0, defStyleAttr); |
|||
try { |
|||
columnWidth = a.getDimensionPixelSize(R.styleable.AutoFitGridLayout_columnWidth, 0); |
|||
|
|||
int[] set = {android.R.attr.columnCount}; |
|||
a = context.obtainStyledAttributes(attrs, set, 0, defStyleAttr); |
|||
defaultColumnCount = a.getInt(0, 6); |
|||
} finally { |
|||
a.recycle(); |
|||
} |
|||
setColumnCount(1); |
|||
} |
|||
|
|||
@Override |
|||
protected void onMeasure(int widthSpec, int heightSpec) { |
|||
super.onMeasure(widthSpec, heightSpec); |
|||
|
|||
int width = MeasureSpec.getSize(widthSpec); |
|||
if (columnWidth > 0 && width > 0) { |
|||
int totalSpace = width - getPaddingRight() - getPaddingLeft(); |
|||
int columnCount = Math.max(1, totalSpace / columnWidth); |
|||
setColumnCount(columnCount); |
|||
} else { |
|||
setColumnCount(defaultColumnCount); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,6 @@ |
|||
<vector android:height="36dp" |
|||
android:viewportHeight="24.0" android:viewportWidth="24.0" |
|||
android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android"> |
|||
<path android:fillAlpha=".36" android:fillColor="@color/colorAccent" android:pathData="M0,20h24v4H0z"/> |
|||
<path android:fillColor="#FF4B4B4B" android:pathData="M11,3L5.5,17h2.25l1.12,-3h6.25l1.12,3h2.25L13,3h-2zM9.62,12L12,5.67 14.38,12L9.62,12z"/> |
|||
</vector> |
@ -0,0 +1,5 @@ |
|||
<vector android:height="36dp" android:tint="#4B4B4B" |
|||
android:viewportHeight="24.0" android:viewportWidth="24.0" |
|||
android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android"> |
|||
<path android:fillColor="#FF000000" android:pathData="M9,4v3h5v12h3L17,7h5L22,4L9,4zM3,12h3v7h3v-7h3L12,9L3,9v3z"/> |
|||
</vector> |
@ -0,0 +1,5 @@ |
|||
<vector android:height="36dp" android:tint="#4B4B4B" |
|||
android:viewportHeight="24.0" android:viewportWidth="24.0" |
|||
android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android"> |
|||
<path android:fillColor="#FF000000" android:pathData="M18,4H6v2l6.5,6L6,18v2h12v-3h-7l5,-5 -5,-5h7z"/> |
|||
</vector> |
@ -0,0 +1,6 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<resources> |
|||
<declare-styleable name="AutoFitGridLayout" > |
|||
<attr name="columnWidth" format="dimension" /> |
|||
</declare-styleable> |
|||
</resources> |
Loading…
Reference in new issue