我为我的片段之一准备了一个很长的onActivityCreated,因为它必须初始化一个很大的网格布局。我没有使用xml,而是通过编程方式进行了此操作,因为与使用xml创建108个单独的视图相比,编写代码更容易。唯一的问题:创建片段时,要永久加载所有内容。
我尝试使用AsyncTaskLoader,但没有用,因为您无法使用其他线程更改视图。无论如何,请告诉我,我可以使其运行更快或更佳。这是我的第一个应用程序,因此很有可能我真的很傻。任何帮助表示赞赏。
我还在此片段中做很多事情,例如使用媒体播放器和使用onclicklisteners。所有这些都可以完美地运行,但加载片段的速度确实很慢。这是我的代码:
@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);
}
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以尝试将Recycler View用作网格视图,并通过创建自定义Recycler View Adapter来使用。我认为它会快速工作,因为“回收站”视图仅创建在屏幕上可见的带有少量偏移的单元。
您可以在Recycler视图here的Grid视图上获得帮助。