Zx5
Zx5

Reputation: 165

Interaction between the Menu Item (of Status Bar) and RecyclerView Item?

I've populated a recyclerview via JSON response from a server.

The recyclerview items consist of an image and time. There's also a button whose visibility is set to invisible.

enter image description here

This is how each item looks with the button (also edit) visible

Now, there is an edit button in the menu item (of the app) on click of which I want to set all the recyclerview item buttons visible.

How could I achieve this? I tried searching for the same but to no avail.

EDIT:

This the item code in the Schedule Fragment where the RecyclerView is displayed.

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.action_edit:
            //onClick of edit menu item

            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

This is the ViewHolder class in ScheduleAdapter

    public class ViewHolder extends RecyclerView.ViewHolder {

    private NetworkImageView imageView_status;
    private TextView textView_status;
    private TextView textView_time;
    private Button button_edit;

    public ViewHolder(View itemView) {
        super(itemView);

        imageView_status = (NetworkImageView) itemView.findViewById(R.id.imageView);
        textView_status = (TextView) itemView.findViewById(R.id.textView_Status);
        textView_time = (TextView) itemView.findViewById(R.id.textView_Time);
        button_edit = (Button) itemView.findViewById(R.id.button);
    }
}

This is the onBindViewHolder of the ScheduleAdapter

    public void onBindViewHolder(final CustomScheduleListAdapter.ViewHolder holder, int position) {

    if (imageLoader == null) {
        imageLoader = AppController.getInstance().getImageLoader();
    }

    holder.imageView_status.setImageUrl(punches.get(position).getImageURL(), imageLoader);
    holder.textView_status.setText(punches.get(position).getStatusName());
    holder.textView_time.setText(punches.get(position).getTimings());

    holder.button_edit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            //Actions performed after button click
        }
    });
}

Upvotes: 1

Views: 459

Answers (1)

MidasLefko
MidasLefko

Reputation: 4559

You can have a field in the Adapter that keeps track of whether the RecyclerView items should be set to visible or not. Then in onBindViewHolder you set the visibility of the views based on it. Lastly in onOptionsItemSelected you set the value of that field in the adapter and call notifyItemRangeChanged on it.

For example:

Adapter:

public class ScheduleAdapter extends RecyclerView.Adapter<CustomScheduleListAdapter.ViewHolder> {

    private boolean showAllViews = false;

    public void setShowAllViews(boolean show) {
        showAllViews = show;
        notifyItemRangeChanged(0, getItemCount());
    }

    @Override
    public void onBindViewHolder(CustomScheduleListAdapter.ViewHolder holder, int position) {
        holder.viewyoucareabout.setVisibility(showAllViews? View.VISIBLE : View.GONE);
    }
}

Schedule Fragment:

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.action_edit:
            adapter.setShowAllViews(true);    
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

Upvotes: 2

Related Questions