我有一个可滚动的标签布局,其中有4个片段和4个适配器。每个片段都有几个按钮,使用SoundPool
来播放短片段。它在很大程度上起作用(除了按钮点击后的一些崩溃),但我注意到从一个片段滚动到另一个片段需要很长时间。根据我看到的使用短片的内容切换到MediaPlayer
之前,我还在使用SoundPool
项目,现在需要一段时间来加载初始页面,有时点击崩溃应用程序(它也是做了MediaPlayer
)。所以我想让滚动更加流畅(我确实打开了硬件加速),页面加载更快,按钮不崩溃。现在我有:
在MainActivity中:
public static class MiscFragment extends Fragment {
public MiscFragment() {
}
View rootView;
GridView gridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.tab_fragment,
container, false);
gridView = (GridView) rootView.findViewById(R.id.tabgridview);
gridView.setAdapter(new MiscAdapter(getActivity()));
return rootView;
}
}
这是一个内部类,它与我正在使用的其他Fragments
相同。以及相应的Adapter
类:
public class MiscAdapter extends BaseAdapter {
private Context mContext;
private String[] mButtons = { "Button1", "Button2", "Button3"};
public MiscAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return mButtons.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button myButton;
if (convertView == null) {
myButton = new Button(mContext);
myButton.setLayoutParams(new GridView.LayoutParams(230, 150));
myButton.setPadding(8, 8, 8, 8);
convertView = myButton;
} else {
myButton = (Button) convertView;
}
myButton.setText(mButtons[position]);
myButton.setId(position);
myButton.setTag(Integer.valueOf(position));
myButton.setOnClickListener(new MyOnClickListener(position));
return convertView;
}
class MyOnClickListener implements OnClickListener {
private int position;
final SoundPool pool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
List<Integer> streams = new ArrayList<Integer>();
final int soundID1 = pool.load(mContext, R.raw.mp3_1, 1);
final int soundID2 = pool.load(mContext, R.raw.mp3_2, 1);
final int soundID3 = pool.load(mContext, R.raw.mp3_3, 1);
public MyOnClickListener(int position) {
this.position = position;
}
public void onClick(View v) {
position = v.getId();
if(position == 0){
int streamID1 = pool.play(soundID1, 1.0f, 1.0f, 1, 0, 1.0f);
streams.add(streamID1);
}
if(position == 1){
int streamID2 = pool.play(soundID2, 1.0f, 1.0f, 1, 0, 1.0f);
streams.add(streamID2);
}
if(position == 2){
int streamID3 = pool.play(soundID3, 1.0f, 1.0f, 1, 0, 1.0f);
streams.add(streamID3);
}
}
}
克隆了另一个Fragments
的Adapter类。我认为问题可能是在每个适配器中重复使用GridView
和/或SoundPool
,但我不确定。真的很感激任何帮助!
答案 0 :(得分:0)
如果可以提供帮助,切勿在主线程上加载内容。为什么要加载一个声音池而不是共享它。最有可能的是加载时间太长。您应该在后台线程上加载声音池并且只加载一次。