使用LinearLayout模拟GridView

自定义Style
<declare-styleable name="mGridView">
    <attr name="col" format="integer"></attr>
</declare-styleable>

使用LinearLayout实现GridView
public class MGridView extends LinearLayout {

    private Context context;
    private List<View> viewList;//GridView中的所有子View
    private int col;//列数
    private int row;//行数

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

    public MGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        viewList = new ArrayList<>();
        this.setOrientation(VERTICAL);
        this.setGravity(Gravity.CENTER_VERTICAL);

        //xml中取出定义的列数赋值给col,默认为1        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.mGridView);
        col = typedArray.getInteger(R.styleable.mGridView_col, 1);
    }


    //GridView中添加View
    public void addChild(View view) {

        viewList.add(view);
        int size = viewList.size();
        row = (int) Math.ceil(((double) size) / ((double) col));

        //应该画到哪一列
        int nCol = (size - 1) % col;

        //是否需要添加新行
        boolean needAdd = false;
        for (int i = 1; i <= row; i++) {

            //只画最新的一行
            if (i == row) {
                LinearLayout layout = (LinearLayout) this.getChildAt(i - 1);
                if (layout == null) {
                    needAdd = true;
                    layout = new LinearLayout(context);
                    layout.setOrientation(HORIZONTAL);
                    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.weight = 1;
                    layout.setLayoutParams(params);
                }

                for (int j = 0; j < col; j++) {

                    //只画最新的一行中添加到的那一列
                    if (j == nCol) {

                        //添加列
                        layout.addView(view);
                    }
                }

                //添加行
                if (needAdd) {
                    addView(layout);
                }
            }
        }
    }
}

布局
<com.example.venn.mygridview.views.MGridView
    android:id="@+id/m_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    venn:col="5"
    >

</com.example.venn.mygridview.views.MGridView>

MainActivity:
public class MainActivity extends Activity {

    private MGridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGridView = (MGridView) this.findViewById(R.id.m_grid);

        for (int i = 0; i < 10; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.mipmap.ic_launcher);
            mGridView.addChild(imageView);

            TextView textView = new TextView(this);
            textView.setText("哈哈哈哈");
            mGridView.addChild(textView);

        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值