首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android Gridlayout初始化耗时太长

Android Gridlayout初始化耗时太长
EN

Stack Overflow用户
提问于 2018-08-09 08:42:49
回答 2查看 395关注 0票数 1

对于我的一个片段,我有一个非常长的onActivityCreated,因为它必须初始化一个大的网格布局。我没有使用xml,而是以编程方式完成了这项工作,因为在我的生活中,编写代码比使用xml创建108个单独的视图更容易。唯一的问题是:在创建片段时,加载所有内容需要花费很长时间。

我曾尝试使用AsyncTaskLoader,但没有成功,因为您不能使用不同的线程来更改视图。无论如何,请告诉我我可以让它运行得更快或更好。这是我的第一个应用,所以很有可能我真的很傻。任何帮助都是非常感谢的。

另外,我在这段代码中做了很多事情,比如使用媒体播放器和onclicklisteners。所有这些都工作得很好,只是加载片段的速度很慢。这是我的代码:

代码语言:javascript
复制
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    if (savedInstanceState == null) {
        TextViewCompat.setAutoSizeTextTypeWithDefaults(textTranslation, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
        GridLayout gridLayout = v.findViewById(R.id.grid_layout);
        final MediaPlayer[] sound_resource = {MediaPlayer.create(v.getContext(), R.raw.a_sound),
                MediaPlayer.create(v.getContext(), R.raw.b_sound), MediaPlayer.create(v.getContext(), R.raw.c_sound),
                MediaPlayer.create(v.getContext(), R.raw.d_sound), MediaPlayer.create(v.getContext(), R.raw.e_sound),
                MediaPlayer.create(v.getContext(), R.raw.f_sound), MediaPlayer.create(v.getContext(), R.raw.g_sound),
                MediaPlayer.create(v.getContext(), R.raw.h_sound), MediaPlayer.create(v.getContext(), R.raw.i_sound),
                MediaPlayer.create(v.getContext(), R.raw.j_sound), MediaPlayer.create(v.getContext(), R.raw.k_sound),
                MediaPlayer.create(v.getContext(), R.raw.l_sound), MediaPlayer.create(v.getContext(), R.raw.m_sound),
                MediaPlayer.create(v.getContext(), R.raw.n_sound), MediaPlayer.create(v.getContext(), R.raw.o_sound),
                MediaPlayer.create(v.getContext(), R.raw.p_sound), MediaPlayer.create(v.getContext(), R.raw.q_sound),
                MediaPlayer.create(v.getContext(), R.raw.r_sound), MediaPlayer.create(v.getContext(), R.raw.s_sound),
                MediaPlayer.create(v.getContext(), R.raw.t_sound), MediaPlayer.create(v.getContext(), R.raw.u_sound),
                MediaPlayer.create(v.getContext(), R.raw.v_sound), MediaPlayer.create(v.getContext(), R.raw.w_sound),
                MediaPlayer.create(v.getContext(), R.raw.x_sound), MediaPlayer.create(v.getContext(), R.raw.y_sound),
                MediaPlayer.create(v.getContext(), R.raw.z_sound), MediaPlayer.create(v.getContext(), R.raw.one_sound),
                MediaPlayer.create(v.getContext(), R.raw.two_sound), MediaPlayer.create(v.getContext(), R.raw.three_sound),
                MediaPlayer.create(v.getContext(), R.raw.four_sound), MediaPlayer.create(v.getContext(), R.raw.five_sound),
                MediaPlayer.create(v.getContext(), R.raw.six_sound), MediaPlayer.create(v.getContext(), R.raw.seven_sound),
                MediaPlayer.create(v.getContext(), R.raw.eight_sound), MediaPlayer.create(v.getContext(), R.raw.nine_sound),
                MediaPlayer.create(v.getContext(), R.raw.zero_sound)};

        String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        List<ImageButton> imageButtonList = new ArrayList<ImageButton>();

        final IsPlaying isPlaying = new IsPlaying(null);

        final int[] ind = {0};

        for (int i = 0, c = 0, r = 0; i < 36 * 3; i++, c++) {
            if (c == 3) {
                c = 0;
                r++;
                ind[0] = r;
            }

            GridLayout.Spec row = GridLayout.spec(r, GridLayout.CENTER);
            GridLayout.Spec col = GridLayout.spec(c, GridLayout.CENTER);
            GridLayout.Spec col2 = GridLayout.spec(c, GridLayout.LEFT);

            GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(row, col);

            if (c == 0) {
                ImageButton imageButton = new ImageButton(v.getContext());
                imageButton.setId(ind[0]);
                imageButton.setBackgroundResource(R.drawable.ic_play_sound);
                imageButtonList.add(imageButton);
                imageButtonList.get(ind[0]).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MediaPlayer player = sound_resource[v.getId()];
                        if (!isPlaying.somethingIsPlaying) {//if nothing is playing
                            player.start();
                            isPlaying.mediaPlayer = player;
                            isPlaying.somethingIsPlaying = true;
                        } else if (player.isPlaying()) {//if something is playing and player is already playing
                            player.seekTo(0);
                            player.start();
                        } else {//if something is playing and player is not playing
                            isPlaying.mediaPlayer.seekTo(0);
                            isPlaying.mediaPlayer.pause();
                            player.start();
                            isPlaying.mediaPlayer = player;
                            isPlaying.somethingIsPlaying = true;
                        }

                        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                isPlaying.somethingIsPlaying = false;
                                mp.seekTo(0);
                            }
                        });
                    }
                });
                gridLayout.addView(imageButton, layoutParams);
            } else if (c == 1) {
                TextView textView = new TextView(v.getContext());
                textView.setText(String.valueOf(abc.charAt(r)));
                textView.setTextSize(27);
                textView.setTextColor(v.getContext().getResources().getColor(R.color.main_text_color));
                textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                gridLayout.addView(textView, layoutParams);

            } else if (c == 2) {
                ImageView imageView = new ImageView(v.getContext());
                imageView.setImageResource(images_resources[r]);

                GridLayout.LayoutParams layoutParams1 = new GridLayout.LayoutParams(row, col2);
                layoutParams1.leftMargin = 13;
                gridLayout.addView(imageView, layoutParams1);
            }
        }

    }
    super.onActivityCreated(savedInstanceState);

}
EN

回答 2

Stack Overflow用户

发布于 2018-08-09 08:46:42

使用自定义适配器类创建视图。

从下面的链接中获得提示...

https://developer.android.com/guide/topics/ui/layout/gridview

票数 0
EN

Stack Overflow用户

发布于 2018-08-09 12:29:03

您可以尝试将回收器视图用作网格视图,并创建自定义的回收器视图适配器。我认为它的工作速度会很快,因为回收器视图只会创建在屏幕上可见的单元格,并有一点偏移。

你可以通过回收器视图here获得关于网格视图的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51757386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档