How I can stop thread in RecyclerView ViewHolder when activity finished?

1k views Asked by At

I make an activity with a RecyclerView;

In the RecyclerView, I made a song list with a seekbar and play button.

In seekbar, this moved with music current position using thread

But change activity, music is stop but error

I want when i change the activity, music is stop and not error

Please help me

//seekbar change listener
seekbar.setMax(music.getDuration());

seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
        if(fromUser)
            music.seekTo(progress);
    }
});

//music playing
public void button(View v){
if(music.isPlaying()){
    music.stop();
    try {
        music.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    music.seekTo(0);

    button.setText(R.string.start);
    seekbar.setProgress(0);
    }else{
    music.start();
    button.setText(R.string.stop);

    Thread();
}
}

//Thread
public void Thread(){
    Runnable task = new Runnable(){
        public void run(){

            while(music.isPlaying()){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                seekbar.setProgress(music.getCurrentPosition());
            }
        }
    };
    Thread thread = new Thread(task);
    thread.start();
}
1

There are 1 answers

0
ZeroOne On

The thread only can be kill in Activity or Fragment. Use this class to override your recyclerview click listener.

Create new java file and name it 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
    }
}

In your activity/fragment

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

       }
}));

Now, you can use OnPause() and OnStop() method to kill the thread or services