简体   繁体   English

刷新或重绘片段视图

[英]Refresh or redraw Fragment's View

I've searched around but have been unable to find a solution to this problem. 我到处搜索,但无法找到解决该问题的方法。 I have a Fragment that is being shown and when you press a button it opens a new Activity on top of that. 我正在显示一个片段,当您按下按钮时,它会在其上方打开一个新的活动。 Then when that Activity is finished it returns some intent data back to the Fragment/Parent Activity. 然后,当该活动完成时,它将一些意图数据返回给“片段/父项”活动。 That data will sometimes change a TextView or add a button to the Fragment. 该数据有时会更改TextView或向Fragment添加一个按钮。 However, I haven't been able to get the Fragment's view to update without re-adding the Fragment to the backstack (popBackStack then replace or add). 但是,如果不将Fragment重新添加到Backstack(popBackStack然后替换或添加),就无法获取Fragment的视图进行更新。

A couple things I've tried is to call invalidate() and requestLayout() on the Fragment's base layout but the only way to refresh the view seems to have it call the onCreateView() again by removing and re-adding it. 我尝试过的几件事是在Fragment的基本布局上调用invalidate()requestLayout() ,但是刷新视图的唯一方法似乎是通过删除并重新添加它再次调用onCreateView() Is there a way to specify the Fragment's "content view" without reloading it? 有没有一种方法可以指定片段的“内容视图”而无需重新加载它?

Parent Activity code: 家长活动代码:

// Used to retrieve the data from the first Activity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        // Try to update the Fragment's view here
        mMyFragment.refreshView(data.getString(NEW_BUTTON_TEXT));
    }
}

Fragment code (MyFragment): 片段代码(MyFragment):

private View mBaseLayout;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mBaseLayout = inflater.inflate(R.layout.my_layout, container, false);
    ...
    return mBaseLayout;
}

public void refreshView(String newButtonText) {
    Button button = mBaseLayout.findViewById(R.id.new_button);
    button.setText(newButtonText);
    button.setVisibility(View.VISIBLE);
    // TODO Reset the Fragment's view with the update mBaseLayout view
    // ? ? ?
    // Tried mBaseLayout.invalidate() and mBaseLayout.requestLayout() here 
}

Any thoughts on this would be really helpful, thanks! 对此有任何想法将非常有帮助,谢谢!

Since there doesn't appear to be a way to force the Fragment's view to be refreshed I ended up just detaching and re-attaching the fragment so the onCreateView() is called again. 由于似乎没有一种方法可以强制刷新Fragment的视图,所以我最终只是分离并重新附加了片段,因此再次调用了onCreateView() This seems to work just how I wanted it to. 这似乎按我想要的方式工作。 I used this solution as a starting point but needed to add commitAllowingStateLoss() to prevent a crash: https://stackoverflow.com/a/19409266/1234752 . 我以此解决方案为起点,但需要添加commitAllowingStateLoss()来防止崩溃: https : commitAllowingStateLoss() Allowing state loss is fine here because the Fragment is just reused and the data is still contained in it. 在这里允许状态丢失是很好的,因为Fragment仅被重用并且数据仍然包含在其中。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        myFragment.updateData(data.getString(NEW_BUTTON_TEXT));
        // Need to use commitAllowingStateLoss() to prevent an IllegalStateException 
        // from being thrown
        getSupportFragmentManager()
            .beginTransaction()
            .detach(mContactDetailsFragment)
            .attach(mContactDetailsFragment)
            .commitAllowingStateLoss();
    }
}
Is there a way to specify the Fragment's "content view" without reloading it?

No, That is because fragments are getting redrawn each time your activity changes, so when you open and finished an activity you fragment oncreateView will be called thus redrawing it each time. 不,那是因为每次活动更改时都会重新绘制片段,因此当您打开并完成一个活动时,oncreateView上的片段将被调用,从而每次都将其重新绘制。

solution: 解:

You can make your fragment as an inner class of your activity and set a FLAG in your onActivityResult to check that it is needed to refresh. 您可以将片段作为活动的内部类 ,并在onActivityResult设置FLAG以检查是否需要刷新。

if (resultCode == Activity.RESULT_OK) {
    isRefresh = true;
    stringData = data.getString(NEW_BUTTON_TEXT));
}

and when the onCreateView of the fragment gets called 当片段的onCreateView被调用时

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mBaseLayout = inflater.inflate(R.layout.my_layout, container, false);
...
if(isRefresh)
   refreshView(stringData)
return mBaseLayout;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM