I have a recyclerview like this:
The distance between all items is 15dp. Except for the first item, I want to check the left and right distances on other items and hide that item if it is less than 15 dp. How can I do that?
Thank you!
Item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@ id/intervalCard"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textview.MaterialTextView
android:id="@ id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
tools:text="99" />
<View
android:id="@ id/dateIndicator"
android:layout_width="0.5dp"
android:layout_height="5dp"
android:background="@color/ripple_gray" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter.kt
class TimeIntervalAdapter(
private val data: List<String>
) : RecyclerView.Adapter<TimeIntervalAdapter.ItemHolder>() {
private val items: MutableList<TimeIntervalItem>
init {
this.items = ArrayList()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.time_interval_item, parent, false)
return ItemHolder(view)
}
override fun onBindViewHolder(holder: ItemHolder, position: Int) {
holder.itemTitle.text = data[position]
}
override fun getItemCount(): Int = data.size
inner class ItemHolder
internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
val itemTitle: MaterialTextView = itemView.findViewById(R.id.itemTitle)
}
}
LayoutManager.kt - for set dynamic distance to items -
class SpanningLinearLayoutManager : LinearLayoutManager {
constructor(context: Context?) : super(context) {}
constructor(context: Context?, orientation: Int, reverseLayout: Boolean) : super(
context,
orientation,
reverseLayout
) {
}
constructor(
context: Context?,
attrs: AttributeSet?,
defStyleAttr: Int,
defStyleRes: Int
) : super(context, attrs, defStyleAttr, defStyleRes) {
}
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
return spanLayoutSize(super.generateDefaultLayoutParams())
}
override fun generateLayoutParams(c: Context, attrs: AttributeSet): RecyclerView.LayoutParams {
return spanLayoutSize(super.generateLayoutParams(c, attrs))
}
override fun generateLayoutParams(lp: ViewGroup.LayoutParams): RecyclerView.LayoutParams {
return spanLayoutSize(super.generateLayoutParams(lp))
}
override fun checkLayoutParams(lp: RecyclerView.LayoutParams): Boolean {
return super.checkLayoutParams(lp)
}
private fun spanLayoutSize(layoutParams: RecyclerView.LayoutParams): RecyclerView.LayoutParams {
if (orientation == HORIZONTAL) {
layoutParams.width = horizontalSpace.roundToInt()
}
return layoutParams
}
override fun canScrollVertically(): Boolean {
return false
}
override fun canScrollHorizontally(): Boolean {
return false
}
var horizontalSpace: Double = 0.0
get() {
return if (field > 1440) (timelineWidth * 60.0 * 24.0) / (field)
else (timelineWidth * 60.0) / (field)
}
var timelineWidth: Int = 0
}
CodePudding user response:
You can hide view for start position in onBindViewHolder method of Adapter
Example code:
if(position == 0){
holder.viewBefore.setVisibility(View.GONE);
}else{
holder.viewBefore.setVisibility(View.VISIBLE);
}
viewBefore is your view name that you want to hide or visible.

