我正在尝试检测当前视图在回收视图中是否可见。使用布局管理器的isViewPartiallyVisible方法,我可以传入视图并查看它是否部分可见。当我将Textview传递给方法时,我得到一个类强制转换异常:
android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
所以我在所有引用和xml布局中将Textview更改为AppCompatTextView,以使库版本与recycleviews匹配。我仍然得到同样的错误。
编辑---这是调用方法
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.AppCompatTextView
....
private fun withinView(wk_predictionTV: WeakReference<AppCompatTextView>, recyclerView: RecyclerView): Boolean {
try {
return recyclerView.layoutManager.isViewPartiallyVisible(predictionTV.get()!!, false, true)
} catch (e: Exception) {
Timber.e("Failed:${e.printStackTrace()}")
}
return true
}
list_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/train_list_filter_edtext"
android:layout_width="match_parent"
android:layout_height="64dp"
android:hint="Enter Station Name"
android:inputType="text" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.captain_miao.optroundcardview.OptRoundCardView
android:id="@+id/top_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/trainlist_side_margins"
android:layout_marginRight="@dimen/trainlist_side_margins"
android:layout_marginTop="@dimen/trainlist_card_margins"
android:paddingTop="0dp"
app:optRoundCardCornerRadius="8dp"
app:optRoundCardLeftBottomCorner="false"
app:optRoundCardLeftTopCorner="true"
app:optRoundCardRightBottomCorner="false"
app:optRoundCardRightTopCorner="true"
>
..
</com.github.captain_miao.optroundcardview.OptRoundCardView>
</LinearLayout>
有什么想法吗?