2015-08-28 114 views
0

我有一個RecyclerView從SQLite數據庫加載其數據。每一行都有3 EditText秒和1 Checkbox從RecyclerView隱藏視圖

<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_editTextelement_price_editText )當點擊包含RecyclerView的片段中的按鈕時。其結果將是這樣的

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

} 
+0

發佈您的代碼,那麼我們就可以跟蹤你... – TheGreat004

+0

增加了更多的代碼,希望這就夠了! – hwiz

回答

0

在你的適配器類,做一個變量

int var_visibility = View.VISIBLE; 

在你的ViewHolder,做下面的事情。現在

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

,在您的複選框聽衆做

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

notifyDataSetChanged(); 
0

在適配器中,添加一個boolean屬性(即private boolean mShowsView),設置true或false根據您的意見的知名度。

在適配器的onBindViewHolder方法中,僅當mShowsView設置爲「true」時才顯示視圖。你可以用

setVisibility(View.VISIBLE/View.GONE); 

打在你的fragment,當你點擊該按鈕,請執行下列操作:

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

這種方法,但僅適用於Recycler View的新增元素,我試圖實現的是將其應用於所有元素(新舊) – hwiz

+0

此方法也適用於較舊的項目:只需使用可見性每個元素。 在你的情況下,應該在適配器的構造函數中調用this.mShow = mShow,而不是在每個視圖綁定中調用。 – Suiko

+0

您能否提供一些代碼或示例? – hwiz