How to update linearlayout child view programmatically in android?

拈花ヽ惹草 提交于 2019-12-11 11:52:57

问题


I have created linearylayout programatically. where it has multiple rows created having white background. Now if I click on any row then only that row's imageview icon should be changed(should be green) and other row's imageview background should be changed to white. Is there any substitude of notifyDataSetChanged for linearlayout in android. below is my code.

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);
                    }

                }
            });

        }

回答1:


No, there is no substitute for notifyDataSetChanged(), you have to manually inform your ImageViews of that change.

You could iterate like this, as soon as you have a change:

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(...);
}

For better performance you may also create an HashMap with your ImageViews and later access it to change some of them as you see fit.




回答2:


I believe what you're looking for is View.invalidate() method. It tells the application that this view is no longer correct, and it will be redrawn when possible. Call this at the end of the onClick method.



来源:https://stackoverflow.com/questions/28108505/how-to-update-linearlayout-child-view-programmatically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!