diff options
author | Agatha Man <agathaman@google.com> | 2020-05-19 16:31:14 -0700 |
---|---|---|
committer | Agatha Man <agathaman@google.com> | 2020-06-01 19:38:36 +0000 |
commit | 55340419ca385e12794eacea70eedcb38ffc5450 (patch) | |
tree | 9a59b9fc4a5dc9d4940988b1dd306cc97dc9f6f6 /RotaryPlayground/src/com/android/car/rotaryplayground | |
parent | eb8e69fe8990667bda922f0445359b99e5e39263 (diff) | |
download | tests-55340419ca385e12794eacea70eedcb38ffc5450.tar.gz |
Add a Scroll demo to RotaryPlayground
Bug: 154971276
Test: make and install
Change-Id: Iaa884c4920b910115440ed8181b7fbd6c5ddafba
Diffstat (limited to 'RotaryPlayground/src/com/android/car/rotaryplayground')
3 files changed, 199 insertions, 8 deletions
diff --git a/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryActivity.java b/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryActivity.java index 1b44ac6..32ab6a5 100644 --- a/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryActivity.java +++ b/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryActivity.java @@ -24,6 +24,8 @@ import android.util.Log; import android.view.View; import android.widget.Toast; +import java.util.Random; + /** * The main activity for Rotary Playground * @@ -33,7 +35,8 @@ import android.widget.Toast; public class RotaryActivity extends FragmentActivity { private static final String TAG = "RotaryActivity"; - private static final String MESSAGE = "Hello there!"; + + private final Random mRandom = new Random(); private Fragment mMenuFragment = null; @@ -44,9 +47,10 @@ public class RotaryActivity extends FragmentActivity { showMenuFragment(); } - /** Event handler for buttons in res/layout/rotary_cards.xml */ - public void onRotaryCardsButtonClick(View v) { - showToast(MESSAGE); + /** Event handler for button clicks. */ + public void onRotaryButtonClick(View v) { + final String[] greetings = getResources().getStringArray(R.array.greetings); + showToast(greetings[mRandom.nextInt(greetings.length)]); } private void showMenuFragment() { diff --git a/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryMenu.java b/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryMenu.java index d54a721..d28d007 100644 --- a/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryMenu.java +++ b/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryMenu.java @@ -33,10 +33,12 @@ public class RotaryMenu extends Fragment { private Fragment mRotaryCards = null; private Fragment mRotaryGrid = null; private Fragment mDirectManipulation = null; + private Fragment mScrollFragment = null; private Button mCardButton; private Button mGridButton; private Button mDirectManipulationButton; + private Button mScrollButton; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @@ -56,6 +58,11 @@ public class RotaryMenu extends Fragment { (v, hasFocus) -> showDirectManipulationExamples(hasFocus)); mDirectManipulationButton.setOnClickListener( (v -> showDirectManipulationExamples(/* hasFocus= */ true))); + + mScrollButton = view.findViewById(R.id.scroll); + mScrollButton.setOnFocusChangeListener((v, hasFocus) -> showScrollFragment(hasFocus)); + mScrollButton.setOnClickListener(v -> showScrollFragment(/* hasFocus= */ true)); + return view; } @@ -66,7 +73,7 @@ public class RotaryMenu extends Fragment { if (mRotaryCards == null) { mRotaryCards = new RotaryCards(); } - showContent(mRotaryCards); + showFragment(mRotaryCards); } private void showGridExample(boolean hasFocus) { @@ -76,7 +83,7 @@ public class RotaryMenu extends Fragment { if (mRotaryGrid == null) { mRotaryGrid = new RotaryGrid(); } - showContent(mRotaryGrid); + showFragment(mRotaryGrid); } // TODO(agathaman): refactor this and the showRotaryCards above into a @@ -88,10 +95,20 @@ public class RotaryMenu extends Fragment { if (mDirectManipulation == null) { mDirectManipulation = new RotaryDirectManipulationWidgets(); } - showContent(mDirectManipulation); + showFragment(mDirectManipulation); + } + + private void showScrollFragment(boolean hasFocus) { + if (!hasFocus) { + return; // Do nothing if no focus. + } + if (mScrollFragment == null) { + mScrollFragment = new ScrollFragment(); + } + showFragment(mScrollFragment); } - private void showContent(Fragment fragment) { + private void showFragment(Fragment fragment) { getFragmentManager().beginTransaction() .replace(R.id.rotary_content, fragment) .commit(); diff --git a/RotaryPlayground/src/com/android/car/rotaryplayground/ScrollFragment.java b/RotaryPlayground/src/com/android/car/rotaryplayground/ScrollFragment.java new file mode 100644 index 0000000..49e341d --- /dev/null +++ b/RotaryPlayground/src/com/android/car/rotaryplayground/ScrollFragment.java @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.car.rotaryplayground; + +import android.content.Context; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** Fragment to demo scrolling with CarRecyclerView. */ +public class ScrollFragment extends Fragment { + + // Item types + private static final int TYPE_BUTTONS = 1; + private static final int TYPE_TEXT = 2; + + private View mScrollView; + + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + mScrollView = inflater.inflate(R.layout.rotary_scroll, container, false); + initView(); + return mScrollView; + } + + private void initView() { + List<ScrollListItem> items = new ArrayList<>(); + + // List of height for items to be added to the demo. + // 0 indicates focusable buttons inflated by R.layout.rotary_scroll.button. + // Values > 0 indiciates non-focusable texts inflated by R.layout.rotary_scroll_text. + final int[] itemHeights = getResources().getIntArray(R.array.scroll_item_heights); + + for (int height : itemHeights) { + if (height < 0) { + continue; + } + ScrollListItem item = (height == 0) + ? ScrollListItem.createButtonsItem() + : ScrollListItem.createTextItemWithHeight(height); + items.add(item); + } + + // Set adapter + Context context = getContext(); + ScrollListItemAdapter adapter = new ScrollListItemAdapter(context, items); + RecyclerView view = (RecyclerView) mScrollView.findViewById(R.id.rotary_scroll_view); + view.setAdapter(adapter); + } + + /** The adapter used by RotaryScroll to render ScrollListItems. */ + private static class ScrollListItemAdapter extends + RecyclerView.Adapter<RecyclerView.ViewHolder> { + + final Context mContext; + final List<ScrollListItem> mItems; + + ScrollListItemAdapter(Context context, List<ScrollListItem> items) { + this.mContext = context; + this.mItems = items; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder( + @NonNull ViewGroup viewGroup, int viewType) { + View view; + switch (viewType) { + case TYPE_BUTTONS: + view = LayoutInflater.from(mContext).inflate( + R.layout.rotary_scroll_button, viewGroup, false); + return new RecyclerView.ViewHolder(view) {}; + + case TYPE_TEXT: + view = LayoutInflater.from(mContext).inflate( + R.layout.rotary_scroll_text, viewGroup, false); + return new ScrollTextHolder(view); + + default: + throw new IllegalArgumentException("Unexpected viewType: " + viewType); + } + } + + @Override + public int getItemViewType(int position) { + return mItems.get(position).getType(); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { + if (getItemViewType(position) == TYPE_TEXT) { + ((ScrollTextHolder) viewHolder).setHeight(mItems.get(position).getHeight()); + } + } + + @Override + public int getItemCount() { + return mItems.size(); + } + } + + /** A ViewHolder for non-focusable ScrollListItems */ + private static class ScrollTextHolder extends RecyclerView.ViewHolder { + + @NonNull + final TextView mScrollTextView; + + ScrollTextHolder(@NonNull View itemView) { + super(itemView); + mScrollTextView = itemView.findViewById(R.id.scroll_text_view); + } + + void setHeight(int height) { + mScrollTextView.setHeight(height); + } + } + + private static class ScrollListItem { + + final int mHeight; + final int mType; + + static ScrollListItem createTextItemWithHeight(int height) { + return new ScrollListItem(TYPE_TEXT, height); + } + static ScrollListItem createButtonsItem() { + return new ScrollListItem(TYPE_BUTTONS, 0); + } + + ScrollListItem(int type, int height) { + this.mType = type; + this.mHeight = height; + } + + int getHeight() { + return mHeight; + } + + int getType() { + return mType; + } + } +} |