隐藏RecyclerView中的视图

时间:2015-08-28 20:50:56

标签: android android-recyclerview

我有一个RecyclerView从SQLite数据库加载其数据。每行有3 EditText s和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"/>

结果如下:

View_1

我想要实现的是在单击包含element_quantity_editText的片段中的按钮时隐藏两个视图(element_price_editTextRecyclerView)。结果将是这样的

View_2

目前我尝试使用多个ViewTypes,如How to create RecyclerView with multiple view type?

但是,这仅适用于新添加的元素,而不适用于其他元素。

这就是我现在所拥有的:

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;
}

我的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);

}

2 个答案:

答案 0 :(得分:0)

在适配器中,添加boolean属性(即private boolean mShowsView),根据视图的可见性设置true或false。

在适配器的onBindViewHolder方法中,仅当mShowsView设置为&#34; true&#34;时才显示视图。你可以玩

setVisibility(View.VISIBLE / View.GONE);

fragment中,当您点击按钮时,请执行以下操作:

`mAdapter.setShowsView(true/false);` 
// to show or hide your views
mAdapter.notifyDataSetChanged(); 
// to refresh your adapter

答案 1 :(得分:0)

在适配器类中,创建一个变量

SELECT @SEQ = Isnull(@SEQ+1,0)

在您的ViewHolder中,执行以下操作。

int var_visibility = View.VISIBLE;

现在,在你的复选框监听器中执行此操作

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