2014-06-12 95 views
0

我有一個有兩個片段的活動。 活動接收這兩個片段的廣播事件。 一個片段具有圖像按鈕和文本視圖。點擊圖像按鈕時,會向服務器發送一個事件,服務器會回覆實況廣播事件。更新來自活動的片段

我們會收到活動的響應,我需要更新片段的UI(需要與其他圖像來改變圖像按鈕)

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.single_window_lock, container, false); 
     updateUI(view); 

     return view; 
    } 

    public void updateUI(View view){ 

     String lockName; 
     final String lockState; 
     final boolean state; 

     final ImageButton singleLockImage = (ImageButton)view.findViewById(R.id.single_lock_image); 
     final TextView lockNameText = (TextView)view.findViewById(R.id.single_lock_name); 
     final TextView lockStateText = (TextView)view.findViewById(R.id.single_lock_state); 
     final ProgressBar progress = (ProgressBar)view.findViewById(R.id.singleLockProgress); 

      doorLock = LockState.getValue(); 

     lockName = doorLock.getName(); 
     if (doorLock.isLocked()) { 
      lockState = getActivity().getString(R.string.door_locks_locked); 
      singleLockImage.setImageResource(R.drawable.doorlocks_single_locked); 
      state = true; 
     } else { 
      lockState = getActivity().getString(R.string.door_locks_unlocked); 
      singleLockImage.setImageResource(R.drawable.doorlocks_single_unlocked); 
      state = false; 
     } 

     lockNameText.setText(lockName); 
     lockStateText.setText(lockState); 
     singleLockImage.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 

         getActivity().changeState(state); 
        } 
       } 
     ); 

    } 

我想打電話給updateUI,這將讓新從Activity中接收到廣播事件後保存的緩存狀態,但我不知道如何通過(查看)

回答

0

改爲使用FragmentActivity。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.news_articles); 

     // Create an instance of ExampleFragment 
     TestFragment0 firstFragment = new TestFragment0(); 

     // In case this activity was started with special instructions from an Intent, 
     // pass the Intent's extras to the fragment as arguments 
     firstFragment.setArguments(getIntent().getExtras()); 

     // Add the fragment to the 'fragment_container' FrameLayout 
     getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit(); 
} 
在FragmentActivity

的片段:

在FragmentActivity

TestFragment0 firstFragment0 = new TestFragment0(); 
firstFragment0.setArguments(getIntent().getExtras()); 
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,firstFragment0).commit(); 
+0

活動的onCreate我打電話了......的BeginTransaction難道我必須再次調用它,當有更新的形式廣播接收機? – user3722531

+0

當再次調用片段時,即使設置了可見性,進度條仍然可見 – user3722531