我有一個回收站視圖的問題。我有幾張可以擴展的卡片視圖。當我點擊其中一個並向下滾動時,另一個也被打開,但是當我向後滾動時,應該打開的那個被關閉。它不一致,很混亂,我甚至不知道如何描述它。感謝您的幫助。Recycler View - 一個視圖打開另一個視圖
回收站查看商品佈局:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/card_margin_bottom"
android:layout_marginLeft="@dimen/card_margin_sides"
android:layout_marginRight="@dimen/card_margin_sides"
android:layout_marginTop="@dimen/card_margin_top">
<LinearLayout
style="@style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/headerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textSize="@dimen/adapter_card_view_title_text_size"/>
<TextView
android:id="@+id/expandText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="@dimen/adapter_card_view_expand_text_size"
android:visibility="gone"/>
</LinearLayout>
適配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private String[] headerText;
private String[] techniqueText;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView headerText;
public TextView expandText;
public CardView mCardView;
public ViewHolder(CardView v) {
super(v);
mCardView = v;
headerText = (TextView) v.findViewById(R.id.headerText);
expandText = (TextView) v.findViewById(R.id.expandText);
}
}
public RecyclerViewAdapter(String[] firstParam, String[] secondParam) {
headerText = firstParam;
techniqueText = secondParam;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_layout, parent, false);
ViewHolder vh = new ViewHolder((CardView) v,parent.getContext());
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.headerText.setText(headerText[position]);
holder.expandText.setText(techniqueText[position]);
holder.mCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.expandText.getVisibility() == View.GONE) {
holder.expandText.setVisibility(View.VISIBLE);
}
else if (holder.expandText.getVisibility() == View.VISIBLE){
holder.expandText.setVisibility(View.GONE);
}
}
});
}
@Override
public int getItemCount() {
return headerText.length;
}
}
回收站V瀏覽器設置:
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_x);
mRecyclerView.setHasFixedSize(false);
mLayoutManager = new LinearLayoutManager(this.getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerViewAdapter(firstParam,secondParam);
mRecyclerView.setAdapter(mAdapter);
這是因爲recyclerview回收細胞。 –
記住視圖被重用,所以你需要在適配器的'getView'中重新初始化每個視圖的狀態。 – dharms