我使用-classpath
;
在RecyclerView
中,我制作了一个带有搜索栏和播放按钮的歌曲列表。
在搜索栏中,使用线程
移动音乐当前位置但改变活动,音乐停止但错误
我希望当我改变活动时,音乐停止而不是错误
请帮帮我
RecyclerView
答案 0 :(得分:0)
线程只能在Activity或Fragment中被杀死。使用此类覆盖您的recyclerview点击侦听器。
创建新的java文件并将其命名为RecyclerItemClickListener.java
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
private GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
// do nothing
}
}
在您的活动/片段
中yourRecyclerviewId.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view1, int position) {
//view1 - is clicked view
//position - position of the clicked list
//put your method in here
}
}));
现在,您可以使用OnPause()和OnStop()方法来终止线程或服务