GridLayout 使用

2018-02-02  本文已影响0人  mouekz
GridLayout 网格布局,在开发中并不是很常用,今天偶然用了一下发现并不是太会用,所以作此笔记以避免后面再次踏坑。

GridLayout 兼容包:compile 'com.android.support:gridlayout-v7:23.+'

xml布局
 <com.shTelecom.view.views.ActionsView
        android:id="@+id/action_msg_listLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rowCount="3"
        android:columnCount="4"
        android:orientation="horizontal"
        android:focusable="true">
    </com.shTelecom.view.views.ActionsView>
@Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int count = getChildCount();
        if (count > lastCount +1){
            for (int i = lastCount; i < count; i++) {
                getChildAt(i).setMinimumWidth(getWidth() / getColumnCount());
                lastCount = i;
//                Log.e("onLayout" ,"-------------- lastCount: " + lastCount);
            }
        }
    }
<GridLayout
        android:id="@+id/action_msg_listLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rowCount="3"
        android:columnCount="4"
        android:orientation="horizontal"
        android:focusable="true">
        <Button
            android:layout_columnSpan="2"
            android:layout_columnWeight="1"
            android:text="1"/>
        <Button
            android:text="2"/>
        <Button
            android:text="3"/>
        <Button
            android:text="4"/>
        <Button
            android:text="5"/>
    </GridLayout>
View属性:
子View属性
//利用代码确定View的位置  
GridLayout.LayoutParams params = new GridLayout.LayoutParams();  
for(int i=1; i <= count; ++i){  
      //首先GridLayout行列,是从0开始计算的,所以我们第一个num应该在1,0的位置  
      //设置所在列的宽度和高度  
      params.width = minWidth;   
      //设置View在表格的什么位置。Spec用来设置View在表格的位置,View占几个单元格,且View在单元格的状态(是否占满单元格的行,或者占满单元格的列)  
      params.rowSpec = GridLayout.spec(i);//行的位置。  
      params.columnSpec = GridLayout.spec(0);//列的位置。  
}
对比TableLayout,GridLayout的缺点和优点

优点:

ActionsView 源码
public class ActionsView extends GridLayout implements View.OnClickListener{
    private LayoutParams layoutParams;
    private LinearLayout.LayoutParams layoutParams2;
    private LayoutInflater inflater;
    private OnClickItemListener onClickItemListener;
    private int lastCount = 0;
    private int width1 = 0;
    private String name;
    private int num;
    private int icon;

    public ActionsView(Context context) {
        super(context ,null);
    }

    public ActionsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
        inflater = LayoutInflater.from(getContext());
        layoutParams = new LayoutParams();
        layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT
                ,LinearLayout.LayoutParams.WRAP_CONTENT);
        width1 = getMeasuredWidth();
    }

    public void addOnClickItemListener(OnClickItemListener onClickListener) {
        this.onClickItemListener = onClickListener;
        int size = getChildCount();
        if (onClickItemListener != null && size > 0) {
            for (int i = 0; i < size; i++) {
                getChildAt(i).setOnClickListener(this);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int count = getChildCount();
        if (count > lastCount +1){
            for (int i = lastCount; i < count; i++) {
                getChildAt(i).setMinimumWidth(getWidth() / getColumnCount());
                lastCount = i;
//                Log.e("onLayout" ,"-------------- lastCount: " + lastCount);
            }
        }
    }

    public void addItem(String name , @DrawableRes int icon , int num) {
        this.name = name;
        this.num = num;
        this.icon = icon;
        LinearLayout layoutp = new LinearLayout(getContext());
        layoutp.setOrientation(LinearLayout.VERTICAL);
        layoutp.setGravity(Gravity.CENTER);
        layoutp.setMinimumWidth(width1 / getColumnCount());
        MsgImageView msgView = new MsgImageView(getContext());
        TextView nameView = new TextView(getContext());

        msgView.setMsgNum(num);
        msgView.setImageResource(icon);
        nameView.setText(name);
        msgView.setLayoutParams(layoutParams2);
        nameView.setLayoutParams(layoutParams2);
        layoutp.addView(msgView);
        layoutp.addView(nameView);

        layoutp.setTag(getChildCount());
        if (onClickItemListener != null) {
            layoutp.setOnClickListener(this);
        }
        addView(layoutp);
    }

    @Override
    public void onClick(View view) {
        if (this.onClickItemListener != null) {
            onClickItemListener.onItem((Integer) view.getTag());
        }
    }

    public interface OnClickItemListener {
        void onItem(int position);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读