hwiz hwiz - 6 months ago 40
Android Question

Hide Views from RecyclerView

I have a

RecyclerView
which loads its data from a SQLite database. Each row has 3
EditText
s 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);

}

Answer

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();
Comments