Skip to content

Commit

Permalink
Remove header in plant list (android#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiembo authored and nic0lette committed Sep 3, 2019
1 parent 9e1883d commit be3b464
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,80 +23,24 @@ import androidx.navigation.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.samples.apps.sunflower.PlantListFragment
import com.google.samples.apps.sunflower.R
import com.google.samples.apps.sunflower.HomeViewPagerFragmentDirections
import com.google.samples.apps.sunflower.PlantListFragment
import com.google.samples.apps.sunflower.data.Plant
import com.google.samples.apps.sunflower.databinding.ListItemPlantBinding

/**
* Adapter for the [RecyclerView] in [PlantListFragment].
*/
class PlantAdapter : ListAdapter<Plant, RecyclerView.ViewHolder>(PlantDiffCallback()) {
/**
* Int array of all the positions with header-style cards
*/
private val headerPositions = intArrayOf(0)

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

// if current position is a header element
if (position in headerPositions) {

// cast generic RecyclerView.ViewHolder to header view holder
val holderViewParams = (holder as HeaderViewHolder).itemView.layoutParams
val layoutParams = holderViewParams as StaggeredGridLayoutManager.LayoutParams

// set width of header to span entire screen
layoutParams.isFullSpan = true
} else { // Must be a plant type
val plant = getItem(position)

// cast generic RecyclerView.ViewHolder to Plant view holder
(holder as PlantViewHolder).bind(plant)
}
}

override fun getItemViewType(position: Int): Int {

// if current position is where a header should go, return header view type
return when (position) {
in headerPositions -> R.layout.plant_list_header
else -> R.layout.list_item_plant
}
}

override fun getItemCount(): Int {

// If no available plants, don't print header either, otherwise return number of cards needed
return if (currentList.size == 0) {
0
} else {
currentList.size + headerPositions.size
}
}

override fun getItem(position: Int): Plant {

// index in plant list is position subtracted by offset of number of headers
return currentList[position - headerPositions.size]
val plant = getItem(position)
(holder as PlantViewHolder).bind(plant)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

// Return a specific viewholder type depending on current viewType
return when (viewType) {

// Generic layout inflate from Layout XML file
R.layout.plant_list_header -> HeaderViewHolder(LayoutInflater.from(parent.context)
.inflate(R.layout.plant_list_header, parent, false))

R.layout.list_item_plant -> PlantViewHolder(ListItemPlantBinding
.inflate(LayoutInflater.from(parent.context), parent, false))

else -> throw IllegalArgumentException("Invalid viewType")
}
return PlantViewHolder(ListItemPlantBinding.inflate(
LayoutInflater.from(parent.context), parent, false))
}

class PlantViewHolder(
Expand Down Expand Up @@ -125,14 +69,6 @@ class PlantAdapter : ListAdapter<Plant, RecyclerView.ViewHolder>(PlantDiffCallba
}
}
}

/**
* HeaderViewHolder class is a basic implementation of RecyclerView.ViewHolder
* Adapt this class if custom bindings are necessary
*/
class HeaderViewHolder(
headerView: View
) : RecyclerView.ViewHolder(headerView)
}

private class PlantDiffCallback : DiffUtil.ItemCallback<Plant>() {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_plant_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingStart="@dimen/card_side_margin"
android:paddingEnd="@dimen/card_side_margin"
android:paddingStart="@dimen/card_side_margin"
android:paddingTop="@dimen/header_margin"
app:layoutManager="androidx.recyclerview.widget.StaggeredGridLayoutManager"
app:spanCount="@integer/grid_columns"
tools:context="com.google.samples.apps.sunflower.GardenActivity"
Expand Down
31 changes: 0 additions & 31 deletions app/src/main/res/layout/plant_list_header.xml

This file was deleted.

0 comments on commit be3b464

Please sign in to comment.