petrrr33
petrrr33

Reputation: 599

Change view visibility for each item inside a RecyclerView within a Fragment when a text is clicked

I've got A Fragment which contains a Layout header with an exit button, a title text and an edit text which is clickable.

Below the header layout I have a RecyclerView that matches the parent layout in width and height.

enter image description here

Each row of the RecyclerView has an ImageView, a title text, a description text, a price text and an invisible ImageView used to remove the item from the Recycler.

enter image description here

Because my EDIT TextView is declared in the Fragment I can't get the ViewHolder inside the adapter in order to change the ImageView visibility like this:

enter image description here

All I want is to make the image visible for each row when the EDIT text is pressed.

Here is the Adapter and ViewHolder code:

public class BagItemsAdapter extends RecyclerView.Adapter<BagItemsAdapter.BagViewHolder> {

private List<BagItem> mBagList;
private Context mContext;
private String quantityPrefix, pricePrefix;

public BagItemsAdapter(Context context,List<BagItem> list)
{
    this.mBagList = list;
    this.mContext = context;
}

@Override
public BagViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.bag_shop_item_layout,parent,false);

    return new BagViewHolder(v);
}

@Override
public void onBindViewHolder(BagViewHolder holder, int position) {

    BagItem currentItem = mBagList.get(position);
    Picasso.with(mContext).load(currentItem.getItemImageUrl()).into(holder.bagItemImage);
    holder.bagItemTitleBrand.setText(currentItem.getItemBrandTitle());
    holder.bagItemDescription.setText(currentItem.getItemDescription());

    holder.removeBagItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

}


@Override
public int getItemCount() {
    return mBagList.size();
}

class BagViewHolder extends RecyclerView.ViewHolder{

    @BindView(R.id.bag_item_image) ImageView bagItemImage;
    @BindView(R.id.remove_bag_item) ImageView removeBagItem;
    @BindView(R.id.bag_item_title_brand) TextView bagItemTitleBrand;
    @BindView(R.id.bag_item_description) TextView bagItemDescription;
    @BindView(R.id.bag_item_quantity) TextView bagItemQuantity;
    @BindView(R.id.bag_item_price) TextView bagItemPrice;
    @BindView(R.id.bag_item_product) TextView bagItemProduct;


    public BagViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this,itemView);
    }
                }
              }

The Fragment code:

public class BagFragment extends Fragment implements BagView {

@BindView(R.id.bag_items_recycler)RecyclerView mRecycler;
@BindView(R.id.edit_bag_items_text)TextView editBagItems;
@BindView(R.id.exit_bag_image)ImageView exitBagImage;
@BindView(R.id.no_items_in_bag_image)ImageView noItemsImage;
@BindView(R.id.no_items_in_bag_text)TextView noItemsText;
@BindView(R.id.my_bag_title_text)TextView myBagTitleText;
@BindView(R.id.check_out_button)Button checkOutButton;

BagPresenter mPresenter;
ItemTouchHelper itemTouchHelper;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPresenter = new BagPresenter(getActivity(),this);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.shopping_bag_fragment_layout,container,false);
    ButterKnife.bind(this,v);
    mPresenter.getBagItems();
    return v;
}



@OnClick({R.id.exit_bag_image,R.id.edit_bag_items_text,R.id.check_out_button})
public void onBagViewsClicked(View v)
{
    switch (v.getId())
    {
        case R.id.exit_bag_image:
            getActivity().getSupportFragmentManager().popBackStack();
            break;
        case R.id.edit_bag_items_text:
            if(editBagItems.getText().equals(getActivity().getResources().getString(R.string.edit_string)))
            {
             editBagItems.setText(getActivity().getResources().getString(R.string.done_string));
                editBagItems.setTypeface(Typeface.DEFAULT_BOLD);
                //TODO here is where I should make the delete ImageView visible for each item
          ((BagItemsAdapter)mRecycler.getAdapter()).showDeleteImage(); "this does not work because i can't get the ViewHolder from the adapter"
            }else if(editBagItems.getText().equals(getActivity().getResources().getString(R.string.done_string)))
            {
                editBagItems.setText(getActivity().getResources().getString(R.string.edit_string));
                editBagItems.setTypeface(Typeface.DEFAULT);
                //TODO here is where I should hide the delete ImageView
              ((BagItemsAdapter)mRecycler.getAdapter()).hideDeleteImage(); "this does not work"
            }


            }
    }
}

Any help is highly appreciated! :)

Upvotes: 0

Views: 2913

Answers (2)

muazhud
muazhud

Reputation: 954

First you need to add a new property to your BagItem. We'll called it editMode.

class BagItem {
    boolean editMode;
}

Initially editMode is false. When the user push edit button, you need to change editMode to true. Make sure that your mBagList is set to public. Next you need to notify the recycler view adapter that the data was changed.

for (BagItem bagItem: bagItemsAdapter.mBagList) {
    bagItem.editMode = true;
}
bagItemsAdapter.notifyDataSetChanged();

You also need to update the visibility of removeBagItem according to editMode value.

holder.removeBagItem.setVisibility(currentItem.editMode ? View.VISIBLE : View.GONE);

Upvotes: 3

Munir
Munir

Reputation: 2558

  1. first you make one boolean global variable in adapter class for example.

    private boolean isVisible = false;
    
  2. According to that variable set visibility of imageview in onBindViewHolder say for example

    if(isVisible){
      holder.removeBagItem.setVisibility(View.VISIBLE);
    }else{
      holder.removeBagItem.setVisibility(View.GONE);
    }
    
  3. Create on public method which set visibility of imageView in adapter for example

    public void setVisibility(){
      isVisible = true;
      this.notifyDataSetChanged();
    }
    
  4. from fragment edit text click call that adapter method for example.

    adapter.setVisibility();
    

Upvotes: 1

Related Questions