我有一個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"/>
這是結果:
我想實現是隱藏兩個視圖(element_quantity_editText
和element_price_editText
)當點擊包含RecyclerView
的片段中的按鈕時。其結果將是這樣的
現在我嘗試使用多個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);
}
發佈您的代碼,那麼我們就可以跟蹤你... – TheGreat004
增加了更多的代碼,希望這就夠了! – hwiz