我在图片点击处理程序上有一些代码可以移动一个"选择的刻度线"单击图像旁边的图像。这段代码工作正常,但在我切换到新片段后,图片不再移动。
我相信布局参数在调用SetLayoutParams时不会立即更新,并且这发生在某个地方,但我不明白为什么在重新加载fragement时会停止发生,因为它被调用的代码相同。
public void Item_Click(View view){
ImageView button = (ImageView) view;
Selector = (ImageView) findViewById(getResources().getIdentifier("Selector_Connected, "id", this.getPackageName()));
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)Selector.getLayoutParams(); //new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_TOP, button.getId());
Selector.setLayoutParams(lp);
Selector.setVisibility(View.VISIBLE);
}
片段由下一个/上一个按钮交换
bundle.putInt("answer",current_survey.get_question1() );
QuestionOne Q1Frag = new QuestionOne();
Q1Frag.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, Q1Frag).commit();
当加载新片段时,这也会根据传递给它的信息移动选择器。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.questionone, container, false);
Selector = (ImageView) view.findViewById(R.id.Selector_Connected);
int answer = getArguments().getInt("answer");
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)Selector.getLayoutParams(); // new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// lp.addRule(RelativeLayout.LEFT_OF, button.getId());
ImageView button = (ImageView) view.findViewById(getResources().getIdentifier("Connected_" + i, "id", view.getContext ().getPackageName()));
lp.addRule(RelativeLayout.ALIGN_TOP, button.getId());
Selector.setLayoutParams(lp);
return view;
}
如上所述,所有这些代码都可以正常工作,直到移动到新的片段并再次向后移动,代码仍然被调用,但选择器永远不会移动!
感谢任何帮助。
答案 0 :(得分:0)
之前我遇到过这个问题 只需创建一个LayoutParams的新实例,并以编程方式将xml中的所有属性设置为它,例如
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
而不是
Selector.setLayoutParams(params);
希望有所帮助