ExpandableLayout
ExpandableLayout copied to clipboard
RecyclerView expanding all items
Hi,
I'm using the ExpansiveLinearLayout and I'm setting the adapter in the onResume method of my activity. All items are collapsed but when I return to this activity all items are expanded. Is there any problem related to setting the adapter for the ExpandableLinearLayout in the onResume method?
Following my code:
activity_items.xml
<com.github.aakira.expandablelayout.ExpandableLinearLayout
android:id="@+id/list_item_car_expandable_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:ael_duration="400"
app:ael_expanded="false">
<TextView
android:id="@+id/list_item_car_expandable_layout_information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:visibility="visible"
android:weightSum="2">
</LinearLayout>
</com.github.aakira.expandablelayout.ExpandableLinearLayout>
CustomAdapter.java
private final List<Item> mItems;
private Context mContext;
private SparseBooleanArray mExpandState = new SparseBooleanArray();
public static class ItemView extends RecyclerView.ViewHolder {
TextView textDisplayName;
TextView textInformation;
ExpandableLinearLayout expandableLayout;
View buttonLayout;
public ItemView(View view) {
super(view);
textDisplayName = (TextView) view.findViewById(R.id.list_item_car_display_name);
textInformation = (TextView) view.findViewById(R.id.list_item_car_expandable_layout_information);
expandableLayout = (ExpandableLinearLayout) view.findViewById(R.id.list_item_car_expandable_layout);
buttonLayout = view.findViewById(R.id.list_item_car_expand_button);
}
}
public CustomAdapter(final List<Item> aItems) {
this.mItems = aItems;
for (int i = 0; i < mItems.size(); i++) {
mExpandState.append(i, false);
}
}
@Override
public ItemView onCreateViewHolder(final ViewGroup parent, final int viewType) {
this.mContext = parent.getContext();
return new ItemView(LayoutInflater.from(mContext).inflate(R.layout.list_item_custom, parent, false));
}
private void onClickButton(final ExpandableLinearLayout expandableLayout) {
expandableLayout.toggle();
}
@Override
public void onBindViewHolder(final ItemView holder, final int position) {
final Item item = mItems.get(position);
holder.setIsRecyclable(true);
holder.textDisplayName.setText(item.getDisplayName());
holder.textDisplayName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickButton(holder.expandableLayout);
}
});
holder.expandableLayout.setInRecyclerView(true);
holder.expandableLayout.setExpanded(mExpandState.get(position));
holder.expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {
@Override
public void onPreOpen() {
createRotateAnimator(holder.buttonLayout, 0f, 180f).start();
mExpandState.put(position, true);
}
@Override
public void onPreClose() {
createRotateAnimator(holder.buttonLayout, 180f, 0f).start();
mExpandState.put(position, false);
}
});
holder.buttonLayout.setRotation(mExpandState.get(position) ? 180f : 0f);
holder.buttonLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
onClickButton(holder.expandableLayout);
}
});
holder.textInformation.setText(tem.getInformation());
}
@Override
public int getItemCount() {
return mItems.size();
}
public ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
animator.setDuration(300);return animator;
}
MyActivity.java
public class MyActivity extends AppCompatActivity {
@ViewById(R.id.activity_cars_recycler_view)
protected RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.activity_items);
mRecyclerView = (RecyclerView) findViewById(R.id.activity_recycler_view);
}
@Override
public void onResume(){
super.onResume();
loadItems();
}
private void loadItems(){
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
List<Item> items = CustomController.getInstance.getAllItems();;
mRecyclerViewsetAdapter(new CustomAdapter(items));
}
}
Thanks in advance, Vinicius Lafourcade
I am facing the same problem. Why the there is no remedy of this? I have followed the document but still no solution. Its frustrating.
I think you should use holder.setIsRecyclable(false) instead of holder.setIsRecyclable(true);