-
Notifications
You must be signed in to change notification settings - Fork 7.4k
(Analytics) implementing ViewPager2 #1427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
thatfiredev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't yet reviewed the Java and Kotlin files. I'll do it once the XML is fixed:
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Outdated
Show resolved
Hide resolved
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Show resolved
Hide resolved
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Outdated
Show resolved
Hide resolved
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Outdated
Show resolved
Hide resolved
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/kotlin/MainActivity.kt
Outdated
Show resolved
Hide resolved
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Outdated
Show resolved
Hide resolved
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Outdated
Show resolved
Hide resolved
| mViewPager = binding.viewPager; | ||
| mViewPager = (ViewPager2)findViewById(R.id.viewPager); | ||
| mViewPager.setAdapter(mImagePagerAdapter); | ||
| mViewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to set the orientation to Horizontal?
I think the default orientation is Horizontal only.
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Show resolved
Hide resolved
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Outdated
Show resolved
Hide resolved
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Outdated
Show resolved
Hide resolved
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/java/MainActivity.java
Outdated
Show resolved
Hide resolved
| //binding.viewPager.adapter = imagePagerAdapter | ||
| val viewPager: ViewPager2 = findViewById(R.id.viewPager) | ||
| viewPager.adapter = imagePagerAdapter | ||
|
|
||
| // Workaround for AppCompat issue not showing ViewPager titles | ||
| val params = binding.pagerTabStrip.layoutParams as ViewPager.LayoutParams | ||
| params.isDecor = true | ||
|
|
||
| // When the visible image changes, send a screen view hit. | ||
| binding.viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() { | ||
| override fun onPageSelected(position: Int) { | ||
| recordImageView() | ||
| recordScreenView() | ||
| } | ||
| }) | ||
| val tabLayout: TabLayout = findViewById(R.id.tab_layout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep viewbinding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we implement OnPageChangeCallback instead of addOnPageChangeListener
Because I'm getting an warning on addOnPageChangeListener what makes me think it is not recognized anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes, let's use OnPageChangeCallback. I believe the method is registerOnPageChangeCallback()
| */ | ||
| private fun getCurrentImageId(): String { | ||
| val position = binding.viewPager.currentItem | ||
| val position = 0//binding.viewPager.currentItem |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| val position = 0//binding.viewPager.currentItem | |
| val position = binding.viewPager.currentItem |
| @SuppressLint("WrongConstant") | ||
| inner class ImagePagerAdapter( | ||
| fm: FragmentManager, | ||
| fm: FragmentActivity, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use the same constructor we used on Java.
| * A [FragmentStateAdapter] that returns a fragment corresponding to | ||
| * one of the sections/tabs/pages. | ||
| */ | ||
| @SuppressLint("WrongConstant") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| @SuppressLint("WrongConstant") |
| override fun getItemCount(): Int { | ||
| return infos.size | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this can actually become a single line:
| override fun getItemCount(): Int { | |
| return infos.size | |
| } | |
| override fun getItemCount(): Int = infos.size |
thatfiredev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AlbertoYabeda Thanks for patiently working on each and every review comment here.
We're 99% done 😬 Just a few more comments
| /local.properties | ||
| .DS_Store | ||
| build/ | ||
| google-services.json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line shouldn't be removed. 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ops! 😅
| */ | ||
| private String getCurrentImageId() { | ||
| int position = mViewPager.getCurrentItem(); | ||
| int position = mViewPager.getCurrentItem();; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| int position = mViewPager.getCurrentItem();; | |
| int position = mViewPager.getCurrentItem(); |
| val viewPager: ViewPager2 = findViewById(R.id.viewPager) | ||
| viewPager.adapter = imagePagerAdapter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's stick to ViewBinding:
| val viewPager: ViewPager2 = findViewById(R.id.viewPager) | |
| viewPager.adapter = imagePagerAdapter |
|
|
||
| binding.viewPager.registerOnPageChangeCallback(pageChangedCallback) | ||
|
|
||
| val tabLayout: TabLayout = findViewById(R.id.tab_layout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's stick to ViewBinding:
| val tabLayout: TabLayout = findViewById(R.id.tab_layout) | |
| val tabLayout: TabLayout = binding.tabLayout |
| lc: Lifecycle | ||
| ) : FragmentStateAdapter(fm, lc) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe we should make the variable name consistent with the java sample:
| lc: Lifecycle | |
| ) : FragmentStateAdapter(fm, lc) { | |
| lifecycle: Lifecycle | |
| ) : FragmentStateAdapter(fm, lifecycle) { |
|
Thanks for your patience reviewing my PR. 😄 |
| val viewPager: ViewPager2 = binding.viewPager | ||
| viewPager.adapter = imagePagerAdapter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is simply repeating the line above it.
| val viewPager: ViewPager2 = binding.viewPager | |
| viewPager.adapter = imagePagerAdapter |
analytics/app/src/main/java/com/google/firebase/quickstart/analytics/kotlin/MainActivity.kt
Show resolved
Hide resolved
| binding.viewPager.registerOnPageChangeCallback(pageChangedCallback) | ||
|
|
||
| val tabLayout: TabLayout = binding.tabLayout | ||
| TabLayoutMediator(tabLayout, viewPager) { tab, position -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the code repetition comment gets applied:
| TabLayoutMediator(tabLayout, viewPager) { tab, position -> | |
| TabLayoutMediator(tabLayout, binding.viewPager) { tab, position -> |
| app:layout_constraintBottom_toTopOf="@+id/viewPager" | ||
| app:layout_constraintEnd_toEndOf="@id/viewPager" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not attach this to the viewPager. (note that this is my personal preference, I don't think there's specific guidance on that.)
| app:layout_constraintBottom_toTopOf="@+id/viewPager" | |
| app:layout_constraintEnd_toEndOf="@id/viewPager" | |
| app:layout_constraintEnd_toEndOf="parent" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll set the end to EndOf "parent"
but I think the Bottom of the tabLayout should be at the top of ViewPager.
Correct me if I'm wrong.
| android:layout_width="match_parent" | ||
| android:layout_height="0dp" | ||
| app:layout_constraintBottom_toBottomOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@+id/tab_layout" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This layout seems to be missing horizontal constraints. Can we have some constraints at the Start and End? That will allow us to set the width to "0dp".
| android:layout_width="match_parent" | |
| android:layout_height="0dp" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/tab_layout" /> | |
| android:layout_width="0dp" | |
| android:layout_height="0dp" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintTop_toBottomOf="@+id/tab_layout" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh Yes. that would be awesome.
thatfiredev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much @AlbertoYabeda !
|
Welcome |
The tab names are appearing.
But the images are not loading on the screen.