2017-08-30 71 views
0

我有一個活動包含兩個片段顯示爲兩個選項卡,選項卡1是片段A和選項卡2是片段B.選項卡1顯示所有項目的列表和選項卡2顯示最愛列表。當我在選項卡1中選擇一個項目並單擊「添加到收藏夾」時,此項目將被插入到SQLite數據庫中,並且選項卡2將顯示此數據庫中的數據。如何刷新片段A中的片段B?

問題是兩個選項卡會在我加入到此活動的同時加載,在我「添加到收藏夾」後,此選項將不會顯示在選項卡2中,直到我再次打開此活動爲止。無論如何,在標籤2中刷新列表後,我在標籤1中點擊「添加到收藏夾」。

我不擅長英語,希望這個問題對你很明顯。謝謝。

回答

0

你可以檢查片段是對用戶可見。你可以試試這個:

public class FavoritesTab extends Fragment { 

    @Override 
    public void setUserVisibleHint(boolean isVisibleToUser) { 
    super.setUserVisibleHint(isVisibleToUser); 
    if (isVisibleToUser) { 
     // Refresh your fragment here 
    } 
    } 
+0

正是我想要它。非常感謝,男人:) – Jacky

+0

很高興我能幫上忙。 – Orvenito

0

如何刷新片段A中的片段B?

您可以使用此link在2個片段之間進行通信。它使用可觀察模式,通過包含2個片段(片段A和片段B)的Activity進行通信。

如果你想要更簡單一些,你可以使用Event Bus,很容易實現。

0

您可以使用廣播接收器通過活動傳達兩個片段。

通過活動交流的兩個片段:

當您在片段A添加項目,您撥打的主機活性的方法,將調用在片段B的方法刷新列表。

0

有一些方法。

1.通過活動

你有片段A和片段B在您的活動。 你也有片段A和片段B.你的活動

MainActivity.java

FragmentA A; 
FragmentB B; 

private void updateB(){ 
    if(B!= null){ 
     B.refresh(); 
    } 
} 

FragmentA.java

MainActivity mainActivity; 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    mainActivity = getActivity(); 
} 
private void addFavorite(){ 
    //add item to DB 
    //reload Fragment A 
    mainActivity.updateB(); 
} 

FragmentB.java

MainActivity mainActivity; 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    mainActivity = getActivity(); 
} 

@Override 
public void onDestroyView() { 
    mActivity.unregisterReceiver(myReceiver); 
    super.onDestroyView(); 
} 
private void refresh(){ 
    ... 
} 




2.使用廣播

FragmentB.java

private MyReceiver myReceiver; 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    myReceiver = new MyReceiver(); 
    IntentFilter filter = new IntentFilter(MyReceiver.ACTION); 
    mActivity.registerReceiver(myReceiver, filter); 
} 


public class MyReceiver extends BroadcastReceiver { 
    public static final String ACTION = "com.xxxx.test"; 
    public static final String TYPE_REFRESH = "TYPE_REFRESH"; 

    @Override 
    public void onReceive(final Context context, final Intent intent) { 
     String type = intent.getStringExtra("type"); 
     if (type.equals(TYPE_REFRESH)) { 
      refresh(); 
     } 
    } 
} 

public static void sendBroadcastForRefresh(Context context) { 
    Intent intent = new Intent(); 
    intent.setAction(MyReceiver.ACTION); 
    intent.putExtra("type", TYPE_REFRESH); 
    context.sendBroadcast(intent); 
} 

FragmentA.java

private void addFavorite(){ 
    .... 
    FragmentB.sendBroadcastForRefresh(mContext); 
} 



3。重新片段

FragmentManager fManager = getSupportFragmentManager(); 
    FragmentTransaction fTransaction = fManager.beginTransaction(); 
    B= FragmentB.newInstance(); 
    fTransaction.replace(R.id.fragment_b_id, B, "B Tag"); // <-- this one will recreate your fragment 
    fTransaction.commitAllowingStateLoss(); //or fTransaction.commit(); 
0

只是再次讓你開B片段的onResume方法將啓動,這將刷新內容,每次覆蓋的onResume方法和顯示收藏夾中的onResume。 把它放在你的片段類:

@Override 
public void onResume() { 
    //refresh content 
    //show favorite items here again 
    super.onResume(); 
} 
0

你可以叫setTargetFragment()當您從A

FragmentB fragmentB = FragmentB.newInstance(); 
fragmentB.setTargetFragment(FragmentA.this, REQUEST_REFRESH_CODE); 
getFragmentManager().beginTransaction().replace(R.id.container, fragmentB).commit(); 

啓動片段B,然後當你想要刷新的B片段A,你可以調用以下代碼:

getTargetFragment().onActivityResult(
       getTargetRequestCode(), 
       Activity.RESULT_OK, 
       new Intent() 
); 

和刷新從onActivityResult()方法A片段中的片段A:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode==REQUEST_REFRESH_CODE && resultCode==Activity.RESULT_OK) { 
     refreshYourFragmentHere(); 
    } 
} 
0

解決方案1:您可以使用RxBus

解決方案2:

class YourActivity extends Activity { 

    AFragment a; 
    BFragment b; 

    public void c() { 
     //init fragment 
     a.setOnItemClickListener(b); 
    } 

} 


class AFragment extends Fragment { 

    OnItemClickListener mOnItemClickListener; 

    public void setOnItemClickListener(OnItemClickListener listener) { 
     this.mOnItemClickListener = listener; 
    } 

    public interface OnItemClickListener { 
     void onClick(int position); 
    } 
} 


class BFragment extends Fragment implements AFragment.OnItemClickListener { 

    @Override 
    public void onClick(int position) { 
     //refresh 
    } 

}