Android 片段和按钮延迟
我有一个带有4个适配器的4个片段的可滚动选项卡布局。每个片段中都有几个按钮,它们使用Android 片段和按钮延迟,android,gridview,android-fragments,soundpool,Android,Gridview,Android Fragments,Soundpool,我有一个带有4个适配器的4个片段的可滚动选项卡布局。每个片段中都有几个按钮,它们使用声音池播放mp3短片。它在大多数情况下都能工作(除了点击按钮后的一些崩溃),但我注意到从一个片段滚动到另一个片段需要很长时间。此外,根据我所看到的使用短剪辑的情况,我在切换到SoundPool之前使用了MediaPlayer项目,现在加载初始页面需要一段时间,有时点击会使应用程序崩溃(它也使用MediaPlayer)。所以我想让滚动更平滑(我确实打开了硬件加速),页面加载更快,按钮不会崩溃。现在我有: 在主要活动
声音池
播放mp3短片。它在大多数情况下都能工作(除了点击按钮后的一些崩溃),但我注意到从一个片段滚动到另一个片段需要很长时间。此外,根据我所看到的使用短剪辑的情况,我在切换到SoundPool
之前使用了MediaPlayer
项目,现在加载初始页面需要一段时间,有时点击会使应用程序崩溃(它也使用MediaPlayer
)。所以我想让滚动更平滑(我确实打开了硬件加速),页面加载更快,按钮不会崩溃。现在我有:
在主要活动中:
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;
}
}
这是一个内部类,与我正在使用的其他片段
相同。以及相应的适配器类:
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);
}
}
}
公共类错误适配器扩展BaseAdapter{
私有上下文;
私有字符串[]mButtons={“Button1”、“Button2”、“Button3”};
公共错误适应(上下文c){
mContext=c;
}
@凌驾
public int getCount(){
返回mButtons.length;
}
@凌驾
公共对象getItem(int位置){
返回null;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
按钮myButton;
if(convertView==null){
myButton=新按钮(mContext);
myButton.setLayoutParams(新的GridView.LayoutParams(230150));
设置填充(8,8,8,8);
convertView=myButton;
}否则{
myButton=(按钮)转换视图;
}
myButton.setText(mButtons[position]);
myButton.setId(位置);
myButton.setTag(Integer.valueOf(position));
myButton.setOnClickListener(新的MyOnClickListener(位置));
返回视图;
}
类MyOnClickListener实现OnClickListener{
私人职位;
最终声音池=新的声音池(1,AudioManager.STREAM_MUSIC,0);
列表流=新的ArrayList();
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);
公共MyOnClickListener(内部职位){
这个位置=位置;
}
公共void onClick(视图v){
位置=v.getId();
如果(位置==0){
int streamID1=pool.play(声音ID1,1.0f,1.0f,1,0,1.0f);
streams.add(streamID1);
}
如果(位置==1){
int streamID2=pool.play(声音id2,1.0f,1.0f,1,0,1.0f);
streams.add(streamID2);
}
如果(位置==2){
int streamID3=pool.play(声音id3,1.0f,1.0f,1,0,1.0f);
streams.add(streamID3);
}
}
}
此适配器类是为其他片段克隆的。我认为问题可能是在每个适配器中重复使用GridView
和/或SoundPool
,但我不确定。如果您能提供帮助,请不要在主线程上加载任何内容。为什么要加载声音池每个而不是共享。很可能加载时间太长。您应该在后台线程上加载声音池,并且只加载一次