Эта библиотека позволяет расширять и сворачивать представления. В настоящее время, если у меня уже есть развернутое представление, и я пытаюсь развернуть другое представление, первое сворачивается на это свое. Я хочу, чтобы несколько просмотров расширялись одновременно.
Activity_main. xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recc2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity. java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recc2);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new SimpleAdapter(recyclerView,this));
}
private static class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.ViewHolder> {
private static final int UNSELECTED = -1;
private RecyclerView recyclerView;
private Context context;
private int selectedItem = UNSELECTED;
SimpleAdapter(RecyclerView recyclerView, Context context) {
this.context=context;
this.recyclerView = recyclerView;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind();
}
@Override
public int getItemCount() {
return 100;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, ExpandableLayout.OnExpansionUpdateListener {
private ExpandableLayout expandableLayout;
private TextView expandButton;
private RecyclerView recyclerView4;
public ViewHolder(View itemView) {
super(itemView);
expandableLayout = itemView.findViewById(R.id.expandable_layout);
expandableLayout.setInterpolator(new OvershootInterpolator());
expandButton = itemView.findViewById(R.id.expand_button);
expandButton.setOnClickListener(this);
recyclerView4=itemView.findViewById(R.id.recycler3);
}
public void bind() {
int position = getAdapterPosition();
boolean isSelected = position == selectedItem;
expandButton.setText(position + ". Tap to expand");
expandButton.setSelected(isSelected);
expandableLayout.setExpanded(isSelected, false);
Adapter_two adapter_two = new Adapter_two();
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerView4.setLayoutManager(layoutManager);
recyclerView4.setAdapter(adapter_two);
}
@Override
public void onClick(View view) {
ViewHolder holder = (ViewHolder) recyclerView.findViewHolderForAdapterPosition(selectedItem);
if (holder != null) {
holder.expandButton.setSelected(false);
holder.expandableLayout.collapse();
}
int position = getAdapterPosition();
if (position == selectedItem) {
selectedItem = UNSELECTED;
} else {
expandButton.setSelected(true);
expandableLayout.expand();
selectedItem = position;
}
}
@Override
public void onExpansionUpdate(float expansionFraction, int state) {
Log.d("ExpandableLayout", "State: " + state);
if (state == ExpandableLayout.State.EXPANDING) {
recyclerView.smoothScrollToPosition(getAdapterPosition());
}
}
}
}
}
адаптер два отвечает за заполнение 20 изображений в каждой строке. Adapter_two. java:
class Adapter_two extends RecyclerView.Adapter<Adapter_two.TwoHolder> {
@NonNull
@Override
public TwoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.viewholder_menuitem, parent, false);
return new TwoHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull TwoHolder holder, int position) {
}
@Override
public int getItemCount() {
return 20;
}
class TwoHolder extends RecyclerView.ViewHolder {
TwoHolder(@NonNull View itemView) {
super(itemView);
}
}
}
recycler_item. xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/expand_button"
style="@style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="6dp"
android:foreground="?selectableItemBackground"
android:padding="16dp" />
<net.cachapa.expandablelayout.ExpandableLayout
android:id="@+id/expandable_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
app:el_duration="300"
app:el_expanded="true"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
</net.cachapa.expandablelayout.ExpandableLayout>
</LinearLayout>