package com.wm.remusic.fragment; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.wm.remusic.R; import com.wm.remusic.provider.RecentStore; import com.wm.remusic.recent.Song; import com.wm.remusic.recent.SongLoader; import com.wm.remusic.recent.TopTracksLoader; import com.wm.remusic.service.MediaService; import com.wm.remusic.service.MusicPlayer; import com.wm.remusic.uitl.CommonUtils; import com.wm.remusic.uitl.IConstants; import com.wm.remusic.widget.DividerItemDecoration; import java.lang.reflect.Field; import java.util.List; /** * Created by wm on 2016/3/9. */ public class RecentFragment extends Fragment { private int currentlyPlayingPosition = 0; private Adapter mAdapter; private RecentStore recentStore; private Toolbar toolbar; private List<Song> mList; private RecyclerView recyclerView; private LinearLayoutManager layoutManager; //接受歌曲播放变化和列表变化广播,刷新列表 private BroadcastReceiver mStatusListener = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(MediaService.META_CHANGED)) { reloadAdapter(); } else if (action.equals(MediaService.PLAYLIST_CHANGED)) { reloadAdapter(); } } }; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); recentStore = RecentStore.getInstance(getContext()); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_recent, container, false); TopTracksLoader recentloader = new TopTracksLoader(getActivity(), TopTracksLoader.QueryType.RecentSongs); List<Song> recentsongs = SongLoader.getSongsForCursor(TopTracksLoader.getCursor()); int songCountInt = recentsongs.size(); mList = recentsongs; recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview); layoutManager = new LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(layoutManager); toolbar = (Toolbar) view.findViewById(R.id.toolbar); toolbar.setPadding(0, CommonUtils.getStatusHeight(getActivity()), 0, 0); ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar); final ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar(); ab.setHomeAsUpIndicator(R.drawable.actionbar_back); ab.setDisplayHomeAsUpEnabled(true); ab.setTitle("最近播放"); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getActivity().onBackPressed(); } }); new loadSongs().execute(""); return view; } @Override public void onResume() { super.onResume(); //注册广播 IntentFilter f = new IntentFilter(); f.addAction(MediaService.META_CHANGED); getActivity().registerReceiver(mStatusListener, f); } @Override public void onPause() { getActivity().unregisterReceiver(mStatusListener); super.onPause(); } //去除界面重叠 @Override public void onDetach() { super.onDetach(); try { //参数是固定写法 Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager"); childFragmentManager.setAccessible(true); childFragmentManager.set(this, null); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } //刷新列表 private void reloadAdapter() { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(final Void... unused) { //List<MusicInfo> songList = MusicUtils.getMusicLists(getContext(), recentStore.getRecentIds()); //mAdapter.updateDataSet(songList); return null; } @Override protected void onPostExecute(Void aVoid) { mAdapter.notifyDataSetChanged(); } }.execute(); } //异步加载recyclerview界面 private class loadSongs extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { if (getActivity() != null) mAdapter = new Adapter(mList); return "Executed"; } @Override protected void onPostExecute(String result) { recyclerView.setAdapter(mAdapter); if (getActivity() != null) recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST)); } @Override protected void onPreExecute() { } } public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int FIRST_ITEM = 0; final static int ITEM = 1; private List<Song> mList; public Adapter(List<Song> list) { if (list == null) { throw new IllegalArgumentException("model Data must not be null"); } mList = list; } //更新adpter的数据 public void updateDataSet(List<Song> list) { this.mList = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { if (viewType == FIRST_ITEM) return new CommonItemViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.common_item, viewGroup, false)); else { return new ListItemViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_musci_common_item, viewGroup, false)); } } //判断布局类型 @Override public int getItemViewType(int position) { return position == FIRST_ITEM ? FIRST_ITEM : ITEM; } //将数据与界面进行绑定 @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { Song model = null; if (position > 0) { model = mList.get(position - 1); } if (holder instanceof ListItemViewHolder) { ((ListItemViewHolder) holder).mainTitle.setText(model.title.toString()); ((ListItemViewHolder) holder).title.setText(model.artistName.toString()); //判断该条目音乐是否在播放 if (MusicPlayer.getCurrentAudioId() == model.id) { ((ListItemViewHolder) holder).playState.setVisibility(View.VISIBLE); ((ListItemViewHolder) holder).playState.setImageResource(R.drawable.song_play_icon); currentlyPlayingPosition = position; } else { ((ListItemViewHolder) holder).playState.setVisibility(View.GONE); } } else if (holder instanceof CommonItemViewHolder) { ((CommonItemViewHolder) holder).textView.setText("(共" + mList.size() + "首)"); ((CommonItemViewHolder) holder).select.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Intent intent = new Intent(getActivity(), SelectActivity.class); // intent.putParcelableArrayListExtra("ids", (ArrayList) mList); // getActivity().startActivity(intent); } }); } } @Override public int getItemCount() { return (null != mList ? mList.size() + 1 : 0); } public class CommonItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView textView; ImageView select; CommonItemViewHolder(View view) { super(view); this.textView = (TextView) view.findViewById(R.id.play_all_number); this.select = (ImageView) view.findViewById(R.id.select); view.setOnClickListener(this); } public void onClick(View v) { //// TODO: 2016/1/20 final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { long[] list = new long[mList.size()]; for (int i = 0; i < mList.size(); i++) { list[i] = mList.get(i).id; } MusicPlayer.playAll(null, list, 0, false); } }, 100); } } public class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { //ViewHolder ImageView moreOverflow, playState; TextView mainTitle, title; ListItemViewHolder(View view) { super(view); this.mainTitle = (TextView) view.findViewById(R.id.viewpager_list_toptext); this.title = (TextView) view.findViewById(R.id.viewpager_list_bottom_text); this.playState = (ImageView) view.findViewById(R.id.play_state); this.moreOverflow = (ImageView) view.findViewById(R.id.viewpager_list_button); moreOverflow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MoreFragment morefragment = MoreFragment.newInstance(mList.get(getAdapterPosition() - 1).id + "", IConstants.MUSICOVERFLOW); morefragment.show(getFragmentManager(), "music"); } }); view.setOnClickListener(this); } @Override public void onClick(View v) { final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // MusicPlayer.play(mList.get(getAdapterPosition() - 1).songId); // long[] ids = new long[1]; // ids[0] = mList.get(getAdapterPosition() - 1).songId; // long[] list = MusicPlayer.getQueue(); // for(int i = 0; i<list.length;i++){ // if(list[i] == ids[0]){ // MusicPlayer.playAll(getContext(),list,i,false); // return; // } // } // MusicPlayer.playNext(getContext(), ids, -1); long[] list = new long[mList.size()]; for (int i = 0; i < mList.size(); i++) { list[i] = mList.get(i).id; } if (getAdapterPosition() > 0) MusicPlayer.playAll(null, list, getAdapterPosition() - 1, false); Handler handler1 = new Handler(); handler1.postDelayed(new Runnable() { @Override public void run() { notifyItemChanged(currentlyPlayingPosition); notifyItemChanged(getAdapterPosition()); } }, 50); } }, 100); } } } }