Skip to content

Commit

Permalink
Ensure expanded items are always visible in RecyclerView demo
Browse files Browse the repository at this point in the history
  • Loading branch information
cachapa committed May 13, 2017
1 parent c214263 commit 653253f
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new SimpleAdapter(recyclerView));

return rootView;
}

private static class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.ViewHolder> {
private static final int UNSELECTED = -1;

private RecyclerView recyclerView;
private int selectedItem = UNSELECTED;

Expand All @@ -53,8 +53,7 @@ public int getItemCount() {
return 100;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, ExpandableLayout.OnExpansionUpdateListener {
private ExpandableLayout expandableLayout;
private TextView expandButton;
private int position;
Expand All @@ -64,16 +63,17 @@ public ViewHolder(View itemView) {

expandableLayout = (ExpandableLayout) itemView.findViewById(R.id.expandable_layout);
expandableLayout.setInterpolator(new OvershootInterpolator());
expandableLayout.setOnExpansionUpdateListener(this);
expandButton = (TextView) itemView.findViewById(R.id.expand_button);

expandButton.setOnClickListener(this);
}

public void bind(int position) {
this.position = position;

expandButton.setText(position + ". Tap to expand");

expandButton.setSelected(false);
expandableLayout.collapse(false);
}
Expand All @@ -85,7 +85,7 @@ public void onClick(View view) {
holder.expandButton.setSelected(false);
holder.expandableLayout.collapse();
}

if (position == selectedItem) {
selectedItem = UNSELECTED;
} else {
Expand All @@ -94,6 +94,11 @@ public void onClick(View view) {
selectedItem = position;
}
}

@Override
public void onExpansionUpdate(float expansionFraction) {
recyclerView.smoothScrollToPosition(getAdapterPosition());
}
}
}
}

0 comments on commit 653253f

Please sign in to comment.