将广播名提取到一个常量类中,实现了仿酷狗的搜索界面及搜索列表的播放子项显示

pull/1/head
jsyjst 7 years ago
parent d5a63ffa48
commit 14f2afa624

@ -1,14 +1,19 @@
package com.example.musicplayer.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.musicplayer.R;
import com.example.musicplayer.entiy.SeachSong;
import com.example.musicplayer.util.CommonUtil;
import com.example.musicplayer.util.FileHelper;
import java.util.ArrayList;
@ -17,14 +22,22 @@ import java.util.ArrayList;
*/
public class SearchContentAdapter extends RecyclerView.Adapter<SearchContentAdapter.ViewHolder> {
private static final String TAG="SearchContentAdapter";
private ArrayList<SeachSong.DataBean> mSongListBeans;
private static ItemClick mItemClick;
private String mSeek;
private Context mContext;
private int mLastPosition = -1;
public static void setItemClick(ItemClick itemClick){
mItemClick = itemClick;
}
public SearchContentAdapter(ArrayList<SeachSong.DataBean> songListBeans){
public SearchContentAdapter(ArrayList<SeachSong.DataBean> songListBeans, String seek, Context context){
mContext = context;
mSeek = seek;
mSongListBeans = songListBeans;
}
@NonNull
@ -40,11 +53,27 @@ public class SearchContentAdapter extends RecyclerView.Adapter<SearchContentAdap
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
SeachSong.DataBean songListBean = mSongListBeans.get(position);
holder.artistTv.setText(songListBean.getSinger());
//设置与搜索一样的string的颜色
CommonUtil.showStringColor(mSeek,songListBean.getSinger(),holder.artistTv);
holder.titleTv.setText(songListBean.getName());
CommonUtil.showStringColor(mSeek,songListBean.getName(),holder.titleTv);
if(FileHelper.getSong().getImgUrl() !=null){
//根据点击显示
holder.playLine.setVisibility((position == mLastPosition||
(songListBean.getName().equals(FileHelper.getSong().getTitle())&&
songListBean.getSinger().equals(FileHelper.getSong().getArtist())))
?View.VISIBLE:View.INVISIBLE);
holder.mItemView.setBackgroundResource((position == mLastPosition||
(songListBean.getName().equals(FileHelper.getSong().getTitle())&&
songListBean.getSinger().equals(FileHelper.getSong().getArtist())))
?R.color.click:R.color.translucent);
}
holder.mItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mItemClick.onClick(position);
equalPosition(position);
}
});
}
@ -58,11 +87,13 @@ public class SearchContentAdapter extends RecyclerView.Adapter<SearchContentAdap
TextView titleTv;
TextView artistTv;
View mItemView;
View playLine;
public ViewHolder(View itemView) {
super(itemView);
titleTv = itemView.findViewById(R.id.tv_title);
artistTv = itemView.findViewById(R.id.tv_artist);
playLine = itemView.findViewById(R.id.line_play);
mItemView = itemView;
}
}
@ -70,4 +101,13 @@ public class SearchContentAdapter extends RecyclerView.Adapter<SearchContentAdap
public interface ItemClick{
void onClick(int position);
}
//判断点击的是否为上一个点击的项目
public void equalPosition(int position) {
if (position != mLastPosition) {
if(mLastPosition!=-1) notifyItemChanged(mLastPosition);
mLastPosition = position;
}
notifyItemChanged(position);
}
}

