Android学习-第51天
今天的任务为数据绑定回调、释放音频以及设备旋转和对象保存,代码如下:
list_item_sound.xml:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="viewModel" type="com.bignerdranch.android.beatbox.SoundViewModel"/> </data> <Button android:layout_width="match_parent" android:layout_height="120dp" android:onClick="@{() -> viewModel.onButtonClicked()}" android:text="@{viewModel.title}" tools:text="Sound name"/> </layout>
BeatBox.java:
package com.bignerdranch.android.beatbox; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.media.AudioManager; import android.media.SoundPool; import android.util.Log; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class BeatBox { private static final String TAG="BeatBox"; private static final String SOUNDS_FOLDER="sample_sounds"; private static final int MAX_SOUNDS=5; private AssetManager mAssets; private List<Sound> mSounds=new ArrayList<>(); private SoundPool mSoundPool; public BeatBox(Context context){ mAssets=context.getAssets(); mSoundPool=new SoundPool(MAX_SOUNDS, AudioManager.STREAM_MUSIC,0); loadSounds(); } public void play(Sound sound){ Integer soundId=sound.getSoundId(); if (soundId==null){ return; } mSoundPool.play(soundId,1.0f,1.0f,1,0,1.0f); } public void release(){ mSoundPool.release(); } private void loadSounds() { String[] soundNames; try { soundNames=mAssets.list(SOUNDS_FOLDER); Log.i(TAG,"Found"+ soundNames.length+"sounds"); }catch (IOException ioe){ Log.e(TAG,"Could not list assets",ioe); return; } for (String filename : soundNames){ try { String assetPath = SOUNDS_FOLDER + "/" + filename; Sound sound = new Sound(assetPath); load(sound); mSounds.add(sound); }catch (IOException ioe){ Log.e(TAG,"Could not load sound"+filename,ioe); } } } private void load(Sound sound) throws IOException{ AssetFileDescriptor afd=mAssets.openFd(sound.getAssetPath()); int soundId=mSoundPool.load(afd,1); sound.setSoundId(soundId); } public List<Sound> getSounds() { return mSounds; } }
BeatBoxFragment.java:
package com.bignerdranch.android.beatbox; import com.bignerdranch.android.beatbox.databinding.FragmentBeatBoxBinding; import com.bignerdranch.android.beatbox.databinding.ListItemSoundBinding; import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.List; public class BeatBoxFragment extends Fragment { private BeatBox mBeatBox; public static BeatBoxFragment newInstance(){ return new BeatBoxFragment(); } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setRetainInstance(true); mBeatBox=new BeatBox(getActivity()); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ FragmentBeatBoxBinding binding= DataBindingUtil .inflate(inflater,R.layout.fragment_beat_box,container,false); binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3)); //binding.recyclerView.setAdapter(new SoundAdapter()); binding.recyclerView.setAdapter(new SoundAdapter(mBeatBox.getSounds())); return binding.getRoot(); } @Override public void onDestroy(){ super.onDestroy(); mBeatBox.release(); } private class SoundHolder extends RecyclerView.ViewHolder{ private ListItemSoundBinding mBinding; public SoundHolder(ListItemSoundBinding binding) { super(binding.getRoot()); mBinding=binding; mBinding.setViewModel(new SoundViewModel(mBeatBox)); } public void bind(Sound sound){ mBinding.getViewModel().setSound(sound); mBinding.executePendingBindings(); } } private class SoundAdapter extends RecyclerView.Adapter<SoundHolder>{ private List<Sound> mSounds; public SoundAdapter(List<Sound> sounds){ mSounds=sounds; } @Override public SoundHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater=LayoutInflater.from(getActivity()); ListItemSoundBinding binding=DataBindingUtil .inflate(inflater,R.layout.list_item_sound,parent,false); return new SoundHolder(binding); } @Override public void onBindViewHolder(SoundHolder holder, int position) { Sound sound=mSounds.get(position); holder.bind(sound); } @Override public int getItemCount() { return mSounds.size(); } } }
效果如下: