crysis
crysis

Reputation: 1584

Access Recyclerview viewholder from activity

I'm developing a music player app for android. When a song completes I send a broadcast and in an activity i want to change the play/pause icon in a row accordingly.

The activity receives the broadcast of next song but I don't know how to update row from activity.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_now_playing);

        mStaggerGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);

        mRecyclerView = (RecyclerView) findViewById(R.id.now_playing_songs_list);
        mRecyclerView.setLayoutManager(mStaggerGridLayoutManager);
        mAdapter = new ListAdapter(this, mSongList)
        mRecyclerView.setAdapter(mAdapter);
        mReciever = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if(intent.getAction().equals(MusicService.SONG_FINISHED)) {
                    //WANT TO CHANGE ROW ICON HERE
                }
            }
        };

How do I access viewholder defined inside mAdapter in activity?

Upvotes: 3

Views: 3353

Answers (1)

user5703518
user5703518

Reputation:

You must change your model from your activity and then ask adapter to update the view. Single responsibility is one of the oop design principles. For example all song objects must have a boolean value name it isPlaying. When user is playing a song it must be set to true. then when you receive your broadcast you find that song from your list and set the boolean to false and then call mAdapter.notifyDataSetChanged(). in your getView method you just check that variable and set UI to say pause or play state.

Upvotes: 2

Related Questions