关于LinnerLayout动态添加的控件 并且给子控件动态设置Id 、点击的问题

以前也没写过这种恶心的东西,前二天刚好有个功能要这么搞    大概场景就是addview里面的那个控件还要加checkbox   分享一下  以免自己忘记。。。。。。

(废话就不多说了  说几个关键的地方)


relativeLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.phone_item, null);   


        boxs1 = new ArrayList<Integer>();  //用这个boxsl保存所有的checkBox的ID


        //初始化内容
        int id = 1;
        for (int i=0;i<list.size();i++) {


            relativeLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.phone_item, null);


            relativeLayout.setId(View.generateViewId()+id);


            boxs1.add(relativeLayout.getId());


            id++;
            TextView name = (TextView) relativeLayout.findViewById(R.id.name);


                name.setText(list.get(i).getName());  //list是通过接口获得的数据源
                final int pos = i;
                mainLinerLayout.addView(relativeLayout);   //mainLinerLayout 主界面的LinnerLayout;


                mainLinerLayout.getChildAt(i).setOnClickListener(new View.OnClickListener() {


                    @Override
                    public void onClick(View v) {


                        UI.showToast(GongHdActivity.this, "点击了" + list.get(pos).getName());


//                        piXuanCheck();  // 点击名字选中单选框功能
                    }
                });
        }

只要有了这个boxsl  剩下的事情都变的简单了,通过循环findViewByID  获取到控件   得到checkbox的选中状态 跟 下标,这个坎就算迈过去了。。。。       




猜你喜欢

转载自blog.csdn.net/android410223sun/article/details/72677358