0

I have a RecyclerView which loads its data from a SQLite database. Each row has 3 EditTexts and 1 Checkbox:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/row_content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:padding="6dp"
            android:background="?android:attr/selectableItemBackground">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/element_name_editText"
        android:singleLine="true"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/element_quantity_editText"
        android:singleLine="true"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/element_price_editText"
        android:singleLine="true"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/element_checkBox"/>

This is the result:

View_1

What I am trying to achieve is to hide two views (element_quantity_editText and element_price_editText) when clicking a button in the fragment containing the RecyclerView. The result would be something like this

View_2

For now I tried using multiple ViewTypes as in How to create RecyclerView with multiple view type?

However, this only applies to the new added elements and not to the rest of them.

This is what I have now:

Myadapter

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    mShow=mPrefs.getBoolean("0", true);

        if (viewType==VIEW_TYPE_ELEMENT){

            if(mShow) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.elements_item, parent, false);
            ElementsRowHolder vh = new ElementsRowHolder(v,mShow);
            return vh;
            }
            else{
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.elements_item_0, parent, false);//elements_item_0 only contains TITLE and CHECKBOX
                ElementsRowHolder vh = new ElementsRowHolder(v,mShow);
                return vh;
            }
        }
        else {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_element_item, parent, false);
            NewElementRowHolder vh = new NewElementRowHolder(v);
            return vh;
        }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ElementsRowHolder) {
        ((ElementsRowHolder)holder).bindModel(mData.get(position));
        }
}

@Override
public int getItemCount() {
        return(mData.size()+1);
}

@Override
public int getItemViewType(int position) {
    return (position == mData.size()) ? VIEW_TYPE_BUTTON : VIEW_TYPE_ELEMENT;
}

My ViewHolder

public ElementsRowHolder(View row, boolean mShow) {
    super(row);

    this.mShow = mShow;

    mElementName=(EditText)row.findViewById(R.id.element_name_editText);
    mElementQuantity=(EditText)row.findViewById(R.id.element_quantity_editText);
    mElementPrice=(EditText)row.findViewById(R.id.element_price_editText);
    mElementCheckBox=(CheckBox)row.findViewById(R.id.element_checkBox);

    if(mShow) {
        mElementQuantity.setVisibility(View.VISIBLE);
        mElementPrice.setVisibility(View.VISIBLE);
        mElementName.setVisibility(View.VISIBLE);
        mElementCheckBox.setVisibility(View.VISIBLE);
    }
    else {
        mElementQuantity.setVisibility(View.GONE);
        mElementPrice.setVisibility(View.GONE);
        mElementName.setVisibility(View.VISIBLE);
        mElementCheckBox.setVisibility(View.VISIBLE);
    }

    row.setOnClickListener(this);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        row.setOnTouchListener(new View.OnTouchListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.findViewById(R.id.row_content).getBackground().setHotspot(event.getX(), event.getY());
                return(false);
            }
        });
    }
}

void bindModel(Element e) {

    mElementName.setText(e.getName());
    if (e.getQuantity() == null && e.getCost() == null) {
    } else {
        mElementQuantity.setText(e.getQuantity().toString());
        mElementPrice.setText(e.getCost().toString());
    }
    mElementCheckBox.setActivated(false);

}
Community
  • 1
  • 1
hwiz
  • 1
  • 1
  • 4

2 Answers2

0

In your adapter, add a boolean attribute (i.e. private boolean mShowsView), set the true or false according to the visibility of your views.

In the onBindViewHolder method of your adapter, display the views only if mShowsView is set to "true". You can play with the

setVisibility(View.VISIBLE / View.GONE);

In your fragment, when you click on the button, do the following:

`mAdapter.setShowsView(true/false);` 
// to show or hide your views
mAdapter.notifyDataSetChanged(); 
// to refresh your adapter
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
Suiko
  • 66
  • 3
  • This works but only for the new added elements to the Recycler View, what I am trying to achieve is to apply it to all the elements (new and old) – hwiz Aug 29 '15 at 11:03
  • This method would also work for older items: just play with the visibility for every elements. In your case, this.mShow = mShow should be called in the constructor of the adapter, not in every view binding. – Suiko Aug 30 '15 at 14:41
0

In your adapter class, make a variable

int var_visibility = View.VISIBLE;

In your ViewHolder, do following.

element_quantity_editText.setVisibility(var_visibility);
element_price_editText.setVisibility(var_visibility);

Now, in your checkbox listener do this

if(checkbox.isChecked)
    var_visibility = View.VISIBLE;
else
    var_visibility = View.GONE;

notifyDataSetChanged();
loadedion
  • 2,217
  • 19
  • 41
Rusheel Jain
  • 843
  • 6
  • 20