-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from rubensousa/fix_edge_alignment
Fix edge alignment when a small number of items is visible in the screen
- Loading branch information
Showing
9 changed files
with
218 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
.../src/main/java/com/rubensousa/dpadrecyclerview/sample/ui/screen/list/ShortListFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.rubensousa.dpadrecyclerview.sample.ui.screen.list | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.compose.ui.unit.dp | ||
import androidx.fragment.app.Fragment | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.rubensousa.dpadrecyclerview.DpadViewHolder | ||
import com.rubensousa.dpadrecyclerview.ParentAlignment | ||
import com.rubensousa.dpadrecyclerview.sample.R | ||
import com.rubensousa.dpadrecyclerview.sample.databinding.MainAdapterItemFeatureBinding | ||
import com.rubensousa.dpadrecyclerview.sample.databinding.ScreenRecyclerviewBinding | ||
import com.rubensousa.dpadrecyclerview.sample.ui.dpToPx | ||
import com.rubensousa.dpadrecyclerview.sample.ui.viewBinding | ||
import com.rubensousa.dpadrecyclerview.sample.ui.widgets.common.ItemAnimator | ||
import com.rubensousa.dpadrecyclerview.spacing.DpadLinearSpacingDecoration | ||
|
||
class ShortListFragment : Fragment(R.layout.screen_recyclerview) { | ||
|
||
private val binding by viewBinding(ScreenRecyclerviewBinding::bind) | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
binding.recyclerView.apply { | ||
setParentAlignment( | ||
ParentAlignment( | ||
edge = ParentAlignment.Edge.NONE, | ||
fraction = 0.5f | ||
) | ||
) | ||
addItemDecoration( | ||
DpadLinearSpacingDecoration.create( | ||
itemSpacing = dpToPx(16.dp), | ||
perpendicularEdgeSpacing = dpToPx(48.dp) | ||
) | ||
) | ||
adapter = Adapter( | ||
items = List(5) { i -> | ||
"Item $i" | ||
} | ||
) | ||
requestFocus() | ||
} | ||
} | ||
|
||
internal class Adapter( | ||
private val items: List<String>, | ||
) : RecyclerView.Adapter<Adapter.ViewHolder>() { | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
return ViewHolder( | ||
MainAdapterItemFeatureBinding.inflate( | ||
LayoutInflater.from(parent.context), parent, false | ||
) | ||
) | ||
} | ||
|
||
override fun getItemCount(): Int = items.size | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
holder.bind(items[position]) | ||
} | ||
|
||
override fun onViewRecycled(holder: ViewHolder) { | ||
super.onViewRecycled(holder) | ||
holder.recycle() | ||
} | ||
|
||
class ViewHolder( | ||
private val binding: MainAdapterItemFeatureBinding, | ||
) : RecyclerView.ViewHolder(binding.root), DpadViewHolder { | ||
|
||
private val animator = ItemAnimator(binding.root) | ||
|
||
init { | ||
itemView.setOnFocusChangeListener { v, hasFocus -> | ||
if (hasFocus) { | ||
animator.startFocusGainAnimation() | ||
} else { | ||
animator.startFocusLossAnimation() | ||
} | ||
} | ||
} | ||
|
||
fun bind(item: String) { | ||
binding.textView.text = item | ||
} | ||
|
||
fun recycle() { | ||
animator.cancel() | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
sample/src/main/res/drawable/list_text_container_background.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item android:drawable="@color/focused_text" android:state_focused="true" /> | ||
<item android:drawable="@color/transparent" /> | ||
</selector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters