gpt4 book ai didi

java - 单击时 fragment 不会自动更新

转载 作者:行者123 更新时间:2023-12-02 03:17:30 25 4
gpt4 key购买 nike

我的操作栏中有一个 fragment ,如果用户单击 fragment 按钮,则页面应自动更新/刷新(如果其中有任何内容)。我面临的问题是, fragment 第一次不会自动更新,但当我第二次按下它时会更新并刷新。

这是流程:

选择 fragment 1 -> 不更新/刷新 -> 返回并从操作栏中选择另一个 fragment 2 -> 再次选择 fragment 1 -> 更新 fragment 1 的 UI。

我在异步任务的 onPostExecute 中从 MainActivity 调用 fragment 1。

主要 Activity ,

private final FragmentOne f1 = new FragmentOne();

@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
f1.updateUI();
}

我的 fragment 类中有这段代码。

// Updates the fragment-1 UI
public void updateUI() {
if (isAdded()) {
String currentDatabaseName = null;
hideAllTables();
hideWarningLabelsAndHeaders();
progressBarIndicator.setVisibility(View.VISIBLE);

if (null != getUserIdentifier()) {
currentDatabaseName = Utils.buildUserDataFromFli(getUserIdentifier());
}
if (null != currentDatabaseName) {
shopData = new ShopDao(ShopApplication.getLoggedInUser(), Utils.getDatabaseIndexMap());
transcationData = new TranscationDao(ShopApplication.getLoggedInUser(), Utils.getDatabaseIndexMap());
}
configureUserDependentFetchers(getUserIdentifier());
}
}

最佳答案

试试这个,也许对你有用;)

关于 Activity :

@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
//to send broadcast for refresh
Intent intentRefresh = new Intent("com.yourapp.REFRESH_DATA");
LocalBroadcastManager.getInstance(YourActivity.this).sendBroadcast(intentRefresh);
}

在您的 fragment 上:

private final BroadcastReceiver broadcastReceiverMap = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.yourapp.REFRESH_DATA")) {
//put here code for refresh
}
}
};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);

//register your LocalBroadcatManager
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.yourapp.REFRESH_DATA");
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
broadcastReceiverMap, intentFilter);
return view;
}

关于java - 单击时 fragment 不会自动更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40078392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com