如何在 ViewSwitcher 动画发生之前更新 GridView?

发布于 2024-10-15 06:25:37 字数 2578 浏览 3 评论 0原文

我开发了一款战舰游戏,作为我的第一个 Android 应用程序。

我决定使用包含两个 GridView 的 ViewSwitcher。一个 bUserGrid 用于用户字段(托管用户船舶),bComputerGrid 用于计算机字段(显示用户截图)。

GridView bComputerGrid, bUserGrid, mComputerGrid, mUserGrid;
ViewSwitcher bigSwitcher, miniSwitcher;

用户攻击(点击“计算机”字段)。应用程序运行 AsyncTask userAttack,它定义该攻击是否成功。

bComputerGrid.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    runUserAttack(position);
  }
});

如果用户错过了,则在 userAttackonPostExecute() 部分中,我调用 notifyDataSetChanged() 以刷新 bComputerGrid。

    protected void onPostExecute(String message) {
        ...
        bComputerAdapter.notifyDataSetChanged();
        mComputerAdapter.notifyDataSetChanged();
        firstmateMsg.setText(message);
        mUserBundle = mUserAttack.getmUBundle();
        mUserAttack=null;
        if (isCancelled()) return;
        if (mUserBundle.getBoolean("userMissed")) {
            mComputerAttack = new computerAttack();
            mComputerAttack.execute(mComputerBundle);
        } else {
            if (doesWon(seqComputerShips)) {
                showDialog(0);
            }
        }
    }

但这并没有发生!如果用户错过,以下语句不会更新bComputerGrid

bComputerAdapter.notifyDataSetChanged();

然后在 userAttackonPostExecute() 部分中,我启动 AsyncTask computerAttack。 将 bComputerGrid 切换到 bUserGrid

bigSwitcher.showNext();

computerAttackonPreExecute() 部分中,我通过调用切换发生但 bComputerGrid< /strong> 在此之前还没有更新过!

计算机执行攻击,并通过调用 bUserAdapter.notifyDataSetChanged() 成功刷新 computerAttackonProgressUpdate() 部分中的用户字段。当计算机错过时,应用程序会等待用户再次敲击(点击)。

    protected void onProgressUpdate(String... messages) {
        ...
        mUserAdapter.notifyDataSetChanged();
        bUserAdapter.notifyDataSetChanged();

    }
    protected void onPostExecute(Boolean result) {
        mComputerBundle = mComputerAttack.getmCBundle();
        mComputerAttack=null;
        if (field_switch) {
            switchViews();
        }

    }

但为什么我无法在切换和计算机拍摄发生之前刷新bComputerGrid

请你帮助我好吗?

I work on a Battleships game as my first Android application.

I decided to use a ViewSwitcher that holds two GridViews. One bUserGrid is for the User field (hosting user ships), bComputerGrid is for the Computer field (showing user shots).

GridView bComputerGrid, bUserGrid, mComputerGrid, mUserGrid;
ViewSwitcher bigSwitcher, miniSwitcher;

User strikes (taps Computer field).The application runs the AsyncTask userAttack which defines whether that strike was successful or not.

bComputerGrid.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    runUserAttack(position);
  }
});

If user missed, then in a onPostExecute() section of userAttack I call notifyDataSetChanged() in order to refresh the bComputerGrid.

    protected void onPostExecute(String message) {
        ...
        bComputerAdapter.notifyDataSetChanged();
        mComputerAdapter.notifyDataSetChanged();
        firstmateMsg.setText(message);
        mUserBundle = mUserAttack.getmUBundle();
        mUserAttack=null;
        if (isCancelled()) return;
        if (mUserBundle.getBoolean("userMissed")) {
            mComputerAttack = new computerAttack();
            mComputerAttack.execute(mComputerBundle);
        } else {
            if (doesWon(seqComputerShips)) {
                showDialog(0);
            }
        }
    }

But it doesn’t happen! Following statement doesn’t update bComputerGrid, if user misses.

bComputerAdapter.notifyDataSetChanged();

Then here in onPostExecute() section of userAttack I start AsyncTask computerAttack. In onPreExecute() section of computerAttack I switch bComputerGrid to bUserGrid by calling

bigSwitcher.showNext();

Switching happens but bComputerGrid hasn't been updated before that!

Computer does its strike and successfully refreshes the user field in onProgressUpdate() section of computerAttack by calling bUserAdapter.notifyDataSetChanged(). When Computer missed, the application comes to wait for the user strike (tap) again.

    protected void onProgressUpdate(String... messages) {
        ...
        mUserAdapter.notifyDataSetChanged();
        bUserAdapter.notifyDataSetChanged();

    }
    protected void onPostExecute(Boolean result) {
        mComputerBundle = mComputerAttack.getmCBundle();
        mComputerAttack=null;
        if (field_switch) {
            switchViews();
        }

    }

But why I can’t refresh bComputerGrid before that switching and Computer shot happens?

Could you, please, help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖心男生 2024-10-22 06:25:37

您在哪里更新适配器?你改变了适配器的底层数据,或者你改变了适配器列表?
适配器也是 UI 的一部分,因此适配器本身的更改应该仅在 GUI 线程中发生。当我更改服务线程中的适配器内容时,我收到了几个 UI 异常(显示的),告诉我在其他一些项目中的情况。
最终您必须重新创建适配器列表以反映基础数据的更改?

Where you update your adapters? You change underlying data of the adapter, or you change the adapter list?
Adapters are also part of the UI, so changes of the adapter itself should happen only in a GUI thread. I got a couple of UI Exceptions (on display) telling me so in some other project as I changed the adapter contents in a Service Thread.
Eventually you have to recreate your adapter list to reflect changes in underlying data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文