刘 争

疯狂了你就赢了,疲倦了你就输了

TableLayout实现gridview嵌套gridview效果

这里写图片描述这里写图片描述

自定义linearlayout

public class CurrenPositionView extends LinearLayout {
    //每行item个数
    private final int ROW_NUM = 5;
    private TextView titleView;
    private ZhaoPin data;
    private TableLayout tableLayout;
    private LayoutInflater layoutInflater;
    private int viewId;

    public CurrenPositionView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CurrenPositionView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CurrenPositionView(Context context) {
        super(context);
    }

    public ZhaoPin getData() {
        return data;
    }

    public void setData(ZhaoPin data) {
        this.data = data;
    }

    private void initView() {
        layoutInflater = LayoutInflater.from(getContext());
        View rootView = layoutInflater.inflate(R.layout.pulldown_view, this);
        titleView = (TextView) rootView.findViewById(R.id.job_name_text);
        tableLayout = (TableLayout) rootView.findViewById(R.id.job_name_table);
        tableLayout.setStretchAllColumns(true);
        setTitleStr(data.getName());
        //初始化表格数据
        int h = data.getJobtype().size() / ROW_NUM;
        int len = data.getJobtype().size() % ROW_NUM;
        int location = 0;
        if (len != 0)
            h += 1;
        for (int n = 1; n <= h; n++) {
            TableRow row = new TableRow(getContext());
            int length = n == h ? len : ROW_NUM;
            for (int m = 0; m < length; m++) {
                //mview外面一层数据
                MyTextView mview = (MyTextView) layoutInflater.inflate(R.layout.table_row_item, null);
                mview.setText(data.getJobtype().get(location).getName());
                mview.setTag("" + viewId + location);
                mview.setGravity(Gravity.CENTER);
                mview.setTextColor(Color.rgb(0,0,0));
                mview.setTextSize(30);
                mview.setOnClickListener(((MainActivity) getContext()).new OnItemClick(data, location, n, tableLayout, mview));
                row.addView(mview);
                location++;
            }
            // 每一列 tablerow 增加一个下拉的layout
            View view = layoutInflater.inflate(R.layout.expand_item, null);
            view.setTag(n);
            view.setVisibility(View.GONE);
            tableLayout.addView(row);
            tableLayout.addView(view);
        }
    }

    public void init() {
        initView();
    }

    public void setTitleStr(CharSequence text) {
        titleView.setText(text);
    }

    public int getViewId() {
        return viewId;
    }

    public void setViewId(int viewId) {
        this.viewId = viewId;
    }
}

不多说,附源码:

Demo下载,点击此处

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bryant_liu24/article/details/77679465
文章标签: gridview textview class
个人分类: android开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