Skip to content

Instantly share code, notes, and snippets.

@hilfritz
Created June 5, 2018 06:42
Show Gist options
  • Save hilfritz/23ae7e8072de6f9ff1577a439828e016 to your computer and use it in GitHub Desktop.
Save hilfritz/23ae7e8072de6f9ff1577a439828e016 to your computer and use it in GitHub Desktop.
Scrollable TextView inside Recyclerview
//recyclerview list item layout xml with scrollable textview
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/managerCommentsDisplay"
android:lines="3"
android:textSize="@dimen/fontSizeCaption2"
android:maxLines = "3"
android:scrollbars="vertical"
/>
</LinearLayout>
</LinearLayout>
//the recyclerview adapter with scrollable textview
class NotesListAdapter(
list: ArrayList<NotesPojo>
): RecyclerView.Adapter<NotesListAdapter.ListRowHolder>() {
val TAG = "NotesListAdapter"
private val sList = list
public interface OnTextChangeListener{
fun onTextChangeDetected()
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ListRowHolder {
val v = LayoutInflater.from(parent?.context).inflate(R.layout.notes_list_item, parent, false)
// pass MyCustomEditTextListener to viewholder in onCreateViewHolder
// so that we don't have to do this expensive allocation in onBindViewHolder
val vh = ListRowHolder(v, MyCustomEditTextListener(), noteImageWidthHeight)
return vh
}
fun log(str:String){
Log.d(TAG, str)
}
override fun onBindViewHolder(vh: ListRowHolder, position: Int) {
val notesPojo = sList[position]
if (notesPojo.managerNote!=null){
str = notesPojo.managerNote
}
vh.managerCommentsDisplay.text = str
}
override fun getItemCount(): Int {
return sList.size
}
/**
* see https://stackoverflow.com/questions/31844373/saving-edittext-content-in-recyclerview
*/
class ListRowHolder (row: View, tcl:MyCustomEditTextListener, noteImageWidthHeight:Array<Int>) : RecyclerView.ViewHolder(row) {
// each data item is just a string in this case
var notesPojo:NotesPojo? =null
public val managerCommentsDisplay: TextView = row.findViewById(R.id.managerCommentsDisplay) as TextView
val TAG = "ListRowHolder"
public val textChangeListener: MyCustomEditTextListener = tcl
init {
this.managerCommentsDisplay.movementMethod = object: ScrollingMovementMethod(){}
//https://stackoverflow.com/questions/8121491/is-it-possible-to-add-a-scrollable-textview-to-a-listview
val listener = object : View.OnTouchListener {
override fun onTouch(v: View, event: MotionEvent): Boolean {
val isLarger: Boolean
isLarger = (v as TextView).lineCount * v.lineHeight > v.getHeight()
if (event.action === MotionEvent.ACTION_MOVE && isLarger) {
v.getParent().requestDisallowInterceptTouchEvent(true)
} else {
v.getParent().requestDisallowInterceptTouchEvent(false)
}
return false
}
}
this.managerCommentsDisplay.setOnTouchListener(listener)
}
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment