簡體   English   中英

片段不會在點擊時自動更新

[英]Fragment doesn't automatically update on clicking

我的操作欄中有一個片段,如果用戶單擊片段按鈕,則頁面中如果有任何內容,應自動更新/刷新。 我面臨的問題是,片段不會在第一次自動更新,而是在我第二次按下時更新和刷新。

這是流程:

選擇片段1->不更新/刷新->返回並從操作欄中選擇另一個片段2->再次選擇片段1->更新片段1的用戶界面。

我在異步任務的onPostExecute中從MainActivity調用片段1。

主要活動,

private final FragmentOne f1 = new FragmentOne();

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

我的片段類中有這段代碼。

// 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());
    }
}

試試這個,也許為你工作;)

活動中:

@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);
    }

在您的片段上:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM