在listview中创建布局中的动态更改并不起作用

时间:2014-04-04 12:40:08

标签: android android-layout relativelayout

在我的项目中,listview行项目视图中有ListView有relativeLayout,该位置根据右侧的可用组件动态变化...

我的问题是,当listview加载它看起来很完美,因为我滚动列表视图它向右移动,我不知道为什么会发生这种情况? 我的代码如下:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.relLayout.getLayoutParams();
                if(holder.weekOfYear.getVisibility() != View.VISIBLE)
                {
                    if(holder.moonImage.getVisibility() != View.VISIBLE)
                    {
                        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

                    }
                    else
                    {
                        params.addRule(RelativeLayout.LEFT_OF,R.id.moon_image);

                    }
                }
                else
                {
                    params.addRule(RelativeLayout.LEFT_OF , weekOfYear);

                }
                holder.relLayout.setLayoutParams(params);

1 个答案:

答案 0 :(得分:0)

这与ListView的回收属性有关。您需要每次都重置视图的位置。试试这个:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.relLayout.getLayoutParams();
            if(holder.weekOfYear.getVisibility() != View.VISIBLE)
            {
                if(holder.moonImage.getVisibility() != View.VISIBLE)
                {
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

                }
                else
                {
                    params.addRule(RelativeLayout.LEFT_OF,R.id.moon_image);

                }
            }
            else
            {
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.addRule(RelativeLayout.LEFT_OF , weekOfYear);

            }
            holder.relLayout.setLayoutParams(params);