【问题标题】:How to update linearlayout child view programmatically in android?如何在android中以编程方式更新linearlayout子视图?
【发布时间】:2015-01-23 11:05:07
【问题描述】:

我以编程方式创建了线性布局。它有多行创建具有白色背景。现在,如果我单击任何行,则只应更改该行的 imageview 图标(应为绿色),而其他行的 imageview 背景应更改为白色。 android 中的线性布局是否有任何 notifyDataSetChanged 的​​替代品。下面是我的代码。

for (int i = 0; i <= obj_tribe_info.al_ethnicity_secondary.size(); i++) {

            final int position = i;

            al_eth_bool.add(false);

            final LinearLayout ll_values = new LinearLayout(_activity);
            ll_values.setOrientation(LinearLayout.HORIZONTAL);
            ll_values.setGravity(Gravity.CENTER_VERTICAL);
            LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            ll_values.setLayoutParams(LLParams);
            ll_values.setBackgroundResource(R.drawable.my_tribe_box_top);

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(50, 0, 0, 0);

            final ImageView iv_icon = new ImageView(_activity);
            if (al_eth_bool.get(position)) {
                iv_icon.setBackgroundResource(R.drawable.notify_onn);
            } else {
                iv_icon.setBackgroundResource(R.drawable.notify_off);
            }
            iv_icon.setLayoutParams(layoutParams);
            ll_values.addView(iv_icon);

            TextView tv_value = new TextView(_activity);
            tv_value.setTextColor(_activity.getResources().getColor(
                    R.color.black));
            tv_value.setLayoutParams(layoutParams);
            if (i == 0) {
                tv_value.setText(obj_tribe_info.str_ethnicity_primary);
            } else {
                tv_value.setText(obj_tribe_info.al_ethnicity_secondary
                        .get(i - 1).str_ethnicity_secondary);
            }
            ll_values.addView(tv_value);

            ll_ethnicity.post(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    ll_ethnicity.addView(ll_values);
                }
            });

            ll_values.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (al_eth_bool.get(position)) {
                        iv_icon.setBackgroundResource(R.drawable.notify_off);
                        al_eth_bool.set(position, false);
                    } else {
                        iv_icon.setBackgroundResource(R.drawable.notify_onn);
                        al_eth_bool.set(position, true);
                    }

                }
            });

        }

【问题讨论】:

    标签: android android-linearlayout updates programmatically-created


    【解决方案1】:

    不,notifyDataSetChanged() 没有替代品,您必须手动通知您的 ImageViews 该更改。

    只要你有变化,你就可以像这样迭代:

    for(int i= 0; i < ll_ethnicity.getChildCount(); i++){
        ImageView iv = (ImageView) ((ViewGroup)ll_ethnicity.getChildAt(i)).getChildAt(0);
        // new background because something has changed
        // check if it's not the imageView you just clicked because you don't want to change its background
        iv.setBackground(...);
    }
    

    为了获得更好的性能,您还可以使用您的 ImageView 创建一个 HashMap,然后访问它以根据需要更改其中的一些。

    【讨论】:

    • 它在 getChildAt(0) 处给出错误;在 ImageView iv = (ImageView) ll_ethnicity.getChildAt(i).getChildAt(0);
    • 是的,我的例子非常初级。您需要检查/将 ll_ethnicity.getChildAt(i) 转换为 ViewGroup 或 LinearLayout。我会编辑我的帖子
    【解决方案2】:

    我相信您正在寻找的是View.invalidate() 方法。它告诉应用程序该视图不再正确,并且将在可能的情况下重新绘制它。在 onClick 方法结束时调用它。

    【讨论】:

      猜你喜欢
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多