-
Notifications
You must be signed in to change notification settings - Fork 15
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 #198 from rubensousa/expose_focus
Add OnViewFocusedListener to observe focus changes
- Loading branch information
Showing
23 changed files
with
807 additions
and
8 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...est-fixtures/src/main/java/com/rubensousa/dpadrecyclerview/testfixtures/DpadFocusEvent.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,22 @@ | ||
/* | ||
* Copyright 2024 Rúben Sousa | ||
* | ||
* 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.rubensousa.dpadrecyclerview.testfixtures | ||
|
||
import android.view.View | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
data class DpadFocusEvent(val parent: RecyclerView.ViewHolder, val child: View, val position: Int) |
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
117 changes: 117 additions & 0 deletions
117
...iew/src/androidTest/kotlin/com/rubensousa/dpadrecyclerview/test/TestNestedListFragment.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,117 @@ | ||
/* | ||
* Copyright 2024 Rúben Sousa | ||
* | ||
* 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.rubensousa.dpadrecyclerview.test | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.fragment.app.Fragment | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.rubensousa.dpadrecyclerview.DpadRecyclerView | ||
import com.rubensousa.dpadrecyclerview.OnViewFocusedListener | ||
import com.rubensousa.dpadrecyclerview.test.tests.AbstractTestAdapter | ||
import com.rubensousa.dpadrecyclerview.testfixtures.DpadFocusEvent | ||
|
||
class TestNestedListFragment : Fragment(R.layout.dpadrecyclerview_test_container) { | ||
|
||
private val configuration = TestAdapterConfiguration( | ||
itemLayoutId = R.layout.dpadrecyclerview_item_horizontal, | ||
numberOfItems = 200 | ||
) | ||
private val parentFocusEvents = arrayListOf<DpadFocusEvent>() | ||
private val childFocusEvents = arrayListOf<DpadFocusEvent>() | ||
private val parentFocusListener = object : OnViewFocusedListener { | ||
override fun onViewFocused( | ||
parent: RecyclerView.ViewHolder, | ||
child: View, | ||
) { | ||
parentFocusEvents.add(DpadFocusEvent(parent, child, parent.layoutPosition)) | ||
} | ||
} | ||
private val childFocusListener = object : OnViewFocusedListener { | ||
override fun onViewFocused( | ||
parent: RecyclerView.ViewHolder, | ||
child: View, | ||
) { | ||
childFocusEvents.add(DpadFocusEvent(parent, child, parent.layoutPosition)) | ||
} | ||
} | ||
private val nestedAdapter = NestedAdapter(configuration, childFocusListener) | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
val recyclerView = view.findViewById<DpadRecyclerView>(R.id.recyclerView) | ||
recyclerView.adapter = nestedAdapter | ||
recyclerView.addOnViewFocusedListener(parentFocusListener) | ||
recyclerView.requestFocus() | ||
} | ||
|
||
fun getChildFocusEvents() = childFocusEvents.toList() | ||
|
||
fun getParentFocusEvents() = parentFocusEvents.toList() | ||
|
||
fun getRecyclerView(): DpadRecyclerView = requireView().findViewById(R.id.recyclerView) | ||
|
||
class NestedAdapter( | ||
private val configuration: TestAdapterConfiguration, | ||
private val onViewFocusedListener: OnViewFocusedListener | ||
) : AbstractTestAdapter<ListViewHolder>(configuration) { | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder { | ||
return ListViewHolder( | ||
LayoutInflater.from(parent.context) | ||
.inflate(R.layout.dpadrecyclerview_nested_list, parent, false), | ||
configuration, | ||
onViewFocusedListener | ||
) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ListViewHolder, position: Int) { | ||
holder.bind(position) | ||
} | ||
|
||
} | ||
|
||
|
||
class ListViewHolder( | ||
val view: View, | ||
val configuration: TestAdapterConfiguration, | ||
onViewFocusedListener: OnViewFocusedListener, | ||
) : RecyclerView.ViewHolder(view) { | ||
|
||
private val adapter = TestAdapter( | ||
adapterConfiguration = configuration, | ||
onViewHolderSelected = { position -> }, | ||
onViewHolderDeselected = { position -> } | ||
) | ||
private val textView = view.findViewById<TextView>(R.id.textView) | ||
private val recyclerView = view.findViewById<DpadRecyclerView>(R.id.nestedRecyclerView) | ||
|
||
init { | ||
recyclerView.adapter = adapter | ||
recyclerView.addOnViewFocusedListener(onViewFocusedListener) | ||
} | ||
|
||
fun bind(position: Int) { | ||
textView.text = "List $position" | ||
} | ||
|
||
} | ||
|
||
} |
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
Oops, something went wrong.