java - 如何使子片段从其父片段中刷新
问题描述
你好,我有片段,里面有两个标签,当我按下父片段中的按钮时,我需要刷新视图页面中的子片段,我用它来刷新相同的片段,它工作正常
FragmentTransaction ft1 = getFragmentManager().beginTransaction();
ft1.detach(this).attach(this).commit();
我尝试了同样的方法,而不是这个,我放了 childfragment.newInstance() 仍然没有刷新所以有什么建议吗?
解决方案
在您的子片段中创建一个界面。刷新界面
在子片段中创建一个方法以将对象分配给 RefreshInterface。初始化刷新接口(刷新接口刷新接口)
调用刷新点击里面的接口方法。
在父片段中实现子片段接口并覆盖该方法。
在 commit() 之前来自父片段的子片段 1. 为子对象创建一个对象 2. 调用 intialiseRefreshInterface(RefreshInterface refreshInterface) 3. commit() 子片段。
您的子片段将刷新 [子片段将 popBackStack() 并重新创建。
下面的示例代码将帮助您实现您的要求
父片段
父片段.java
public class OneFragment extends Fragment implements TwoFragment.RefreshInterface {
TwoFragment child;
Button launch;
FrameLayout containerLayout;
// child fragment interface object
TwoFragment.RefreshInterface refreshInterface;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
launch = view.findViewById(R.id.fragment_launch);
containerLayout = view.findViewById(R.id.fragment_container);
// RefreshInterface. this is mandatory
refreshInterface = this;
launch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchChildFragment();
}
});
return view;
}
@Override
public void refresh_fragment() {
// as we added the child fragment to backstack. we will remove the fragment from backstack and add again
if (getActivity().getSupportFragmentManager().getBackStackEntryCount() > 0){
getActivity().getSupportFragmentManager().popBackStack();
}
launchChildFragment();
}
private void launchChildFragment(){
// creating the object for child fragment
child = new TwoFragment();
// intialising the RefreshInterface object
child.intialiseRefreshInterface(refreshInterface);
// calling the child fragment
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, child).addToBackStack(null).commit();
}
}
片段父.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapplication.OneFragment">
<Button
android:layout_centerHorizontal="true"
android:id="@+id/fragment_launch"
android:text="Launch child fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_below="@id/fragment_launch"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
子片段
public class TwoFragment extends Fragment {
Button refershBtn;
RefreshInterface refreshInterface;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
refershBtn = view.findViewById(R.id.btn_refersh);
refershBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
refreshInterface.refresh_fragment();
}
});
return view;
}
public void intialiseRefreshInterface(RefreshInterface refreshInterface){
this.refreshInterface = refreshInterface;
}
public interface RefreshInterface {
void refresh_fragment();
}
}
fragment_child.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.outthinking.myapplication.TwoFragment">
<!-- TODO: Update blank fragment layout -->
<EditText
android:id="@+id/refersh_tv"
android:textSize="24sp"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="REFRESH DATA" />
<Button
android:id="@+id/btn_refersh"
android:text="refersh"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>