chu*_*ubs 15 layout android android-fragments android-recyclerview
我使用多个屏幕RecyclerView,也有一个小的Fragment是对的顶部RecyclerView.当Fragment显示时,我想确保我可以滚动到底部RecyclerView.在Fragment并不总是显示.如果我使用了边距,RecyclerView我需要动态删除并在Fragment显示时添加它们.我可以将余量添加到列表中的最后一项,但这也很复杂,如果我稍后加载更多东西(即分页),我将不得不再次剥离这些边距.
如何动态添加或删除视图边距?有什么其他选择来解决这个问题?
Rea*_*hed 52
因此,如果你想在RecyclerView你的底部添加一些填充,你可以设置paddingBottom然后设置clipToPadding为false.这是一个例子
<android.support.v7.widget.RecyclerView
android:id="@+id/my_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="100dp" />
Run Code Online (Sandbox Code Playgroud)
Fra*_*esc 12
你应该使用Item Decorator.
public class MyItemDecoration extends RecyclerView.ItemDecoration {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
// only for the last one
if (parent.getChildAdapterPosition(view) == parent.getAdapter().getItemCount() - 1) {
outRect.top = /* set your margin here */;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在 kotlin 中使用它为最后一个索引提供边距RecyclerView
override fun onBindViewHolder(holder: RecyclerView.ViewHolder(view), position: Int) {
if (position == itemsList.lastIndex){
val params = holder.itemView.layoutParams as FrameLayout.LayoutParams
params.bottomMargin = 100
holder.itemView.layoutParams = params
}else{
val params = holder.itemView.layoutParams as RecyclerView.LayoutParams
params.bottomMargin = 0
holder.itemView.layoutParams = params
}
//other codes ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6713 次 |
| 最近记录: |