@ -33,7 +33,6 @@ public class SongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Mp3Info> mMp3InfoList;
private Context mContext;
private int mLastPosition = -1;
private int defaultPosition;
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
@ -79,9 +78,9 @@ public class SongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
if (viewType == itemViewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_song_item, parent, false);
TypedValue typedValue = new TypedValue();
mContext.getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true);
view.setBackgroundResource(typedValue.resourceId);
// TypedValue typedValue = new TypedValue();
// mContext.getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true);
// view.setBackgroundResource(typedValue.resourceId);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
@ -107,6 +106,13 @@ public class SongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.artistTv.setTextColor(MyApplication.getContext().getResources().
getColor(position == mLastPosition ? R.color.musicStyle_low : R.color.short_white));
holder.playingIv.setVisibility(position == mLastPosition ? View.VISIBLE : View.GONE);
if(FileHelper.getSong().getImgUrl()!=null){
holder.playingIv.setVisibility(View.GONE);
holder.songNameTv.setTextColor(MyApplication.getContext().getResources().
getColor(R.color.white));
holder.artistTv.setTextColor(MyApplication.getContext().getResources().
getColor(R.color.short_white));
}
// holder.playingIv.setVisibility(position==FileHelper.getSong().getCurrent()?View.VISIBLE:View.GONE);
holder.songView.setOnClickListener(new View.OnClickListener() {
@Override
@ -126,7 +132,6 @@ public class SongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
FileHelper.saveSong(song);
onItemClickListener.onSongClick();
defaultPosition=FileHelper.getSong().getCurrent();
equalPosition(position);

@ -0,0 +1,15 @@
package com.example.musicplayer.constant;
/**
* Created by on 2018/11/24.
*/
public class BroadcastName {
public static final String ONLINE_SONG="online_song";
public static final String ONLINE_SONG_FINISH="online_song_finish";
public static final String SONG_CHANGE="song_change";
public static final String LOCAL_SONG_CHANGE_LIST="local_song_change_list";
public static final String SONG_PAUSE ="song_pause";
public static final String SONG_RESUME="song_resume";
}

@ -10,6 +10,7 @@ import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.example.musicplayer.constant.BroadcastName;
import com.example.musicplayer.constant.MyApplication;
import com.example.musicplayer.entiy.Mp3Info;
import com.example.musicplayer.entiy.Song;
@ -31,7 +32,6 @@ public class PlayerService extends Service {
private List<Mp3Info> mMp3InfoList;
private int mCurrent;
private IntentFilter intentFilter;
@Override
public void onCreate(){
@ -63,8 +63,9 @@ public class PlayerService extends Service {
}else{
mPlayStatusBinder.stop();
}
sendBroadcast(new Intent("android.song.change")); //发送广播改变播放栏的信息
sendBroadcast(new Intent("android.song.change.local.song.list"));//发送广播改变当地列表的显示
sendBroadcast(new Intent(BroadcastName.SONG_CHANGE)); //发送广播改变播放栏的信息
sendBroadcast(new Intent(BroadcastName.LOCAL_SONG_CHANGE_LIST));//发送广播改变当地列表的显示
sendBroadcast(new Intent(BroadcastName.ONLINE_SONG_FINISH));//发送网络歌曲播放结束的广播改变网络搜索列表的改变
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@ -99,7 +100,7 @@ public class PlayerService extends Service {
mediaPlayer.prepare(); //进行缓冲
isPlaying=true;
mediaPlayer.setOnPreparedListener(new PreparedListener(currentTime));
sendBroadcast(new Intent("android.song.change"));
sendBroadcast(new Intent(BroadcastName.SONG_CHANGE));
} catch (Exception e) {
e.printStackTrace();
@ -116,7 +117,7 @@ public class PlayerService extends Service {
FileHelper.saveSong(song);
isPlaying=true;
mediaPlayer.start();
sendBroadcast(new Intent("SONG_ONLINE"));
sendBroadcast(new Intent(BroadcastName.ONLINE_SONG));
}catch (Exception e){
CommonUtil.showToast(MyApplication.getContext(),"抱歉该歌曲暂无版权");
e.printStackTrace();
@ -132,7 +133,7 @@ public class PlayerService extends Service {
isPlaying=false;
mediaPlayer.pause();
isPause = true;
sendBroadcast(new Intent("SONG_PAUSE"));//发送暂停的广播给主活动
sendBroadcast(new Intent(BroadcastName.SONG_PAUSE));//发送暂停的广播给主活动
}
}
@ -141,7 +142,7 @@ public class PlayerService extends Service {
mediaPlayer.start();
isPlaying=true;
isPause=false;
sendBroadcast(new Intent("SONG_RESUME"));
sendBroadcast(new Intent(BroadcastName.SONG_RESUME));
}
}
public void next(){
@ -164,8 +165,9 @@ public class PlayerService extends Service {
mPlayStatusBinder.play(0);
sendBroadcast(new Intent("android.song.change")); //发送广播改变播放栏的信息
sendBroadcast(new Intent("android.song.change.local.song.list"));//发送广播改变当地列表的显示
sendBroadcast(new Intent(BroadcastName.SONG_CHANGE)); //发送广播改变播放栏的信息
sendBroadcast(new Intent(BroadcastName.LOCAL_SONG_CHANGE_LIST));//发送广播改变当地列表的显示
sendBroadcast(new Intent(BroadcastName.ONLINE_SONG_FINISH));//发送网络歌曲播放结束的广播改变网络搜索列表的改变
}
public void last(){
mCurrent=FileHelper.getSong().getCurrent();
@ -187,8 +189,9 @@ public class PlayerService extends Service {
mPlayStatusBinder.play(0);
sendBroadcast(new Intent("android.song.change")); //发送广播改变播放栏的信息
sendBroadcast(new Intent("android.song.change.local.song.list"));//发送广播改变当地列表的显示
sendBroadcast(new Intent(BroadcastName.SONG_CHANGE)); //发送广播改变播放栏的信息
sendBroadcast(new Intent(BroadcastName.LOCAL_SONG_CHANGE_LIST));//发送广播改变当地列表的显示
sendBroadcast(new Intent(BroadcastName.ONLINE_SONG_FINISH));//发送网络歌曲播放结束的广播改变网络搜索列表的改变
}
/**

@ -4,10 +4,12 @@ import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
@ -97,4 +99,12 @@ public class CommonUtil {
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
/**
* 使
*/
public static void showStringColor(String appointStr, String originalStr, TextView textView){
originalStr = originalStr.replaceAll(appointStr, "<font color='#FFC66D'>" + appointStr+ "</font>");
textView.setText(Html.fromHtml(originalStr));
}
}

@ -17,6 +17,7 @@ import android.view.ViewGroup;
import com.example.musicplayer.R;
import com.example.musicplayer.adapter.SongAdapter;
import com.example.musicplayer.constant.BroadcastName;
import com.example.musicplayer.contract.ILocalMusicContract;
import com.example.musicplayer.entiy.Mp3Info;
import com.example.musicplayer.presenter.LocalMusicPresenter;
@ -75,7 +76,7 @@ public class LocalMusicFragment extends Fragment implements ILocalMusicContract.
super.onActivityCreated(savedInstanceState);
intentFilter=new IntentFilter();
intentFilter.addAction("android.song.change.local.song.list");
intentFilter.addAction(BroadcastName.LOCAL_SONG_CHANGE_LIST);
songChangeReceiver=new SongChangeLocalMusicReceiver();
getActivity().registerReceiver(songChangeReceiver,intentFilter);
initView();

@ -28,6 +28,7 @@ import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.musicplayer.R;
import com.example.musicplayer.constant.BroadcastName;
import com.example.musicplayer.constant.PlayerStatus;
import com.example.musicplayer.entiy.Song;
import com.example.musicplayer.service.PlayerService;
@ -78,10 +79,10 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("android.song.change");
intentFilter.addAction("SONG_PAUSE");
intentFilter.addAction("SONG_RESUME");
intentFilter.addAction("SONG_ONLINE");
intentFilter.addAction(BroadcastName.SONG_CHANGE);
intentFilter.addAction(BroadcastName.SONG_PAUSE);
intentFilter.addAction(BroadcastName.SONG_RESUME);
intentFilter.addAction(BroadcastName.ONLINE_SONG);
songChangeReceiver = new SongChangeReceiver();
registerReceiver(songChangeReceiver, intentFilter);
initView();
@ -287,12 +288,12 @@ public class MainActivity extends AppCompatActivity {
mSeekBar.setMax((int) mSong.getDuration());
if (!action.equals("SONG_ONLINE")) {
if (!action.equals(BroadcastName.ONLINE_SONG)) {
if(mSong.getImgUrl()==null) FileHelper.setSingerImg(MainActivity.this, mSong.getArtist(), mCoverIv);
if (action.equals("SONG_PAUSE")) {
if (action.equals(BroadcastName.SONG_PAUSE)) {
mPlayerBtn.setSelected(false);
mCircleAnimator.pause();
} else if (action.equals("SONG_RESUME")) {
} else if (action.equals(BroadcastName.SONG_RESUME)) {
mPlayerBtn.setSelected(true);
mCircleAnimator.resume();
mSeekBarThread = new Thread(new SeekBarThread());

@ -1,6 +1,9 @@
package com.example.musicplayer.view;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.SyncStateContract;
@ -15,6 +18,7 @@ import android.widget.EditText;
import com.example.musicplayer.R;
import com.example.musicplayer.adapter.SearchContentAdapter;
import com.example.musicplayer.constant.BroadcastName;
import com.example.musicplayer.constant.Constant;
import com.example.musicplayer.contract.ISearchContentContract;
import com.example.musicplayer.entiy.SeachSong;
@ -46,6 +50,8 @@ public class SearchContentFragment extends Fragment implements ISearchContentCon
private LinearLayoutManager manager;
private SearchContentAdapter mAdapter;
private ArrayList<SeachSong.DataBean> mSongList=new ArrayList<>();
private SongFinishReceiver songChangeReceiver;
private IntentFilter intentFilter;
private LRecyclerViewAdapter mLRecyclerViewAdapter;//下拉刷新
@ -66,6 +72,12 @@ public class SearchContentFragment extends Fragment implements ISearchContentCon
mPresenter.attachView(this);
mPresenter.search(getSeekContent(),1);
//注册广播
intentFilter = new IntentFilter();
intentFilter.addAction(BroadcastName.ONLINE_SONG_FINISH);
SongFinishReceiver songFinishReceiver = new SongFinishReceiver();
getActivity().registerReceiver(songFinishReceiver,intentFilter);
searchMore();
}
@ -79,7 +91,7 @@ public class SearchContentFragment extends Fragment implements ISearchContentCon
@Override
public void setSongsList(final ArrayList<SeachSong.DataBean> songListBeans) {
mSongList.addAll(songListBeans);
mAdapter = new SearchContentAdapter(mSongList);
mAdapter = new SearchContentAdapter(mSongList,getSeekContent(),getActivity());
mLRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
mRecycler.setLayoutManager(manager);
mRecycler.setAdapter(mLRecyclerViewAdapter);
@ -121,6 +133,8 @@ public class SearchContentFragment extends Fragment implements ISearchContentCon
@Override
public void searchMore() {
mRecycler.setPullRefreshEnabled(false);
mRecycler.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
@ -130,7 +144,7 @@ public class SearchContentFragment extends Fragment implements ISearchContentCon
}
});
//设置底部加载颜色
mRecycler.setFooterViewColor(R.color.colorAccent, R.color.musicStyle_low ,R.color.transparent);
mRecycler.setFooterViewColor(R.color.colorAccent, R.color.musicStyle_low ,R.color.translucent);
//设置底部加载文字提示
mRecycler.setFooterViewHint("拼命加载中","已经全部为你呈现了","网络不给力啊,点击再试一次吧");
@ -151,4 +165,11 @@ public class SearchContentFragment extends Fragment implements ISearchContentCon
}
});
}
class SongFinishReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
mAdapter.notifyDataSetChanged();
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

@ -10,7 +10,6 @@
</View>
<com.github.jdsjlzx.recyclerview.LRecyclerView
android:background="@color/translucent"
android:id="@+id/recycler_song_list"
android:layout_width="match_parent"
android:layout_height="match_parent"

@ -28,7 +28,7 @@
android:maxEms="15"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="18sp" />
android:textSize="16sp" />
<TextView
android:id="@+id/tv_artist"
@ -39,7 +39,8 @@
android:ellipsize="end"
android:maxEms="15"
android:singleLine="true"
android:textColor="@color/short_white" />
android:textColor="@color/short_white"
android:textSize="14sp"/>
</LinearLayout>

@ -1,10 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:background="@color/translucent">
<View
android:id="@+id/line_play"
android:layout_alignParentStart="true"
android:layout_width="5dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:background="@color/yellow"
android:visibility="invisible"/>
<ImageView
android:id="@+id/iv_add"
android:layout_toEndOf="@+id/line_play"
android:layout_marginStart="20dp"
android:layout_marginEnd="10dp"
android:layout_centerVertical="true"
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/add" />
<LinearLayout
android:layout_toRightOf="@id/iv_playing"
android:layout_toRightOf="@+id/iv_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
@ -19,7 +39,7 @@
android:maxEms="15"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="18sp" />
android:textSize="16sp" />
<TextView
android:id="@+id/tv_artist"
@ -30,14 +50,18 @@
android:ellipsize="end"
android:maxEms="15"
android:singleLine="true"
android:textColor="@color/short_white" />
android:textColor="@color/short_white"
android:textSize="14sp" />
</LinearLayout>
<View
android:layout_toRightOf="@+id/iv_add"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_height="0.1dip"
android:background="@color/gray" />
</RelativeLayout>

@ -12,9 +12,11 @@
<color name="seekColor">#80FFFFFF</color>
<color name="translucent">#80010738</color>
<color name="click">#99010738</color>
<color name="actionBarColor">#050e41</color>
<color name="transparent">#00000000</color>
<color name="musicStyle_low">#2ad3df</color>
<color name="player">#081038</color>
<color name="white_easy">#bcb9b9</color>
<color name="yellow">#FFC66D</color>
</resources>

Loading…
Cancel
Save