2017-08-30 73 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 
    } 

}