我有一个带有 4 个片段和 4 个适配器的可滚动选项卡布局。每个片段中都有几个按钮,SoundPool
用于播放短的 mp3 剪辑。它在大多数情况下都有效(除了单击按钮后的几次崩溃),但我注意到从一个片段滚动到另一个片段需要很长时间。此外,我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);
}
}
}
这个 Adapter 类是为另一个克隆的Fragments
。我认为问题可能是在每个适配器中重复使用GridView
和/或SoundPool
,但我不确定。任何帮助将不胜感激!