Layout animations

This commit is contained in:
Daniele 2021-09-05 17:35:37 +02:00
parent 5026415e45
commit 387ea890de
5 changed files with 109 additions and 32 deletions

View File

@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager
import it.danieleverducci.subitobeers.entities.Beer import it.danieleverducci.subitobeers.entities.Beer
import it.danieleverducci.subitobeers.ui.BeerDetailFragment import it.danieleverducci.subitobeers.ui.BeerDetailFragment
@ -17,6 +18,7 @@ class MainActivity : AppCompatActivity(), BeerNavigation {
override fun showBeerDetail(beer: Beer) { override fun showBeerDetail(beer: Beer) {
val detailFragment = BeerDetailFragment(beer) val detailFragment = BeerDetailFragment(beer)
supportFragmentManager.beginTransaction() supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
.replace(R.id.fragment_container, detailFragment) .replace(R.id.fragment_container, detailFragment)
.addToBackStack(null) .addToBackStack(null)
.commit() .commit()

View File

@ -24,9 +24,15 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
enum class FILTERTYPE {SINCE, TO, CLEAR} enum class FILTERTYPE {SINCE, TO, CLEAR}
companion object {
val INSTANCESTATE_FILTER_OPEN = "filter_open"
val INSTANCESTATE_FILTER_SINCE = "filter_since"
val INSTANCESTATE_FILTER_TO = "filter_to"
}
private val rvAdapter = BeerRecyclerAdapter() private val rvAdapter = BeerRecyclerAdapter()
private val repo = BeersRepository() private val repo = BeersRepository()
lateinit var binding: FragmentBeersListBinding private var binding: FragmentBeersListBinding? = null
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
@ -34,36 +40,52 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
): View? { ): View? {
setHasOptionsMenu(true); setHasOptionsMenu(true);
// Inflate layout if (binding == null) {
binding = FragmentBeersListBinding.inflate( // Inflate layout
LayoutInflater.from(container!!.context), binding = FragmentBeersListBinding.inflate(
container, LayoutInflater.from(container!!.context),
false container,
) false
)
// Register filter buttons listeners // Register filter buttons listeners
binding.listFilterSinceBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.SINCE) } binding!!.listFilterSinceBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.SINCE) }
binding.listFilterToBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.TO) } binding!!.listFilterToBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.TO) }
binding.listFilterClear.setOnClickListener { onFilterButtonClicked(FILTERTYPE.CLEAR) } binding!!.listFilterClear.setOnClickListener { onFilterButtonClicked(FILTERTYPE.CLEAR) }
// Set the adapter // Set the adapter
with(binding.list) { with(binding!!.list) {
layoutManager = LinearLayoutManager(context) layoutManager = LinearLayoutManager(context)
adapter = rvAdapter adapter = rvAdapter
}
// Register for recyclerview adapter events
rvAdapter.listener = this
// Register for network completed events
repo.listener = this
// Restore state
repo.filter.brewedBefore = savedInstanceState?.getString(INSTANCESTATE_FILTER_TO)
repo.filter.brewedAfter = savedInstanceState?.getString(INSTANCESTATE_FILTER_SINCE)
binding!!.listFilterDropdown.visibility =
if (savedInstanceState?.getBoolean(INSTANCESTATE_FILTER_OPEN) == true) View.VISIBLE else View.GONE
// Load first page
loadBeers(true)
} }
// Register for recyclerview adapter events return binding!!.root
rvAdapter.listener = this
// Register for network completed events
repo.listener = this
// Load first page
loadBeers(true)
return binding.root
} }
override fun onSaveInstanceState(outState: Bundle) {
outState.putBoolean(INSTANCESTATE_FILTER_OPEN, binding!!.listFilterDropdown.visibility == View.VISIBLE)
outState.putString(INSTANCESTATE_FILTER_SINCE, repo.filter.brewedAfter)
outState.putString(INSTANCESTATE_FILTER_TO, repo.filter.brewedBefore)
super.onSaveInstanceState(outState)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater) super.onCreateOptionsMenu(menu, inflater)
requireActivity().menuInflater.inflate(R.menu.list_menu, menu) requireActivity().menuInflater.inflate(R.menu.list_menu, menu)
@ -101,8 +123,8 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
if (type == FILTERTYPE.CLEAR) { if (type == FILTERTYPE.CLEAR) {
repo.filter.clear() repo.filter.clear()
loadBeers(true) loadBeers(true)
binding.listFilterSinceBt.setText(R.string.list_filter_brewed_after) binding!!.listFilterSinceBt.setText(R.string.list_filter_brewed_after)
binding.listFilterToBt.setText(R.string.list_filter_brewed_before) binding!!.listFilterToBt.setText(R.string.list_filter_brewed_before)
return return
} }
@ -111,10 +133,10 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
if (type == FILTERTYPE.SINCE) { if (type == FILTERTYPE.SINCE) {
repo.filter.setBrewedAfter(selMonth, selYear) repo.filter.setBrewedAfter(selMonth, selYear)
binding.listFilterSinceBt.setText(repo.filter.brewedAfter) binding!!.listFilterSinceBt.setText(repo.filter.brewedAfter)
} else { } else {
repo.filter.setBrewedBefore(selMonth, selYear) repo.filter.setBrewedBefore(selMonth, selYear)
binding.listFilterToBt.setText(repo.filter.brewedBefore) binding!!.listFilterToBt.setText(repo.filter.brewedBefore)
} }
// Update list // Update list
@ -146,8 +168,8 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
* Toggles beer filter visibility * Toggles beer filter visibility
*/ */
private fun toggleFilter() { private fun toggleFilter() {
binding.listFilterDropdown.visibility = binding!!.listFilterDropdown.visibility =
if (binding.listFilterDropdown.visibility == View.VISIBLE) View.GONE else View.VISIBLE if (binding!!.listFilterDropdown.visibility == View.VISIBLE) View.GONE else View.VISIBLE
} }
/** /**

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/anim/slide_in_right.xml
**
** Copyright 2007, 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.
*/
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/anim/slide_out_left.xml
**
** Copyright 2007, 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.
*/
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>

View File

@ -4,7 +4,8 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical"
android:animateLayoutChanges="true">
<LinearLayout <LinearLayout
android:id="@+id/list_filter_dropdown" android:id="@+id/list_filter_dropdown"