仅当 RecyclerView 可滚动时,如何向 RecyclerView 中的最后一个

我想底部填充添加到最后一个视图中RecyclerView,但只有当RecyclerView是滚动的-换句话说,如果最后一个适配器产品,没有滚动完全可见,不添加填充。我意识到从 onBindViewHolder 中,findLastCompletelyVisibleItemPosition()将始终返回前一项的位置,因为当前视图在技术上尚不可见。我也尝试了 ItemDecorator 但这也不起作用,因为它们是在视图之前添加的,所以我们仍然不知道 RecyclerView 是否可滚动。我的理想方法如下所示:


@Override

public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

    ...

    if (position == items.size() - 1 && [is scrollable]) {

        ((MyViewHolder) viewHolder).addBottomPadding(...);

    }

}

那[可滚动]位是我不确定的。有没有另一种方法来实现这一点?


慕标5832272
浏览 193回答 2
2回答

神不在的星期二

好的,所以我为您提供了一个解决方案:)。核心概念是将 附加viewTreeObserver OnGlobalLayoutListener到onBind. 然后在OnGlobalLayout测量视图后调用它的方法,这对您的要求至关重要。一旦被调用,只需计算y + itemViewHeight并将其与height of the RecyclerView. 要获得此高度,您还必须将 附加OnGlobalLayoutListener到 RecyclerView,然后设置Adapter with the height of it as parameter. 您可能需要在调用 onGobalLayout 之前设置一个“空”适配器以防止错误。要记住两件重要的事情:- 永远不要忘记删除 OnGlobalLayoutListener- 下面的代码很丑陋,只是一个最小的可行产品活动:class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {&nbsp; &nbsp; super.onCreate(savedInstanceState)&nbsp; &nbsp; setContentView(R.layout.activity_main)&nbsp; &nbsp; rvTest.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)&nbsp; &nbsp; //maybe an adapter without content has to be provided so you won't get the error: no adapter attached skipping layout&nbsp; &nbsp; rvTest.viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {&nbsp; &nbsp; &nbsp; &nbsp; override fun onGlobalLayout() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rvTest.adapter = RvTestAdapter(this@MainActivity, rvTest.height)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rvTest.viewTreeObserver.removeOnGlobalLayoutListener(this) //must remove!&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })}}适配器:class RvTestAdapter(val context: Context, val recyclerViewHeight: Int): RecyclerView.Adapter<TestViewHolder>() {&nbsp; &nbsp; override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = TestViewHolder(LayoutInflater.from(context).inflate(R.layout.vh_test, parent, false))&nbsp; &nbsp; override fun getItemCount() = 3&nbsp; &nbsp; override fun onBindViewHolder(holder: TestViewHolder, position: Int) {&nbsp; &nbsp; &nbsp; &nbsp; if (position == 2) { //last position&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.itemView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun onGlobalLayout() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (holder.itemView.y + holder.itemView.height&nbsp; > recyclerViewHeight) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("YESSSS", "WOOP WOOP")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.itemView.viewTreeObserver.removeOnGlobalLayoutListener(this)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}(希望你不介意 Kotlin 代码)

繁花不似锦

将android:clipToPadding属性添加到您的视图,并为您的视图提供填充,该视图将随 recylerview 项目滚动。&nbsp; <android.support.v7.widget.RecyclerView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:clipToPadding="false"&nbsp; &nbsp; &nbsp; &nbsp; android:paddingBottom="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:paddingTop="16dp"&nbsp; &nbsp; &nbsp;/>希望这会有所帮助
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java