Skip to content

Commit

Permalink
Merge pull request #13 from alex-tiurin/bindViewHolder
Browse files Browse the repository at this point in the history
bindViewHolder in UI thread
  • Loading branch information
alex-tiurin authored Feb 17, 2022
2 parents ef10cac + ebd0823 commit 1f90957
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,9 @@ class RecyclerViewTest : BaseTest() {
.item(hasDescendant(allOf(withId(R.id.tv_name), withText(containsString(CONTACTS[2].name)))))
.isDisplayed()
}

@Test
fun createHandlerFromUiTest(){
page.recycler.getItemAdapterPositionAtIndex(hasDescendant(allOf(withId(R.id.tv_name), withText(containsString(CONTACTS.last().name)))), 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte


viewManager = LinearLayoutManager(this)
viewAdapter = ContactAdapter(ArrayList<Contact>(),
viewAdapter = ContactAdapter(this, ArrayList<Contact>(),
object : ContactAdapter.OnItemClickListener {
override fun onItemClick(contact: Contact) {
val intent = Intent(applicationContext, ChatActivity::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package com.atiurin.sampleapp.adapters

import android.content.Context
import android.view.GestureDetector
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.GestureDetectorCompat
import androidx.recyclerview.widget.RecyclerView
import com.atiurin.sampleapp.R
import com.atiurin.sampleapp.data.entities.Contact
import com.atiurin.sampleapp.view.CircleImageView

class ContactAdapter(private var mDataset: ArrayList<Contact>, val listener: OnItemClickListener) :
RecyclerView.Adapter<ContactAdapter.MyViewHolder>() {
class ContactAdapter(
val context: Context,
private var mDataset: ArrayList<Contact>,
val listener: OnItemClickListener
) :
RecyclerView.Adapter<ContactAdapter.MyViewHolder>(), GestureDetector.OnGestureListener {

interface OnItemClickListener {
fun onItemClick(item: Contact)
}

class MyViewHolder(val view: LinearLayout) : RecyclerView.ViewHolder(view)

open fun updateData(data: ArrayList<Contact>) {
Expand All @@ -37,8 +46,19 @@ class ContactAdapter(private var mDataset: ArrayList<Contact>, val listener: OnI
val status = holder.view.findViewById(R.id.tv_status) as TextView
tvTitle.text = mDataset[position].name
status.text = mDataset[position].status
avatar.setImageDrawable(holder.view.context.getResources().getDrawable(mDataset[position].avatar))
avatar.setImageDrawable(
holder.view.context.getResources().getDrawable(mDataset[position].avatar)
)
GestureDetectorCompat(context, this)
}

override fun getItemCount() = mDataset.size

// GestureDetector.OnGestureListener
override fun onDown(p0: MotionEvent?): Boolean = true
override fun onShowPress(p0: MotionEvent?) = Unit
override fun onSingleTapUp(p0: MotionEvent?): Boolean = true
override fun onScroll(p0: MotionEvent?, p1: MotionEvent?, p2: Float, p3: Float): Boolean = true
override fun onLongPress(p0: MotionEvent?) = Unit
override fun onFling(p0: MotionEvent?, p1: MotionEvent?, p2: Float, p3: Float): Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.util.SparseArray
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.util.HumanReadables
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
Expand All @@ -26,7 +27,10 @@ internal fun <T : VH, VH : RecyclerView.ViewHolder> itemsMatching(
viewHolderCache.put(itemType, cachedViewHolder)
}
// Bind data to ViewHolder and apply matcher to view descendants.
adapter.bindViewHolder((cachedViewHolder as T?)!!, position)
runOnUiThread{
//requires UI thread to create handler in some cases
adapter.bindViewHolder((cachedViewHolder as T?)!!, position)
}
if (viewHolderMatcher.matches(cachedViewHolder)) {
matchedItems.add(MatchedItem(position,
HumanReadables.getViewHierarchyErrorMessage(
Expand Down

0 comments on commit 1f90957

Please sign in to comment.