我有回收站視圖。我在列表項視圖中添加了可展開的視圖。單擊它可以展開並顯示佈局。但是現在如果打開它,我想點擊相同的項目關閉它。如何在第二次點擊時關閉展開的列表項目(如果展開的話)?
現在,如果我點擊第1項,佈局會被展開,然後如果我點擊第2項的佈局,第2項會被展開,第1項佈局會被關閉。
我按照這個鏈接:
RecyclerView expand/collapse items
適配器:
public class BookingsAdapter extends RecyclerView.Adapter<BookingsAdapter.MyViewHolder> implements View.OnClickListener{
private int expandedPosition = -1;
private List<Bookings> bookingsList;
int status;
Context context;
public interface OnItemClickListener {
void onItemClick(Events item);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView eventName,eventType,eventDateTime,userName;
public RelativeLayout expandLayout;
public CardView cardView;
public MyViewHolder(View view) {
super(view);
eventName = (TextView) view.findViewById(R.id.text_eventName);
eventType = (TextView) view.findViewById(R.id.textView_EventType);
eventDateTime = (TextView) view.findViewById(R.id.text_dateTime);
userName = (TextView) view.findViewById(R.id.text_userName);
expandLayout = (RelativeLayout) view.findViewById(R.id.expandLayout);
cardView = (CardView) view.findViewById(R.id.card_view);
}
}
public BookingsAdapter(ArrayList<Bookings> bookingsList,Context context) {
this.bookingsList = bookingsList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.bookings_card, parent, false);
MyViewHolder holder = new MyViewHolder(itemView);
// Sets the click adapter for the entire cell
// to the one in this class.
holder.itemView.setOnClickListener(BookingsAdapter.this);
holder.itemView.setTag(holder);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Bookings bookings = bookingsList.get(position);
holder.eventName.setText(bookings.getEventName());
holder.eventType.setText(bookings.getEventType());
holder.eventDateTime.setText(bookings.getEventDateTime());
holder.userName.setText(bookings.getUserName());
if (position == expandedPosition) {
holder.expandLayout.setVisibility(View.VISIBLE);
} else {
holder.expandLayout.setVisibility(View.GONE);
}
}
@Override
public void onClick(View view) {
MyViewHolder holder = (MyViewHolder) view.getTag();
Bookings bookingsItem = bookingsList.get(holder.getPosition());
// Check for an expanded view, collapse if you find one
if (expandedPosition >= 0) {
int prev = expandedPosition;
notifyItemChanged(prev);
}
// Set the current position to "expanded"
expandedPosition = holder.getPosition();
notifyItemChanged(expandedPosition);
Toast.makeText(context, "Clicked: "+ bookingsItem.getEventName(), Toast.LENGTH_SHORT).show();
}
@Override
public int getItemCount() {
return bookingsList.size();
}
}
佈局的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@android:color/white"
android:layout_marginTop="05dp"
android:layout_marginRight="05dp"
android:layout_marginLeft="05dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parentLayout">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="90dp"
android:id="@+id/detailsLayout">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="05dp"
android:layout_marginBottom="05dp"
android:id="@+id/eventLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event name"
android:id="@+id/text_eventName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="14sp"
android:textColor="@android:color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="("
android:id="@+id/textView26"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/textView_EventType"
android:textSize="12sp"
android:textColor="@color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date and time"
android:id="@+id/text_dateTime"
android:layout_below="@+id/textView26"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="05dp"
android:textSize="12sp"
android:textColor="@color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event type"
android:id="@+id/textView_EventType"
android:layout_below="@+id/text_eventName"
android:layout_toRightOf="@+id/textView26"
android:layout_toEndOf="@+id/textView26"
android:layout_marginTop="05dp"
android:textSize="12sp"
android:textColor="@color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")"
android:id="@+id/textView29"
android:layout_alignTop="@+id/textView_EventType"
android:layout_toRightOf="@+id/textView_EventType"
android:layout_toEndOf="@+id/textView_EventType"
android:textSize="12sp"
android:textColor="@color/colorAccent" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/place_autocomplete_separator"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_alignParentTop="true">
</View>
</RelativeLayout>
<View
android:layout_width="3dp"
android:layout_height="wrap_content"
android:background="@color/grey">
</View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/eventLayout"
android:layout_toRightOf="@+id/eventLayout"
android:layout_toEndOf="@+id/eventLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/eventLayout"
android:id="@+id/profilepicLayout">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/ic_person_black_48dp"
android:id="@+id/profileImage"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="false"
android:background="@drawable/circle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:id="@+id/text_userName"
android:layout_below="@+id/profileImage"
android:layout_marginTop="05dp"
android:textColor="@android:color/black"
android:textSize="12sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_below="@+id/detailsLayout"
android:id="@+id/expandLayout"
android:visibility="gone">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/place_autocomplete_separator"></View>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/imageView12"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/ic_call_black_18dp"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="05dp"
android:layout_marginBottom="05dp" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/imageView13"
android:layout_alignTop="@+id/imageView12"
android:layout_centerHorizontal="true"
android:background="@drawable/ic_textsms_black_18dp" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/imageView14"
android:layout_alignParentBottom="false"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:background="@drawable/ic_chat_black_18dp"
android:layout_centerVertical="true"
android:layout_alignTop="@+id/imageView13" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
編輯:我在課堂上添加布爾變量,這樣做, 。注意到了。當我點擊項目時,佈局不會關閉
@Override
public void onClick(View view) {
MyViewHolder holder = (MyViewHolder) view.getTag();
Bookings bookingsItem = bookingsList.get(holder.getPosition());
// Check for an expanded view, collapse if you find one
// set previously expanded row to false
for(int i=0;i<bookingsList.size();i++)
{
if(bookingsList.get(i).expanded)
{
bookingsList.get(i).expanded = false;
}
}
//set current item expanded
bookingsList.get(holder.getPosition()).expanded = true;
if (expandedPosition >= 0) {
int prev = expandedPosition;
notifyItemChanged(prev);
}
// Set the current position to "expanded"
expandedPosition = holder.getPosition();
notifyItemChanged(expandedPosition);
Toast.makeText(context, "Clicked: "+ bookingsItem.getEventName(), Toast.LENGTH_SHORT).show();
}
現在我想要關閉項目點擊時同一項目的展開佈局。
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ –
從onclick部分刪除你的if()條件。並測試它 – Tara
可能重複[按鈕在可擴展列表視圖android](http://stackoverflow.com/questions/9880950/button-in-expandable-listview-android) – karan