从片段中显示动作栏中的进度条未显示

时间:2015-05-04 09:46:59

标签: android android-fragments progress-bar navigation-drawer

我有一个带导航抽屉的Android应用程序。所以我只有一个活动和许多片段,每个菜单项至少有一个。我的问题是我无法在操作栏中显示进度条而无需在每个片段的特定菜单中指定进度条(假设您希望根据您显示的哪个片段具有不同的操作栏项目最后指定了很多菜单)。

是不是有办法让所有人都可以使用全局菜单,然后只为每个片段添加我想要的菜单项?

在我的MainActivity中,我有:

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

在我的片段中:

getActivity().setProgressBarIndeterminateVisibility(true);

getActivity().setProgressBarIndeterminateVisibility(false);

菜单如下所示:

<item
    android:id="@+id/action_search"
    android:title="@string/action_search"
    android:orderInCategory="100"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="always"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

<item
    android:id="@+id/action_progress"
    android:title="Loading..."
    android:visible="false"
    android:orderInCategory="200"
    app:showAsAction="always"
    app:actionLayout="@layout/progressbar_layout" />

编辑:

我有一个MVP模型,所以我正在调用showProgress:

public class DashboardFragment extends Fragment implements DashboardView {

private DashboardPresenterImpl presenter;
private RelativeLayout incidentsContainer;

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

    incidentsContainer = (RelativeLayout) rootView.findViewById(R.id.incidentsContainer);
    toggleView(View.GONE);

    presenter = new DashboardPresenterImpl(this);

    try {
        presenter.loadIncidents();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

private void toggleView(int visibility) {
    incidentsContainer.setVisibility(visibility);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    setTitle(R.string.dashboard);
}

@Override
public void showProgress() {
    getActivity().getActionBar();
    getActivity().setProgressBarIndeterminateVisibility(true);
}

@Override
public void hideProgress() {
    getActivity().getActionBar();
    getActivity().setProgressBarIndeterminateVisibility(false);
}

@Override
public void showContent() {
    toggleView(View.VISIBLE);
}
}

调用showProgress()的Presenter看起来像这样:

public class DashboardPresenterImpl implements DashboardPresenter {

    private DashboardView view;

    public DashboardPresenterImpl(DashboardView view) {
        this.view = view;
    }

    public void loadIncidents() throws InterruptedException {
        view.showProgress();
        new LoadContentTask().execute();
    }

    private class LoadContentTask extends AsyncTask<URL, Integer, Long> {

        protected Long doInBackground(URL... urls) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return 0l;
        }

        protected void onPostExecute(Long result) {
            view.showContent();
            view.hideProgress();
        }
    }
}

异步任务现在只是伪造一些数据加载并测试进度条。

这是我希望每个菜单项都可用的两个全局菜单项。但是进度条没有显示并隐藏在片段的命令中。那我在这里错过了什么?

0 个答案:

没有答案