2015-07-19 89 views
0

我在圖像點擊處理器上有一些代碼來移動被點擊的圖像旁邊的「選定打勾」圖像。此代碼工作正常,但切換到新片段後,圖像不再移動。LayoutParams在切換片段後不更新

我相信佈局參數在調用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

我在 之前,這個問題只是做的LayoutParams的一個新實例,你在XML具有的屬性設置爲它編程,以便例如

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 

,比

Selector.setLayoutParams(params); 

希望它有幫助

相關問題