Skip to content

Commit

Permalink
[Carousel][A11y] Fix some a11y bugs in Carousel
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 559881261
  • Loading branch information
imhappi committed Aug 25, 2023
1 parent 5f1cab6 commit 3d84841
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Expand Up @@ -18,6 +18,8 @@

import io.material.catalog.R;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;
Expand Down Expand Up @@ -48,10 +50,14 @@ public class FullScreenStrategyDemoFragment extends DemoFragment {

@NonNull
@Override
@SuppressLint("SourceLockedOrientationActivity")
public View onCreateDemoView(
@NonNull LayoutInflater layoutInflater,
@Nullable ViewGroup viewGroup,
@Nullable Bundle bundle) {
// We want to force portrait mode for the fullscreen vertical carousel
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

return layoutInflater.inflate(
R.layout.cat_carousel_full_screen_fragment, viewGroup, false /* attachToRoot */);
}
Expand Down Expand Up @@ -90,8 +96,7 @@ public void onClick(View v) {
RecyclerView fullscreenRecyclerView =
view.findViewById(R.id.fullscreen_carousel_recycler_view);
CarouselLayoutManager carouselLayoutManager =
new CarouselLayoutManager(new FullScreenCarouselStrategy(),
RecyclerView.VERTICAL);
new CarouselLayoutManager(new FullScreenCarouselStrategy(), RecyclerView.VERTICAL);
carouselLayoutManager.setDebuggingEnabled(
fullscreenRecyclerView, debugSwitch.isChecked());
fullscreenRecyclerView.setLayoutManager(carouselLayoutManager);
Expand Down
Expand Up @@ -1211,9 +1211,13 @@ public boolean requestChildRectangleOnScreen(
getOffsetToScrollToPosition(
getPosition(child), getKeylineStateForPosition(getPosition(child)));
if (!focusedChildVisible) {
// TODO(b/266816148): Implement smoothScrollBy when immediate is false.
if (delta != 0) {
// TODO(b/266816148): Implement smoothScrollBy when immediate is false.
parent.scrollBy(delta, 0);
if (isHorizontal()) {
parent.scrollBy(delta, 0);
} else {
parent.scrollBy(0, delta);
}
return true;
}
}
Expand Down Expand Up @@ -1307,7 +1311,12 @@ public int computeHorizontalScrollOffset(@NonNull State state) {
*/
@Override
public int computeHorizontalScrollExtent(@NonNull State state) {
return keylineStateList == null ? 0 : (int) keylineStateList.getDefaultState().getItemSize();
if (getChildCount() == 0 || keylineStateList == null || getItemCount() <= 1) {
return 0;
}
float itemRatio =
(keylineStateList.getDefaultState().getItemSize() / computeHorizontalScrollRange(state));
return (int) (getWidth() * itemRatio);
}

/**
Expand All @@ -1329,7 +1338,12 @@ public int computeVerticalScrollOffset(@NonNull State state) {

@Override
public int computeVerticalScrollExtent(@NonNull State state) {
return keylineStateList == null ? 0 : (int) keylineStateList.getDefaultState().getItemSize();
if (getChildCount() == 0 || keylineStateList == null || getItemCount() <= 1) {
return 0;
}
float itemRatio =
(keylineStateList.getDefaultState().getItemSize() / computeVerticalScrollRange(state));
return (int) (getHeight() * itemRatio);
}

@Override
Expand Down

0 comments on commit 3d84841

Please sign in to comment.